Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // SettingsViewController.swift
- // myProject
- //
- // Created by MAC on 04/03/16.
- // Copyright © 2016 myProject.inc. All rights reserved.
- //
- import UIKit
- import Social
- import MessageUI
- class SettingTableViewController: UITableViewController , MFMailComposeViewControllerDelegate , MFMessageComposeViewControllerDelegate , UIAlertViewDelegate{
- @IBOutlet var userCollegeLbl: UILabel!
- @IBOutlet var usernameLbl: UILabel!
- @IBOutlet var userImageView: UIImageView!
- @IBOutlet var reportCell: UITableViewCell!
- var backendless = Backendless.sharedInstance()
- var loggedUser = BackendlessUser()
- var tapGestureRecognizer = UITapGestureRecognizer()
- var pickerData: [[Int]] = [[Int]]()
- var pickerData1: [String] = [String]()
- var emailSubjectString = ""
- var theCalendar = Calendar.autoupdatingCurrent
- let appDelegate = UIApplication.shared.delegate as! AppDelegate
- var notification = UILocalNotification()
- var dateComp : DateComponents = DateComponents()
- var dateCompForPicker : DateComponents = DateComponents()
- var dateCompForPicker2 : DateComponents = DateComponents()
- let mainSwitchNSUserDefault = UserDefaults.standard
- let notificationSwitch = UserDefaults.standard
- let fireDateDefault = UserDefaults.standard
- let rescheduledFireDateDefault = UserDefaults.standard
- let userDefaults = UserDefaults.standard
- var inviteTextBody = ""
- override func viewWillAppear(_ animated: Bool) {
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
- let userImageURL = documentsURL.appendingPathComponent("DocumentsuserImage.png")
- let userImage = UIImage(contentsOfFile: userImageURL.path)
- if userImage == nil {
- if let link = (loggedUser.getProperty("userImageLink")){
- if link as! String != "" {
- userImageView.sd_setImage(with: URL(string: loggedUser.getProperty("userImageLink") as! String)){
- [weak self]
- (image, error, _, _) -> Void in
- // Reveal image here
- if error == nil {
- self!.userImageView.image = image
- if let data = UIImagePNGRepresentation(self!.userImageView.image!) {
- let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
- let userImageURL = documentsURL.appendingPathComponent("DocumentsuserImage.png")
- // data.writeToFile(userImageURL, atomically: true)
- try? data.write(to: userImageURL, options: [.atomic])
- }
- }
- }
- }
- }else {
- self.userImageView.image = UIImage(named: "DefaultUserImage")}
- }else {
- self.userImageView.contentMode = .scaleAspectFill
- self.userImageView.image = userImage }
- loggedUser = (backendless?.userService.currentUser)!
- usernameLbl.text = loggedUser.getProperty("username") as? String
- userCollegeLbl.text = loggedUser.getProperty("college") as? String
- userImageView.layer.cornerRadius = userImageView.layer.frame.height / 2
- userImageView.clipsToBounds = true
- self.userImageView.layer.borderColor = UIColor.lightGray.cgColor
- self.userImageView.layer.borderWidth = 1
- if let userDefaultUserType = userDefaults.object(forKey: "userType") as! String?{
- if userDefaultUserType == "Student"{
- }
- else if userDefaultUserType == "Staff"{
- }
- }
- self.tableView.backgroundColor = UIColor(red: 243.0/255, green: 243.0/255, blue: 243.0/255, alpha:1)
- tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(showReportForm))
- reportCell.addGestureRecognizer(tapGestureRecognizer)
- if let userDefaultUserType = userDefaults.object(forKey: "userType") as! String?{
- if userDefaultUserType == "Staff"{
- emailSubjectString = "Great new tool "
- inviteTextBody = "Hey! \n"
- }
- else {
- emailSubjectString = "Great new tool "
- inviteTextBody = "Hey! \n"
- }
- }else {
- emailSubjectString = "Great new tool"
- inviteTextBody = "Hey! \n"
- }
- }
- func showReportForm (){
- // Email Subject
- let emailTitle: String = "Feedback"
- // Email Content
- let messageBody: String = ""
- // To address
- let mc: MFMailComposeViewController = MFMailComposeViewController()
- mc.mailComposeDelegate = self
- mc.setSubject(emailTitle)
- mc.setMessageBody(messageBody, isHTML: false)
- mc.setToRecipients(toRecipents)
- // Present mail view controller on screen
- self.present(mc, animated: true, completion: { _ in })
- if !MFMailComposeViewController.canSendMail() {
- let alertView = UIAlertView(title: "Error", message: "Mail services are not available, Please try again later.", delegate: self, cancelButtonTitle: "OK")
- alertView.show()
- return
- }
- }
- func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
- switch result {
- case MFMailComposeResult.cancelled:
- print("Mail cancelled")
- case MFMailComposeResult.saved:
- print("Mail saved")
- case MFMailComposeResult.sent:
- print("Mail sent")
- case MFMailComposeResult.failed:
- print("Mail sent failure: \(error!.localizedDescription)")
- default:
- break
- }
- // Close the Mail Interface
- self.dismiss(animated: true, completion: { _ in })
- }
- override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
- if (indexPath as NSIndexPath).row == 3 {
- //Create the AlertController
- let actionSheetController: UIAlertController = UIAlertController(title: "", message: "Share with your friends.", preferredStyle: .actionSheet)
- //Create and add the Cancel action
- let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .cancel) { action -> Void in
- //Just dismiss the action sheet
- }
- actionSheetController.addAction(cancelAction)
- //Create and add first option action
- let shareEmailAction: UIAlertAction = UIAlertAction(title: "Email", style: .default)
- { action -> Void in
- // Email Subject
- let emailTitle: String = self.emailSubjectString
- // Email Content
- let messageBody: String = self.inviteTextBody
- // To address
- let toRecipents: [String] = [""]
- let mc: MFMailComposeViewController = MFMailComposeViewController()
- mc.mailComposeDelegate = self
- mc.setSubject(emailTitle)
- mc.setMessageBody(messageBody, isHTML: false)
- mc.setToRecipients(toRecipents)
- // Present mail view controller on screen
- self.present(mc, animated: true, completion: { _ in })
- if !MFMailComposeViewController.canSendMail() {
- let alertView = UIAlertView(title: "Error", message: "Mail services are not available, Please try again later.", delegate: self, cancelButtonTitle: "OK")
- alertView.show()
- return
- }
- }
- actionSheetController.addAction(shareEmailAction)
- //Create and add a second option action
- let shareWhatsappAction: UIAlertAction = UIAlertAction(title: "Whatsapp", style: .default)
- { action -> Void in
- let escapedString = self.inviteTextBody.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
- let whatsappURL:URL? = URL(string: "whatsapp://send?text=\(escapedString!)")
- if (UIApplication.shared.canOpenURL(whatsappURL!)) {
- UIApplication.shared.openURL(whatsappURL!)
- }
- }
- actionSheetController.addAction(shareWhatsappAction)
- let shareWithFBAction: UIAlertAction = UIAlertAction(title: "Facebook", style: .default)
- { action -> Void in
- //self.performSegueWithIdentifier("segue_setup_provider", sender: self)
- if SLComposeViewController.isAvailable(forServiceType: SLServiceTypeFacebook){
- let facebookSheet:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
- facebookSheet.setInitialText(self.inviteTextBody)
- self.present(facebookSheet, animated: true, completion: nil)
- } else {
- let alert = UIAlertController(title: "Accounts", message: "Please login to a Facebook account to share.", preferredStyle: UIAlertControllerStyle.alert)
- alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
- self.present(alert, animated: true, completion: nil)
- }
- }
- actionSheetController.addAction(shareWithFBAction)
- let shareWithTwitterAction: UIAlertAction = UIAlertAction(title: "Twitter", style: .default)
- { action -> Void in
- //self.performSegueWithIdentifier("segue_setup_provider", sender: self)
- if SLComposeViewController.isAvailable(forServiceType: SLServiceTypeTwitter){
- let twitterSheet:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
- twitterSheet.setInitialText(self.inviteTextBody)
- self.present(twitterSheet, animated: true, completion: nil)
- } else {
- let alert = UIAlertController(title: "Accounts", message: "Please login to a Twitter account to share.", preferredStyle: UIAlertControllerStyle.alert)
- alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
- self.present(alert, animated: true, completion: nil)
- }
- }
- actionSheetController.addAction(shareWithTwitterAction)
- let shareWithSMSAction: UIAlertAction = UIAlertAction(title: "SMS", style: .default)
- { action -> Void in
- if (MFMessageComposeViewController.canSendText()) {
- let controller = MFMessageComposeViewController()
- controller.body = self.inviteTextBody
- controller.messageComposeDelegate = self
- self.present(controller, animated: true, completion: nil)
- }
- //self.performSegueWithIdentifier("segue_setup_provider", sender: self)
- }
- actionSheetController.addAction(shareWithSMSAction)
- let cell = self.tableView(tableView, cellForRowAt: indexPath)
- //We need to provide a popover sourceView when using it on iPad
- actionSheetController.popoverPresentationController?.sourceView = cell as UIView
- //Present the AlertController
- self.present(actionSheetController, animated: true, completion: nil)
- }
- }
- func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
- //... handle sms screen actions
- self.dismiss(animated: true, completion: nil)
- }
- override func viewDidAppear(_ animated: Bool) {
- usernameLbl.text = loggedUser.getProperty("username") as? String
- userCollegeLbl.text = loggedUser.getProperty("college") as? String
- let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
- let userImageURL = documentsURL.appendingPathComponent("DocumentsuserImage.png")
- let userImage = UIImage(contentsOfFile: userImageURL.path)
- if userImage == nil {
- if let link = (loggedUser.getProperty("profile_image_link")){
- if link as! String != "" {
- let darkView = UIView()
- darkView.frame = userImageView.bounds
- darkView.backgroundColor = UIColor.black
- darkView.alpha = 0.75
- var activityIndicator = UIActivityIndicatorView()
- activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .white)
- activityIndicator.hidesWhenStopped = true
- activityIndicator.startAnimating()
- activityIndicator.center = userImageView.center
- activityIndicator.center.y = userImageView.center.y - 8
- activityIndicator.center.x = userImageView.center.x - 8
- darkView.addSubview(activityIndicator)
- userImageView.addSubview(darkView)
- userImageView.sd_setImage( with: URL(string: loggedUser.getProperty("profile_image_link") as! String)){
- [weak self]
- (image, error, _, _) -> Void in
- // Reveal image here
- if error == nil {
- self!.userImageView.image = image
- if let data = UIImagePNGRepresentation(self!.userImageView.image!) {
- let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
- let userImageURL = documentsURL.appendingPathComponent("DocumentsuserImage.png")
- try? data.write(to: userImageURL, options: [.atomic])
- activityIndicator.stopAnimating()
- darkView.removeFromSuperview()
- }
- }else {
- activityIndicator.stopAnimating()
- darkView.removeFromSuperview()
- }
- }
- }
- }
- else {
- self.userImageView.image = UIImage(named: "DefaultUserImage")}
- }else {
- self.userImageView.contentMode = .scaleAspectFill
- self.userImageView.image = userImage }
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- // Dispose of any resources that can be recreated.
- }
- override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
- let indexPath1: IndexPath = IndexPath(row: 0, section: 0)
- if indexPath == indexPath1 {
- return 90
- }
- return 50
- }
- }
Add Comment
Please, Sign In to add comment