Added Android code
[wl-app.git] / iOS / WolneLektury / Extensions / UIViewController+Ext.swift
1 //
2 //  UIViewController+Ext.swift
3 //  WolneLektury
4 //
5 //  Created by Pawel Dabrowski on 30/05/2018.
6 //  Copyright © 2018 Fundacja Nowoczesna Polska. All rights reserved.
7 //
8
9 import Foundation
10 import UIKit
11 import MBProgressHUD
12
13 extension UIViewController
14 {
15     var syncManager:SyncManager{
16         return appDelegate.syncManager
17     }
18 }
19
20 extension UINavigationController{
21     open override var preferredStatusBarStyle : UIStatusBarStyle {
22         return .lightContent
23     }
24 }
25
26 extension UIViewController{
27     
28
29     static func createViewControllerFromStoryboard(storyboardName:String = "Login") -> UIViewController {
30         
31         let viewController:UIViewController = UIStoryboard(name:storyboardName, bundle: nil).instantiateViewController(withIdentifier: String(describing: self)) as UIViewController
32         return viewController
33     }
34     
35     class func instance() -> Self
36     {
37         return instantiateFromStoryboardHelper()
38     }
39     
40     private class func instantiateFromStoryboardHelper<T>() -> T
41     {
42         let storyboard = UIStoryboard(name: "Main", bundle: nil)
43         let controller = storyboard.instantiateViewController(withIdentifier: "\(self)") as! T
44         return controller
45     }
46 }
47
48 extension UIViewController{
49     
50     func presentAlertViewController(title:String,message:String, okAction:UIAlertAction?)
51     {
52         let alertController = UIAlertController(title:title, message:message, preferredStyle: .alert)
53         
54         var cancelTitle = "button_cancel".localized
55         if okAction == nil{
56             cancelTitle = "button_ok".localized
57         }
58         
59         let cancelAction = UIAlertAction(title: cancelTitle, style: .cancel, handler: nil)
60         alertController.addAction(cancelAction)
61         
62         if okAction != nil
63         {
64             alertController.addAction(okAction!)
65         }
66         self.present(alertController, animated: true, completion:nil)
67     }
68     
69     func presentProblemOccuredAlert(message: String){
70         presentAlertViewController(title: "problem_occurred".localized, message: message, okAction: nil)
71     }
72     
73     func presentToast(message: String) {
74         view.makeToast(message, duration: 3.0, position: .bottom)
75     }
76     
77     func share(string: String, button: UIButton) {
78         let items = [string]
79         let activity = UIActivityViewController(activityItems: items, applicationActivities: nil)
80         
81         // Pop style on iPad
82         if let actv = activity.popoverPresentationController {
83             actv.sourceView = button
84         }
85
86         self.present(activity, animated: true, completion: nil)
87     }
88     
89     func onBecomeFriendButtonTapped(){
90         guard Constants.donateEnabled else { return }
91
92         if syncManager.isLoggedIn() {
93             appDelegate.mainNavigator.presentPayPalForm()
94         }
95         else {
96             appDelegate.mainNavigator.presentLogin(push: true)
97         }
98     }
99 }
100
101 extension UIViewController{
102     func setIsPopup(){
103         providesPresentationContextTransitionStyle = true
104         definesPresentationContext = true
105         modalPresentationStyle = .overCurrentContext
106         modalTransitionStyle = .crossDissolve
107     }
108 }
109
110 extension UIViewController{
111     
112     func containerAdd(childViewController: UIViewController, toView: UIView){
113         
114         addChildViewController(childViewController)
115         childViewController.view.frame = toView.bounds
116         toView.addSubview(childViewController.view)
117         childViewController.view.translatesAutoresizingMaskIntoConstraints = false
118         childViewController.view.topAnchor.constraint(equalTo: toView.topAnchor).isActive = true
119         childViewController.view.leftAnchor.constraint(equalTo: toView.leftAnchor).isActive = true
120         childViewController.view.rightAnchor.constraint(equalTo: toView.rightAnchor).isActive = true
121         childViewController.view.bottomAnchor.constraint(equalTo: toView.bottomAnchor).isActive = true
122         childViewController.didMove(toParentViewController: self)
123     }
124     
125     func containerRemove(childViewController: UIViewController){
126         
127         childViewController.willMove(toParentViewController: nil)
128         childViewController.view.removeFromSuperview()
129         childViewController.removeFromParentViewController()
130     }
131 }