<commit_before>//
//  Copyright (c) 2014-2016 Actor LLC. <https://actor.im>
//

import Foundation
import UIKit
import YYText
import YYImage

public class AABubbleTextCell : AABubbleCell {
    
    // TODO: Better max width calculations
    
    static let fontSize: CGFloat = AADevice.isiPad ? 17 : 16
    static let fontRegular = UIFont.textFontOfSize(fontSize)
    static let fontItalic = UIFont.italicTextFontOfSize(fontSize)
    static let fontBold = UIFont.boldTextFontOfSize(fontSize)
    
    private static let dateFont = UIFont.italicSystemFontOfSize(11)
    private static let senderFont = UIFont.boldSystemFontOfSize(15)
    
    static let bubbleFont = fontRegular
    static let bubbleFontUnsupported = fontItalic
    static let senderHeight = CGFloat(20)
    
    private let messageText = YYLabel()
    private let senderNameLabel = YYLabel()
    private let dateText = YYLabel()
    private let statusView = UIImageView()
    
    private var needRelayout = true
    private var isClanchTop:Bool = false
    private var isClanchBottom:Bool = false
    
    
    private var messageState = ACMessageState.UNKNOWN().ordinal()
    private var cellLayout: TextCellLayout!
    
    public init(frame: CGRect) {
        super.init(frame: frame, isFullSize: false)
        
        messageText.displaysAsynchronously = true
        messageText.ignoreCommonProperties = true
        messageText.clearContentsBeforeAsynchronouslyDisplay = false
        messageText.fadeOnAsynchronouslyDisplay = true
        messageText.highlightTapAction = { (containerView: UIView, text: NSAttributedString, range: NSRange, rect: CGRect) -> () in
            let attributes = text.attributesAtIndex(range.location, effectiveRange: nil)
            if let attrs = attributes["YYTextHighlight"] as? YYTextHighlight {
                if let url = attrs.userInfo!["url"] as? String {
                    self.openUrl(NSURL(string: url)!)
                }
            }
        }
        messageText.highlightLongPressAction = { (containerView: UIView, text: NSAttributedString, range: NSRange, rect: CGRect) -> () in
            let attributes = text.attributesAtIndex(range.location, effectiveRange: nil)
            if let attrs = attributes["YYTextHighlight"] as? YYTextHighlight {
                if let url = attrs.userInfo!["url"] as? String {
                    self.urlLongTap(NSURL(string: url)!)
                }
            }
        }

        
        
        senderNameLabel.displaysAsynchronously = true
        senderNameLabel.ignoreCommonProperties = true
        senderNameLabel.fadeOnAsynchronouslyDisplay = true
        senderNameLabel.clearContentsBeforeAsynchronouslyDisplay = false
        
        dateText.displaysAsynchronously = true
        dateText.clearContentsBeforeAsynchronouslyDisplay = false
        dateText.fadeOnAsynchronouslyDisplay = true
        dateText.font = AABubbleTextCell.dateFont
        dateText.lineBreakMode = .ByClipping
        dateText.numberOfLines = 1
        dateText.textAlignment = .Right
        
        statusView.contentMode = UIViewContentMode.Center
        
        contentView.addSubview(messageText)
        contentView.addSubview(dateText)
        contentView.addSubview(statusView)
        contentView.addSubview(senderNameLabel)
    }
    
    public required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // Data binding
    
