<commit_before>//
//  ViewController.swift
//  Rocket.Chat.iOS
//
//  Created by <NAME> on 8/6/15.
//  Copyright © 2015 Rocket.Chat. All rights reserved.
//

import UIKit
import MMDrawerController

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    
    @IBOutlet var bottomViewBottomConstraint: NSLayoutConstraint!
    @IBOutlet var tableViewTopConstraint: NSLayoutConstraint!
    
    
    //Variable to keep the logged in user
    var currentUser = User?()
    
    //Array to keep dummy messages
    var mArray1:[Message] = []
    

    @IBOutlet var mainTableview: UITableView!
    @IBOutlet var composeMsg: UITextView!
    
    // Variable to access the dummy chatroom
    var cR1:ChatRoom?
    
    // indexPath to find the bottom of the tableview
    var bottomIndexPath:NSIndexPath = NSIndexPath()
    
    
    var dateFormatter = NSDateFormatter()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Notifications to manage keyboard
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWasShown:"), name:UIKeyboardWillShowNotification, object: nil);
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil);
        
        // Create tap gesture
        let tapGesture = UITapGestureRecognizer(target: self, action: Selector("dismissKeyboard"))
        tapGesture.cancelsTouchesInView = true
        
        //Create double tap gesture
        let doubleTapGesture = UITapGestureRecognizer(target: self, action: Selector("showHiddenTimestamp:"))
        doubleTapGesture.numberOfTapsRequired = 2
        
        //Add gestures on tableview
        mainTableview.addGestureRecognizer(tapGesture)
        mainTableview.addGestureRecognizer(doubleTapGesture)
        
        
        /********* Dummy data *********/
        let delegate = UIApplication.sharedApplication().delegate as! AppDelegate
        let context = delegate.stack!.context

        
        //Create user u1
        let u1:User = User(context: context, id: "1", username: "Komic", avatar: UIImage(named: "Default-Avatar")!, status: User.Status.ONLINE, timezone: NSTimeZone.systemTimeZone())
        
        //Create some messages for u1
        let msg1:Message = Message(id: "1", text: "Message 1 from Komic", tstamp: NSDate(timeInterval: randomTime(), sinceDate: NSDate()), user: u1)
        let msg2:Message = Message(id: "2", text: "Bigger Message, number 2 from Komic just for fun", tstamp: NSDate(timeInterval: randomTime(), sinceDate: NSDate()), user: u1)
        let msg3:Message = Message(id: "3", text: "Message 3 from Komic just to see what's up with the cell height", tstamp: NSDate(timeInterval: randomTime(), sinceDate: NSDate()), user: u1)
        let msg4:Message = Message(id: "4", text: "Message 4 from Komic, a little smaller", tstamp: NSDate(timeInterval: randomTime(), sinceDate: NSDate()), user: u1)
        let msg5:Message = Message(id: "5", text: "Message 5 from Komic is message 5", tstamp: NSDate(timeInterval: randomTime(), sinceDate: NSDate()), user: u1)
    
        
        //Create user u2
        let u2:User = User(context: context, id: "2", username: "Yorgos", avatar: UIImage(named: "Default-Avatar")!, status: User.Status.ONLINE, timezone: NSTimeZone.systemTimeZone())
        
        //Create some messages for u2
        let msg6:Message = Message(id: "6", text: "Message 6 from Yorgos", tstamp: NSDate(timeInterval: randomTime(), sinceDate: NSDate()), user: u2)
        let msg7:Message = Message(id: "7", text: "Message 7 from Yorgos", tstamp: NSDate(timeInterval: randomTime(), sinceDate: NSDate()), user: u2)
        let msg8:Message = Message(id: "8", text: "Message 8 from Yorgos", tstamp: NSDate(timeInterval: randomTime(), sinceDate: NSDate()), user: u2)
        let msg9:Message = Message(id: "9", text: "Message 9 from Yorgos", tstamp: NSDate(timeInterval: randomTime(), sinceDate: NSDate()), user: u2)
        let msg10:Message = Message(id: "10", text: "Message 10 from Yorgos", tstamp: NSDate(timeInterval: randomTime(), sinceDate: NSDate()), user: u2)
        let msg11:Message = Message(id: "11", text: "Message 11 from Yorgos", tstamp: NSDate(timeInterval: randomTime(), sinceDate: NSDate()), user: u2)
        
        
        //Create user u3
        let u3:User = User(context: context, id: "3", username: "GeorgeP", avatar: UIImage(named: "Default-Avatar")!, status: User.Status.ONLINE, timezone: NSTimeZone.systemTimeZone())
        
        //Create some messages for u3
        let msg12:Message = Message(id: "12", text: "Message 12 from GeorgeP", tstamp: NSDate(timeInterval: randomTime(), sinceDate: NSDate()), user: u3)
        let msg13:Message = Message(id: "13", text: "Message 13 from GeorgeP", tstamp: NSDate(timeInterval: randomTime(), sinceDate: NSDate()), user: u3)
        let msg14:Message = Message(id: "14", text: "Message 14 from GeorgeP", tstamp: NSDate(timeInterval: randomTime(), sinceDate: NSDate()), user: u3)
        let msg15:Message = Message(id: "15", text: "Message 15 from GeorgeP", tstamp: NSDate(timeInterval: randomTime(), sinceDate: NSDate()), user: u3)
        let msg16:Message = Message(id: "16", text: "Message 16 from GeorgeP", tstamp: NSDate(timeInterval: randomTime(), sinceDate: NSDate()), user: u3)
        let msg17:Message = Message(id: "17", text: "Message 17 from GeorgeP", tstamp: NSDate(timeInterval: randomTime(), sinceDate: NSDate()), user: u3)
        let msg18:Message = Message(id: "18", text: "Message 18 from GeorgeP", tstamp: NSDate(timeInterval: randomTime(), sinceDate: NSDate()), user: u3)
        let msg19:Message = Message(id: "19", text: "Message 19 from GeorgeP", tstamp: NSDate(timeInterval: randomTime(), sinceDate: NSDate()), user: u3)
        let msg20:Message = Message(id: "20", text: "Message 20 from GeorgeP", tstamp: NSDate(timeInterval: randomTime(), sinceDate: NSDate()), user: u3)
        
        //Create set of users
        let uSet1:Set<User> = [u1, u2, u3]
        
        
    
        //Inserting messages in an array (DUMB way)
        mArray1 = [msg1, msg2, msg3, msg4, msg5, msg6, msg7, msg8, msg9, msg10, msg11, msg12, msg13, msg14, msg15, msg16, msg17, msg18, msg19, msg20]
        
       
        
        //BubbleSort to keep messages in order by tstamps
        let nElements = mArray1.count
        
        var didSwap = true
        
        while didSwap {
            didSwap = false
            
            for i in 0..<nElements - 1 {
                if (mArray1[i].tstamp.compare(mArray1[i+1].tstamp) == NSComparisonResult.OrderedDescending)  {
                    let tmp = mArray1[i]
                    mArray1[i] = mArray1[i+1]
                    mArray1[i+1] = tmp
                    didSwap = true
                }
            }
        }
        
        
          //Logs
