added iOS source code
[wl-app.git] / iOS / Pods / FolioReaderKit / Source / PageViewController.swift
1 //
2 //  PageViewController.swift
3 //  FolioReaderKit
4 //
5 //  Created by Heberti Almeida on 14/07/16.
6 //  Copyright © 2016 FolioReader. All rights reserved.
7 //
8
9 import UIKit
10
11 class PageViewController: UIPageViewController {
12
13     var segmentedControl: UISegmentedControl!
14     var viewList = [UIViewController]()
15     var segmentedControlItems = [String]()
16     var viewControllerOne: UIViewController!
17     var viewControllerTwo: UIViewController!
18     var index: Int
19     fileprivate var readerConfig: FolioReaderConfig
20     fileprivate var folioReader: FolioReader
21
22     // MARK: Init
23
24     init(folioReader: FolioReader, readerConfig: FolioReaderConfig) {
25         self.folioReader = folioReader
26         self.readerConfig = readerConfig
27         self.index = self.folioReader.currentMenuIndex
28         super.init(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
29
30         self.edgesForExtendedLayout = UIRectEdge()
31         self.extendedLayoutIncludesOpaqueBars = true
32     }
33
34     required init?(coder: NSCoder) {
35         fatalError("storyboards are incompatible with truth and beauty")
36     }
37
38     override func viewDidLoad() {
39         super.viewDidLoad()
40
41         segmentedControl = UISegmentedControl(items: segmentedControlItems)
42         segmentedControl.addTarget(self, action: #selector(PageViewController.didSwitchMenu(_:)), for: UIControlEvents.valueChanged)
43         segmentedControl.selectedSegmentIndex = index
44         segmentedControl.setWidth(100, forSegmentAt: 0)
45         segmentedControl.setWidth(100, forSegmentAt: 1)
46         self.navigationItem.titleView = segmentedControl
47
48         viewList = [viewControllerOne, viewControllerTwo]
49
50         viewControllerOne.didMove(toParentViewController: self)
51         viewControllerTwo.didMove(toParentViewController: self)
52
53         self.delegate = self
54         self.dataSource = self
55
56         self.view.backgroundColor = UIColor.white
57         self.setViewControllers([viewList[index]], direction: .forward, animated: false, completion: nil)
58
59         // FIXME: This disable scroll because of highlight swipe to delete, if you can fix this would be awesome
60         for view in self.view.subviews {
61             if view is UIScrollView {
62                 let scroll = view as! UIScrollView
63                 scroll.bounces = false
64             }
65         }
66         
67         //PD: changed
68         let greenColor = UIColor(red:0.00, green:0.51, blue:0.53, alpha:1.00)
69         let tintColor = folioReader.isNight(greenColor, UIColor.white)
70
71         let closeImage = UIImage(readerImageNamed: "icon-navbar-close")?.imageTintColor(tintColor)?.withRenderingMode(.alwaysOriginal)// ignoreSystemTint(withConfiguration: readerConfig)
72         self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: closeImage, style: .plain, target: self, action: #selector(dismiss as () -> Void))
73     }
74
75     override func viewWillAppear(_ animated: Bool) {
76         super.viewWillAppear(animated)
77         configureNavBar()
78     }
79
80     //PD: changed
81     func configureNavBar() {
82         
83         let greenColor = UIColor(red:0.00, green:0.51, blue:0.53, alpha:1.00)
84         let navBackground = folioReader.isNight(self.readerConfig.nightModeMenuBackground, greenColor)
85         let tintColor = folioReader.isNight(greenColor, UIColor.white)
86         let navText = tintColor
87         let font = UIFont(name: "Avenir-Light", size: 17)!
88         setTranslucentNavigation(color: navBackground, tintColor: tintColor, titleColor: navText, andFont: font)
89     }
90     
91     // MARK: - Segmented control changes
92
93     @objc func didSwitchMenu(_ sender: UISegmentedControl) {
94         self.index = sender.selectedSegmentIndex
95         let direction: UIPageViewControllerNavigationDirection = (index == 0 ? .reverse : .forward)
96         setViewControllers([viewList[index]], direction: direction, animated: true, completion: nil)
97         self.folioReader.currentMenuIndex = index
98     }
99
100     // MARK: - Status Bar
101
102     //PD: changed
103     override var preferredStatusBarStyle : UIStatusBarStyle {
104         return .lightContent //self.folioReader.isNight(.lightContent, .default)
105     }
106 }
107
108 // MARK: UIPageViewControllerDelegate
109
110 extension PageViewController: UIPageViewControllerDelegate {
111
112     func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
113
114         if finished && completed {
115             let viewController = pageViewController.viewControllers?.last
116             segmentedControl.selectedSegmentIndex = viewList.index(of: viewController!)!
117         }
118     }
119 }
120
121 // MARK: UIPageViewControllerDataSource
122
123 extension PageViewController: UIPageViewControllerDataSource {
124
125     func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
126
127         let index = viewList.index(of: viewController)!
128         if index == viewList.count - 1 {
129             return nil
130         }
131
132         self.index = self.index + 1
133         return viewList[self.index]
134     }
135
136     func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
137
138         let index = viewList.index(of: viewController)!
139         if index == 0 {
140             return nil
141         }
142
143         self.index = self.index - 1
144         return viewList[self.index]
145     }
146 }
147