    public override func bind(message: ACMessage, reuse: Bool, cellLayout: AACellLayout, setting: AACellSetting) {
        
        // Saving cell settings
        self.cellLayout = cellLayout as! TextCellLayout
        self.isClanchTop = setting.clenchTop
        self.isClanchBottom = setting.clenchBottom
        
        if (!reuse) {
            
            // When not reusing force to relayout view
            needRelayout = true

            // Text Layout
            messageText.textLayout = self.cellLayout.textLayout
            
            // Setting sender name if needed
            if isGroup && !isOut {
                senderNameLabel.hidden = false
                senderNameLabel.textLayout = self.cellLayout.senderLayout
            } else {
                senderNameLabel.hidden = true
                senderNameLabel.textLayout = nil
            }
        }
        
        // Always update bubble insets
        if (isOut) {
            bindBubbleType(.TextOut, isCompact: isClanchBottom)
            // dateText.textColor = appStyle.chatTextDateOutColor
            
            bubbleInsets = UIEdgeInsets(
                top: (isClanchTop ? AABubbleCell.bubbleTopCompact : AABubbleCell.bubbleTop),
                left: 0 + (AADevice.isiPad ? 16 : 0),
                bottom: (isClanchBottom ? AABubbleCell.bubbleBottomCompact : AABubbleCell.bubbleBottom),
                right: (isClanchBottom ? 10 : 4) + (AADevice.isiPad ? 16 : 0))
            contentInsets = UIEdgeInsets(
                top: AABubbleCell.bubbleContentTop,
                left: 10,
                bottom: AABubbleCell.bubbleContentBottom,
                right: (isClanchBottom ? 4 : 10))
        } else {
            bindBubbleType(.TextIn, isCompact: isClanchBottom)
            // dateText.textColor = appStyle.chatTextDateInColor
            
            bubbleInsets = UIEdgeInsets(
                top: (isClanchTop ? AABubbleCell.bubbleTopCompact : AABubbleCell.bubbleTop),
                left: (isClanchBottom ? 10 : 4) + (AADevice.isiPad ? 16 : 0),
                bottom: (isClanchBottom ? AABubbleCell.bubbleBottomCompact : AABubbleCell.bubbleBottom),
                right: 0 + (AADevice.isiPad ? 16 : 0))
            contentInsets = UIEdgeInsets(
                top: (isGroup ? 18 : 0) + AABubbleCell.bubbleContentTop,
                left: (isClanchBottom ? 11 : 17),
                bottom: AABubbleCell.bubbleContentBottom,
                right: 10)<commit_msg>fix(iOS): Fixing date color<commit_after>//
//  Copyright (c) 2014-2016 Actor LLC. <https://actor.im>
//

import Foundation
import UIKit
import YYText
import YYImage

public class AABubbleTextCell : AABubbleCell {
    
    // TODO: Better max width calculations
    
    static let fontSize: CGFloat = AADevice.isiPad ? 17 : 16
    static let fontRegular = UIFont.textFontOfSize(fontSize)
    static let fontItalic = UIFont.italicTextFontOfSize(fontSize)
    static let fontBold = UIFont.boldTextFontOfSize(fontSize)
    
    private static let dateFont = UIFont.italicSystemFontOfSize(11)
    private static let senderFont = UIFont.boldSystemFontOfSize(15)
    
    static let bubbleFont = fontRegular
    static let bubbleFontUnsupported = fontItalic
    static let senderHeight = CGFloat(20)
    
    private let messageText = YYLabel()
    private let senderNameLabel = YYLabel()
    private let dateText = YYLabel()
    private let statusView = UIImageView()
    
    private var needRelayout = true
    private var isClanchTop:Bool = false
    private var isClanchBottom:Bool = false
    
    
    private var messageState = ACMessageState.UNKNOWN().ordinal()
    private var cellLayout: TextCellLayout!
    
    public init(frame: CGRect) {
        super.init(frame: frame, isFullSize: false)
        
        messageText.displaysAsynchronously = true
        messageText.ignoreCommonProperties = true
        messageText.clearContentsBeforeAsynchronouslyDisplay = false
        messageText.fadeOnAsynchronouslyDisplay = true
        messageText.highlightTapAction = { (containerView: UIView, text: NSAttributedString, range: NSRange, rect: CGRect) -> () in
            let attributes = text.attributesAtIndex(range.location, effectiveRange: nil)
            if let attrs = attributes["YYTextHighlight"] as? YYTextHighlight {
                if let url = attrs.userInfo!["url"] as? String {
                    self.openUrl(NSURL(string: url)!)
                }
            }
        }
        messageText.highlightLongPressAction = { (containerView: UIView, text: NSAttributedString, range: NSRange, rect: CGRect) -> () in
            let attributes = text.attributesAtIndex(range.location, effectiveRange: nil)
            if let attrs = attributes["YYTextHighlight"] as? YYTextHighlight {
                if let url = attrs.userInfo!["url"] as? String {
                    self.urlLongTap(NSURL(string: url)!)
                }
            }
        }

        
        
        senderNameLabel.displaysAsynchronously = true
        senderNameLabel.ignoreCommonProperties = true
        senderNameLabel.fadeOnAsynchronouslyDisplay = true
        senderNameLabel.clearContentsBeforeAsynchronouslyDisplay = false
        
        dateText.displaysAsynchronously = true
        dateText.clearContentsBeforeAsynchronouslyDisplay = false
        dateText.fadeOnAsynchronouslyDisplay = true
        dateText.font = AABubbleTextCell.dateFont
        dateText.lineBreakMode = .ByClipping
        dateText.numberOfLines = 1
        dateText.textAlignment = .Right
        
        statusView.contentMode = UIViewContentMode.Center
        
        contentView.addSubview(messageText)
        contentView.addSubview(dateText)
        contentView.addSubview(statusView)
        contentView.addSubview(senderNameLabel)
    }
    
    public required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // Data binding
    
    public override func bind(message: ACMessage, reuse: Bool, cellLayout: AACellLayout, setting: AACellSetting) {
        
        // Saving cell settings
        self.cellLayout = cellLayout as! TextCellLayout
        self.isClanchTop = setting.clenchTop
        self.isClanchBottom = setting.clenchBottom
        
        if (!reuse) {
            
            // When not reusing force to relayout view
            needRelayout = true

            // Text Layout
            messageText.textLayout = self.cellLayout.textLayout
            
            // Setting sender name if needed
            if isGroup && !isOut {
                senderNameLabel.hidden = false
                senderNameLabel.textLayout = self.cellLayout.senderLayout
            } else {
                senderNameLabel.hidden = true
                senderNameLabel.textLayout = nil
            }
        }
        
        // Always update bubble insets
        if (isOut) {
            bindBubbleType(.TextOut, isCompact: isClanchBottom)
            dateText.textColor = appStyle.chatTextDateOutColor
            
            bubbleInsets = UIEdgeInsets(
                top: (isClanchTop ? AABubbleCell.bubbleTopCompact : AABubbleCell.bubbleTop),
                left: 0 + (AADevice.isiPad ? 16 : 0),
                bottom: (isClanchBottom ? AABubbleCell.bubbleBottomCompact : AABubbleCell.bubbleBottom),
                right: (isClanchBottom ? 10 : 4) + (AADevice.isiPad ? 16 : 0))
            contentInsets = UIEdgeInsets(
                top: AABubbleCell.bubbleContentTop,
                left: 10,
                bottom: AABubbleCell.bubbleContentBottom,
                right: (isClanchBottom ? 4 : 10))
        } else {
            bindBubbleType(.TextIn, isCompact: isClanchBottom)
            dateText.textColor = appStyle.chatTextDateInColor
            
            bubbleInsets = UIEdgeInsets(
                top: (isClanchTop ? AABubbleCell.bubbleTopCompact : AABubbleCell.bubbleTop),
                left: (isClanchBottom ? 10 : 4) + (AADevice.isiPad ? 16 : 0),
                bottom: (isClanchBottom ? AABubbleCell.bubbleBottomCompact : AABubbleCell.bubbleBottom),
                right: 0 + (AADevice.isiPad ? 16 : 0))
            contentInsets = UIEdgeInsets(
                top: (isGroup ? 18 : 0) + AABubbleCell.bubbleContentTop,
                left: (isClanchBottom ? 11 : 17),
                bottom: AABubbleCell.bubbleContentBottom,
                right: 10)
{'fraction_non_alphanumeric': 0.07478129205921938, 'fraction_numerical': 0.008916554508748318, 'mean_word_length': 2.1908212560386473, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '539543', 'n_tokens_mistral': 3429, 'n_tokens_neox': 3234, 'n_words': 714}