//        for i in mArray1 {
//            
//            print(i.text + " " + "\(i.user.username)")
//            
//        }
        
        
        
        //Create chatroom 1
        let c1:ChatRoom = ChatRoom(id: "1", name: "c1", type: .PUBLIC, users: uSet1, messages: mArray1)
        
        
        //store the created chatroom c1 into global variable cR1
        cR1 = c1
        
        /********* End of Dummy data *********/
        
        
        //Remove lines between cells
        mainTableview.separatorStyle = UITableViewCellSeparatorStyle.None
        
        mainTableview.rowHeight = UITableViewAutomaticDimension
        mainTableview.estimatedRowHeight = 75
        
        
        
        //Set bottomIndexpath to last cell's index
        bottomIndexPath = NSIndexPath(forRow: cR1!.messages.count-1, inSection: 0)
        mainTableview.scrollToRowAtIndexPath(bottomIndexPath, atScrollPosition: UITableViewScrollPosition.Bottom, animated: true)
        
        //fix for tableview not scrolling all the way to the bottom in iOS 9
        mainTableview.reloadData()
        
        
        //Set border to composeMsg textarea
        composeMsg.layer.borderColor = UIColor.blackColor().CGColor
        composeMsg.layer.borderWidth = 0.5
        composeMsg.layer.cornerRadius = 10
        
        dateFormatter.dateFormat = "HH:mm"
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    @IBAction func leftNavTapped(sender: AnyObject) {
        
        //get the appDelegate
        let appdelegate:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        
        //Toggle the drawer to the left
        appdelegate.centerContainer?.toggleDrawerSide(MMDrawerSide.Left, animated: true, completion: nil)
        
    }<commit_msg>Added fake sessionTokens to dummy data<commit_after>//
//  ViewController.swift
//  Rocket.Chat.iOS
//
//  Created by <NAME> on 8/6/15.
//  Copyright © 2015 Rocket.Chat. All rights reserved.
//

