Guest User

Untitled

a guest
Nov 15th, 2016
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 17.44 KB | None | 0 0
  1. //
  2. //  SettingsViewController.swift
  3. //  myProject
  4. //
  5. //  Created by MAC on 04/03/16.
  6. //  Copyright © 2016 myProject.inc. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import Social
  11. import MessageUI
  12.  
  13.  
  14. class SettingTableViewController: UITableViewController , MFMailComposeViewControllerDelegate , MFMessageComposeViewControllerDelegate , UIAlertViewDelegate{
  15.    
  16.     @IBOutlet var userCollegeLbl: UILabel!
  17.     @IBOutlet var usernameLbl: UILabel!
  18.     @IBOutlet var userImageView: UIImageView!
  19.    
  20.     @IBOutlet var reportCell: UITableViewCell!
  21.    
  22.     var backendless = Backendless.sharedInstance()
  23.     var loggedUser = BackendlessUser()
  24.    
  25.     var tapGestureRecognizer = UITapGestureRecognizer()
  26.     var pickerData: [[Int]] = [[Int]]()
  27.     var pickerData1: [String] = [String]()
  28.    
  29.    
  30.     var emailSubjectString = ""
  31.    
  32.     var theCalendar =  Calendar.autoupdatingCurrent
  33.    
  34.    
  35.    
  36.     let appDelegate = UIApplication.shared.delegate as! AppDelegate
  37.    
  38.     var notification  = UILocalNotification()
  39.    
  40.     var dateComp : DateComponents = DateComponents()
  41.     var dateCompForPicker : DateComponents = DateComponents()
  42.     var dateCompForPicker2 : DateComponents = DateComponents()
  43.    
  44.     let mainSwitchNSUserDefault = UserDefaults.standard
  45.     let notificationSwitch = UserDefaults.standard
  46.     let fireDateDefault = UserDefaults.standard
  47.     let rescheduledFireDateDefault = UserDefaults.standard
  48.     let userDefaults = UserDefaults.standard
  49.    
  50.    
  51.     var  inviteTextBody =    ""
  52.    
  53.    
  54.     override func viewWillAppear(_ animated: Bool) {
  55.        
  56.     }
  57.    
  58.    
  59.     override func viewDidLoad() {
  60.         super.viewDidLoad()
  61.        
  62.        
  63.         let documentsURL  = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
  64.         let userImageURL = documentsURL.appendingPathComponent("DocumentsuserImage.png")
  65.         let userImage = UIImage(contentsOfFile: userImageURL.path)
  66.         if userImage == nil {
  67.            
  68.             if let  link =  (loggedUser.getProperty("userImageLink")){
  69.                
  70.                 if link as! String != "" {
  71.                    
  72.                     userImageView.sd_setImage(with: URL(string: loggedUser.getProperty("userImageLink") as! String)){
  73.                         [weak self]
  74.                         (image, error, _, _) -> Void in
  75.                         // Reveal image here
  76.                        
  77.                        
  78.                         if error == nil {
  79.                            
  80.                             self!.userImageView.image  = image
  81.                            
  82.                            
  83.                             if let data = UIImagePNGRepresentation(self!.userImageView.image!) {
  84.                                
  85.                                 let documentsURL  = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
  86.                                 let userImageURL = documentsURL.appendingPathComponent("DocumentsuserImage.png")
  87.                                 //  data.writeToFile(userImageURL, atomically: true)
  88.                                 try? data.write(to: userImageURL, options: [.atomic])
  89.                                
  90.                             }
  91.                            
  92.                         }
  93.                     }
  94.                 }
  95.                
  96.                
  97.             }else {
  98.                
  99.                 self.userImageView.image = UIImage(named: "DefaultUserImage")}
  100.            
  101.         }else {
  102.             self.userImageView.contentMode = .scaleAspectFill
  103.             self.userImageView.image = userImage }
  104.        
  105.         loggedUser = (backendless?.userService.currentUser)!
  106.        
  107.         usernameLbl.text = loggedUser.getProperty("username") as? String
  108.         userCollegeLbl.text = loggedUser.getProperty("college") as? String
  109.        
  110.         userImageView.layer.cornerRadius = userImageView.layer.frame.height / 2
  111.         userImageView.clipsToBounds = true
  112.        
  113.         self.userImageView.layer.borderColor = UIColor.lightGray.cgColor
  114.         self.userImageView.layer.borderWidth = 1
  115.        
  116.        
  117.         if let userDefaultUserType = userDefaults.object(forKey: "userType") as! String?{
  118.            
  119.             if userDefaultUserType == "Student"{
  120.                
  121.                
  122.                
  123.             }
  124.             else if userDefaultUserType == "Staff"{
  125.                
  126.             }
  127.            
  128.         }
  129.        
  130.        
  131.         self.tableView.backgroundColor = UIColor(red: 243.0/255, green: 243.0/255, blue: 243.0/255, alpha:1)
  132.        
  133.        
  134.         tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(showReportForm))
  135.         reportCell.addGestureRecognizer(tapGestureRecognizer)
  136.        
  137.        
  138.         if let userDefaultUserType = userDefaults.object(forKey: "userType") as! String?{
  139.            
  140.             if userDefaultUserType == "Staff"{
  141.                
  142.                
  143.                 emailSubjectString = "Great new tool "
  144.                 inviteTextBody =  "Hey! \n"
  145.                
  146.                
  147.             }
  148.             else {
  149.                
  150.                 emailSubjectString = "Great new tool "
  151.                
  152.                 inviteTextBody =  "Hey! \n"
  153.                
  154.             }
  155.            
  156.         }else {
  157.            
  158.             emailSubjectString = "Great new tool"
  159.            
  160.             inviteTextBody =  "Hey! \n"
  161.         }
  162.     }
  163.    
  164.     func showReportForm (){
  165.        
  166.        
  167.         // Email Subject
  168.         let emailTitle: String = "Feedback"
  169.         // Email Content
  170.         let messageBody: String = ""
  171.         // To address
  172.         let toRecipents: [String] = ["[email protected]"]
  173.         let mc: MFMailComposeViewController = MFMailComposeViewController()
  174.         mc.mailComposeDelegate = self
  175.         mc.setSubject(emailTitle)
  176.         mc.setMessageBody(messageBody, isHTML: false)
  177.         mc.setToRecipients(toRecipents)
  178.         // Present mail view controller on screen
  179.         self.present(mc, animated: true, completion: { _ in })
  180.        
  181.         if !MFMailComposeViewController.canSendMail() {
  182.             let alertView = UIAlertView(title: "Error", message: "Mail services are not available, Please try again later.", delegate: self, cancelButtonTitle: "OK")
  183.             alertView.show()
  184.             return
  185.            
  186.         }
  187.     }
  188.    
  189.    
  190.    
  191.    
  192.    
  193.    
  194.     func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
  195.         switch result {
  196.         case MFMailComposeResult.cancelled:
  197.             print("Mail cancelled")
  198.         case MFMailComposeResult.saved:
  199.             print("Mail saved")
  200.         case MFMailComposeResult.sent:
  201.             print("Mail sent")
  202.         case MFMailComposeResult.failed:
  203.             print("Mail sent failure: \(error!.localizedDescription)")
  204.         default:
  205.             break
  206.         }
  207.        
  208.         // Close the Mail Interface
  209.         self.dismiss(animated: true, completion: { _ in })
  210.     }
  211.    
  212.    
  213.     override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  214.        
  215.        
  216.         if (indexPath as NSIndexPath).row == 3 {
  217.            
  218.             //Create the AlertController
  219.             let actionSheetController: UIAlertController = UIAlertController(title: "", message: "Share with your friends.", preferredStyle: .actionSheet)
  220.            
  221.             //Create and add the Cancel action
  222.             let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .cancel) { action -> Void in
  223.                 //Just dismiss the action sheet
  224.             }
  225.             actionSheetController.addAction(cancelAction)
  226.             //Create and add first option action
  227.             let shareEmailAction: UIAlertAction = UIAlertAction(title: "Email", style: .default)
  228.             { action -> Void in
  229.                
  230.                 // Email Subject
  231.                 let emailTitle: String = self.emailSubjectString
  232.                 // Email Content
  233.                 let messageBody: String = self.inviteTextBody
  234.                 // To address
  235.                 let toRecipents: [String] = [""]
  236.                 let mc: MFMailComposeViewController = MFMailComposeViewController()
  237.                 mc.mailComposeDelegate = self
  238.                 mc.setSubject(emailTitle)
  239.                 mc.setMessageBody(messageBody, isHTML: false)
  240.                 mc.setToRecipients(toRecipents)
  241.                 // Present mail view controller on screen
  242.                 self.present(mc, animated: true, completion: { _ in })
  243.                
  244.                 if !MFMailComposeViewController.canSendMail() {
  245.                     let alertView = UIAlertView(title: "Error", message: "Mail services are not available, Please try again later.", delegate: self, cancelButtonTitle: "OK")
  246.                     alertView.show()
  247.                     return
  248.                    
  249.                 }
  250.                
  251.             }
  252.             actionSheetController.addAction(shareEmailAction)
  253.             //Create and add a second option action
  254.             let shareWhatsappAction: UIAlertAction = UIAlertAction(title: "Whatsapp", style: .default)
  255.             { action -> Void in
  256.                
  257.                
  258.                 let escapedString = self.inviteTextBody.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
  259.                
  260.                
  261.                
  262.                 let whatsappURL:URL? = URL(string: "whatsapp://send?text=\(escapedString!)")
  263.                
  264.                
  265.                 if (UIApplication.shared.canOpenURL(whatsappURL!)) {
  266.                    
  267.                     UIApplication.shared.openURL(whatsappURL!)
  268.                 }
  269.                
  270.             }
  271.             actionSheetController.addAction(shareWhatsappAction)
  272.            
  273.            
  274.             let shareWithFBAction: UIAlertAction = UIAlertAction(title: "Facebook", style: .default)
  275.             { action -> Void in
  276.                
  277.                 //self.performSegueWithIdentifier("segue_setup_provider", sender: self)
  278.                 if SLComposeViewController.isAvailable(forServiceType: SLServiceTypeFacebook){
  279.                     let facebookSheet:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
  280.                     facebookSheet.setInitialText(self.inviteTextBody)
  281.                     self.present(facebookSheet, animated: true, completion: nil)
  282.                 } else {
  283.                     let alert = UIAlertController(title: "Accounts", message: "Please login to a Facebook account to share.", preferredStyle: UIAlertControllerStyle.alert)
  284.                     alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
  285.                     self.present(alert, animated: true, completion: nil)
  286.                 }
  287.             }
  288.             actionSheetController.addAction(shareWithFBAction)
  289.            
  290.             let shareWithTwitterAction: UIAlertAction = UIAlertAction(title: "Twitter", style: .default)
  291.             { action -> Void in
  292.                
  293.                 //self.performSegueWithIdentifier("segue_setup_provider", sender: self)
  294.                
  295.                 if SLComposeViewController.isAvailable(forServiceType: SLServiceTypeTwitter){
  296.                     let twitterSheet:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
  297.                     twitterSheet.setInitialText(self.inviteTextBody)
  298.                     self.present(twitterSheet, animated: true, completion: nil)
  299.                 } else {
  300.                     let alert = UIAlertController(title: "Accounts", message: "Please login to a Twitter account to share.", preferredStyle: UIAlertControllerStyle.alert)
  301.                     alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
  302.                     self.present(alert, animated: true, completion: nil)
  303.                 }
  304.                
  305.             }
  306.             actionSheetController.addAction(shareWithTwitterAction)
  307.            
  308.            
  309.             let shareWithSMSAction: UIAlertAction = UIAlertAction(title: "SMS", style: .default)
  310.             { action -> Void in
  311.                
  312.                
  313.                 if (MFMessageComposeViewController.canSendText()) {
  314.                     let controller = MFMessageComposeViewController()
  315.                     controller.body = self.inviteTextBody
  316.                     controller.messageComposeDelegate = self
  317.                     self.present(controller, animated: true, completion: nil)
  318.                 }
  319.                 //self.performSegueWithIdentifier("segue_setup_provider", sender: self)
  320.                
  321.             }
  322.             actionSheetController.addAction(shareWithSMSAction)
  323.            
  324.             let cell = self.tableView(tableView, cellForRowAt: indexPath)
  325.             //We need to provide a popover sourceView when using it on iPad
  326.             actionSheetController.popoverPresentationController?.sourceView = cell as UIView
  327.            
  328.             //Present the AlertController
  329.             self.present(actionSheetController, animated: true, completion: nil)
  330.         }
  331.        
  332.        
  333.        
  334.        
  335.     }
  336.    
  337.     func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
  338.         //... handle sms screen actions
  339.         self.dismiss(animated: true, completion: nil)
  340.     }
  341.    
  342.    
  343.    
  344.     override func viewDidAppear(_ animated: Bool) {
  345.        
  346.         usernameLbl.text = loggedUser.getProperty("username") as? String
  347.         userCollegeLbl.text = loggedUser.getProperty("college") as? String
  348.        
  349.         let documentsURL  = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
  350.         let userImageURL = documentsURL.appendingPathComponent("DocumentsuserImage.png")
  351.         let userImage = UIImage(contentsOfFile: userImageURL.path)
  352.         if userImage == nil {
  353.            
  354.            
  355.            
  356.             if let  link =  (loggedUser.getProperty("profile_image_link")){
  357.                
  358.                 if link as! String != "" {
  359.                    
  360.                     let darkView  = UIView()
  361.                     darkView.frame = userImageView.bounds
  362.                    
  363.                     darkView.backgroundColor = UIColor.black
  364.                     darkView.alpha = 0.75
  365.                    
  366.                    
  367.                    
  368.                     var activityIndicator = UIActivityIndicatorView()
  369.                     activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .white)
  370.                     activityIndicator.hidesWhenStopped = true
  371.                     activityIndicator.startAnimating()
  372.                     activityIndicator.center = userImageView.center
  373.                     activityIndicator.center.y = userImageView.center.y - 8
  374.                     activityIndicator.center.x = userImageView.center.x - 8
  375.                    
  376.                     darkView.addSubview(activityIndicator)
  377.                     userImageView.addSubview(darkView)
  378.                    
  379.                    
  380.                     userImageView.sd_setImage( with: URL(string: loggedUser.getProperty("profile_image_link") as! String)){
  381.                         [weak self]
  382.                         (image, error, _, _) -> Void in
  383.                         // Reveal image here
  384.                        
  385.                         if error == nil {
  386.                             self!.userImageView.image  = image
  387.                            
  388.                             if let data = UIImagePNGRepresentation(self!.userImageView.image!) {
  389.                                
  390.                                 let documentsURL  = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
  391.                                 let userImageURL = documentsURL.appendingPathComponent("DocumentsuserImage.png")
  392.                                 try? data.write(to: userImageURL, options: [.atomic])
  393.                                
  394.                                
  395.                                 activityIndicator.stopAnimating()
  396.                                 darkView.removeFromSuperview()
  397.                             }
  398.                            
  399.                         }else {
  400.                            
  401.                             activityIndicator.stopAnimating()
  402.                             darkView.removeFromSuperview()
  403.                         }
  404.                        
  405.                        
  406.                     }
  407.                 }
  408.             }
  409.             else {
  410.                
  411.                 self.userImageView.image = UIImage(named: "DefaultUserImage")}
  412.            
  413.         }else {
  414.             self.userImageView.contentMode = .scaleAspectFill
  415.             self.userImageView.image = userImage }
  416.        
  417.     }
  418.    
  419.     override func didReceiveMemoryWarning() {
  420.         super.didReceiveMemoryWarning()
  421.         // Dispose of any resources that can be recreated.
  422.     }
  423.    
  424.    
  425.    
  426.    
  427.     override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  428.        
  429.         let indexPath1: IndexPath = IndexPath(row: 0, section: 0)
  430.        
  431.        
  432.         if indexPath ==  indexPath1  {
  433.            
  434.             return 90
  435.         }
  436.        
  437.         return 50
  438.     }
  439.    
  440.    
  441.    
  442.    
  443.    
  444. }
Add Comment
Please, Sign In to add comment