import UIKit
import MMDrawerController

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    
    @IBOutlet var bottomViewBottomConstraint: NSLayoutConstraint!
    @IBOutlet var tableViewTopConstraint: NSLayoutConstraint!
    
    
    //Variable to keep the logged in user
    var currentUser = User?()
    
    //Array to keep dummy messages
    var mArray1:[Message] = []
    

    @IBOutlet var mainTableview: UITableView!
    @IBOutlet var composeMsg: UITextView!
    
    // Variable to access the dummy chatroom
    var cR1:ChatRoom?
    
    // indexPath to find the bottom of the tableview
    var bottomIndexPath:NSIndexPath = NSIndexPath()
    
    
    var dateFormatter = NSDateFormatter()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Notifications to manage keyboard
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWasShown:"), name:UIKeyboardWillShowNotification, object: nil);
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil);
        
        // Create tap gesture
        let tapGesture = UITapGestureRecognizer(target: self, action: Selector("dismissKeyboard"))
        tapGesture.cancelsTouchesInView = true
        
        //Create double tap gesture
        let doubleTapGesture = UITapGestureRecognizer(target: self, action: Selector("showHiddenTimestamp:"))
        doubleTapGesture.numberOfTapsRequired = 2
        
        //Add gestures on tableview
        mainTableview.addGestureRecognizer(tapGesture)
        mainTableview.addGestureRecognizer(doubleTapGesture)
        
        
        /********* Dummy data *********/
        let delegate = UIApplication.sharedApplication().delegate as! AppDelegate
        let context = delegate.stack!.context

        
        //Create user u1
      let u1:User = User(context: context, id: "1", username: "Komic", sessionToken: "<PASSWORD>", avatar: UIImage(named: "Default-Avatar")!, status: User.Status.ONLINE, timezone: NSTimeZone.systemTimeZone())
        
        //Create some messages for u1
        let msg1:Message = Message(id: "1", text: "Message 1 from Komic", tstamp: NSDate(timeInterval: randomTime(), sinceDate: NSDate()), user: u1)
        let msg2:Message = Message(id: "2", text: "Bigger Message, number 2 from Komic just for fun", tstamp: NSDate(timeInterval: randomTime(), sinceDate: NSDate()), user: u1)
        let msg3:Message = Message(id: "3", text: "Message 3 from Komic just to see what's up with the cell height", tstamp: NSDate(timeInterval: randomTime(), sinceDate: NSDate()), user: u1)
        let msg4:Message = Message(id: "4", text: "Message 4 from Komic, a little smaller", tstamp: NSDate(timeInterval: randomTime(), sinceDate: NSDate()), user: u1)
        let msg5:Message = Message(id: "5", text: "Message 5 from Komic is message 5", tstamp: NSDate(timeInterval: randomTime(), sinceDate: NSDate()), user: u1)
    
        
        //Create user u2
        let u2:User = User(context: context, id: "2", username: "Yorgos", sessionToken: "<PASSWORD>", avatar: UIImage(named: "Default-Avatar")!, status: User.Status.ONLINE, timezone: NSTimeZone.systemTimeZone())
        
        //Create some messages for u2
        let msg6:Message = Message(id: "6", text: "Message 6 from Yorgos", tstamp: NSDate(timeInterval: randomTime(), sinceDate: NSDate()), user: u2)
        let msg7:Message = Message(id: "7", text: "Message 7 from Yorgos", tstamp: NSDate(timeInterval: randomTime(), sinceDate: NSDate()), user: u2)
        let msg8:Message = Message(id: "8", text: "Message 8 from Yorgos", tstamp: NSDate(timeInterval: randomTime(), sinceDate: NSDate()), user: u2)
        let msg9:Message = Message(id: "9", text: "Message 9 from Yorgos", tstamp: NSDate(timeInterval: randomTime(), sinceDate: NSDate()), user: u2)
        let msg10:Message = Message(id: "10", text: "Message 10 from Yorgos", tstamp: NSDate(timeInterval: randomTime(), sinceDate: NSDate()), user: u2)
        let msg11:Message = Message(id: "11", text: "Message 11 from Yorgos", tstamp: NSDate(timeInterval: randomTime(), sinceDate: NSDate()), user: u2)
        
        
        //Create user u3
        let u3:User = User(context: context, id: "3", username: "GeorgeP", sessionToken: "789", avatar: UIImage(named: "Default-Avatar")!, status: User.Status.ONLINE, timezone: NSTimeZone.systemTimeZone())
        
        //Create some messages for u3
        let msg12:Message = Message(id: "12", text: "Message 12 from GeorgeP", tstamp: NSDate(timeInterval: randomTime(), sinceDate: NSDate()), user: u3)
        let msg13:Message = Message(id: "13", text: "Message 13 from GeorgeP", tstamp: NSDate(timeInterval: randomTime(), sinceDate: NSDate()), user: u3)
        let msg14:Message = Message(id: "14", text: "Message 14 from GeorgeP", tstamp: NSDate(timeInterval: randomTime(), sinceDate: NSDate()), user: u3)
        let msg15:Message = Message(id: "15", text: "Message 15 from GeorgeP", tstamp: NSDate(timeInterval: randomTime(), sinceDate: NSDate()), user: u3)
        let msg16:Message = Message(id: "16", text: "Message 16 from GeorgeP", tstamp: NSDate(timeInterval: randomTime(), sinceDate: NSDate()), user: u3)
        let msg17:Message = Message(id: "17", text: "Message 17 from GeorgeP", tstamp: NSDate(timeInterval: randomTime(), sinceDate: NSDate()), user: u3)
        let msg18:Message = Message(id: "18", text: "Message 18 from GeorgeP", tstamp: NSDate(timeInterval: randomTime(), sinceDate: NSDate()), user: u3)
        let msg19:Message = Message(id: "19", text: "Message 19 from GeorgeP", tstamp: NSDate(timeInterval: randomTime(), sinceDate: NSDate()), user: u3)
        let msg20:Message = Message(id: "20", text: "Message 20 from GeorgeP", tstamp: NSDate(timeInterval: randomTime(), sinceDate: NSDate()), user: u3)
        
        //Create set of users
        let uSet1:Set<User> = [u1, u2, u3]
        
        
    
        //Inserting messages in an array (DUMB way)
        mArray1 = [msg1, msg2, msg3, msg4, msg5, msg6, msg7, msg8, msg9, msg10, msg11, msg12, msg13, msg14, msg15, msg16, msg17, msg18, msg19, msg20]
        
       
        
        //BubbleSort to keep messages in order by tstamps
        let nElements = mArray1.count
        
        var didSwap = true
        
        while didSwap {
            didSwap = false
            
            for i in 0..<nElements - 1 {
                if (mArray1[i].tstamp.compare(mArray1[i+1].tstamp) == NSComparisonResult.OrderedDescending)  {
                    let tmp = mArray1[i]
                    mArray1[i] = mArray1[i+1]
                    mArray1[i+1] = tmp
                    didSwap = true
                }
            }
        }
        
        
          //Logs
//        for i in mArray1 {
//            
//            print(i.text + " " + "\(i.user.username)")
//            
//        }
        
        
        
        //Create chatroom 1
        let c1:ChatRoom = ChatRoom(id: "1", name: "c1", type: .PUBLIC, users: uSet1, messages: mArray1)
        
        
        //store the created chatroom c1 into global variable cR1
        cR1 = c1

        
        /********* End of Dummy data *********/
        
        
        //Remove lines between cells
        mainTableview.separatorStyle = UITableViewCellSeparatorStyle.None
        
        mainTableview.rowHeight = UITableViewAutomaticDimension
        mainTableview.estimatedRowHeight = 75
        
        
        
        //Set bottomIndexpath to last cell's index
        bottomIndexPath = NSIndexPath(forRow: cR1!.messages.count-1, inSection: 0)
        mainTableview.scrollToRowAtIndexPath(bottomIndexPath, atScrollPosition: UITableViewScrollPosition.Bottom, animated: true)
      
        //fix for tableview not scrolling all the way to the bottom in iOS 9
        mainTableview.reloadData()
        
        
        //Set border to composeMsg textarea
        composeMsg.layer.borderColor = UIColor.blackColor().CGColor
        composeMsg.layer.borderWidth = 0.5
        composeMsg.layer.cornerRadius = 10
        
        dateFormatter.dateFormat = "HH:mm"
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    @IBAction func leftNavTapped(sender: AnyObject) {
        
        //get the appDelegate
        let appdelegate:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        
        //Toggle the drawer to the left
        appdelegate.centerContainer?.toggleDrawerSide(MMDrawerSide.Left, animated: true, completion: nil)
        
    }
{'fraction_non_alphanumeric': 0.11507156890261017, 'fraction_numerical': 0.02329497614369913, 'mean_word_length': 2.7295373665480427, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, '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': '3626852', 'n_tokens_mistral': 5516, 'n_tokens_neox': 5311, 'n_words': 1501}