2 // MenuViewController.swift
5 // Created by Pawel Dabrowski on 29/05/2018.
6 // Copyright © 2018 Fundacja Nowoczesna Polska. All rights reserved.
13 class MenuViewController: UIViewController {
15 @IBOutlet weak var tableView: UITableView!
17 var cellsArray: [WLTableViewCell]!
20 @IBOutlet weak var bottomView: UIView!
21 @IBOutlet weak var bottomViewBgView: UIView!
23 @IBOutlet weak var guestBottomView: UIView!
24 @IBOutlet weak var loginButton: UIButton!
26 @IBOutlet weak var userBottomView: UIView!
27 @IBOutlet weak var loggedInLabel: UILabel!
28 @IBOutlet weak var logoutButton: UIButton!
30 @IBOutlet weak var bottomViewHeightConstraint: NSLayoutConstraint!
32 var selectedRow: IndexPath?
34 var firstAppear = true
35 override func viewDidLoad() {
37 tableView.separatorStyle = .none
39 var contentInset = tableView.contentInset
41 tableView.contentInset = contentInset
43 loginButton.text = "menu_login".localized
44 logoutButton.text = "sign_out".localized
45 loginButton.layer.cornerRadius = 15
46 loginButton.layer.borderColor = UIColor.white.cgColor
47 loginButton.layer.borderWidth = 1
48 logoutButton.layer.cornerRadius = 15
49 logoutButton.layer.borderColor = UIColor.white.cgColor
50 logoutButton.layer.borderWidth = 1
52 bottomView.backgroundColor = Constants.Colors.menuTintColor()
53 bottomViewBgView.backgroundColor = Constants.Colors.menuTintColor()
58 appDelegate.sideMenuNavigationController = self.navigationController
62 override func viewDidAppear(_ animated: Bool) {
63 super.viewDidAppear(animated)
65 tableView.selectRow(at: IndexPath(row: wlRow, section: 0), animated: false, scrollPosition: .none)
70 if syncManager.isLoggedIn(){
71 cellsArray = [WLTableViewCell]()
73 if DatabaseManager.shared.isUserPremium() == false && Constants.donateEnabled{
74 cellsArray.append(MenuSupportUsTableViewCell.instance(delegate: self))
77 if selectedRow == nil {
78 selectedRow = IndexPath(row: cellsArray.count, section: 0)
79 tableView.selectRow(at: selectedRow, animated: false, scrollPosition: .none)
82 cellsArray.append(MenuTableViewCell.instance(menuItem: .wolne_lektury))
83 wlRow = cellsArray.count - 1
84 cellsArray.append(MenuLineTableViewCell.instance())
85 if Constants.donateEnabled {
86 cellsArray.append(MenuTableViewCell.instance(menuItem: .premium))
88 cellsArray.append(MenuTableViewCell.instance(menuItem: .catalog))
89 cellsArray.append(MenuTableViewCell.instance(menuItem: .audiobooks))
90 cellsArray.append(MenuTableViewCell.instance(menuItem: .reading))
91 cellsArray.append(MenuTableViewCell.instance(menuItem: .favourites))
92 cellsArray.append(MenuTableViewCell.instance(menuItem: .completed))
93 cellsArray.append(MenuLineTableViewCell.instance())
94 cellsArray.append(MenuTableViewCell.instance(menuItem: .downloaded))
95 cellsArray.append(MenuLineTableViewCell.instance())
96 cellsArray.append(MenuTableViewCell.instance(menuItem: .news))
97 cellsArray.append(MenuTableViewCell.instance(menuItem: .settings))
98 cellsArray.append(MenuTableViewCell.instance(menuItem: .about))
102 cellsArray = [WLTableViewCell]()
104 if Constants.donateEnabled {
105 cellsArray.append(MenuSupportUsTableViewCell.instance(delegate: self))
108 if selectedRow == nil {
109 selectedRow = IndexPath(row: cellsArray.count, section: 0)
110 tableView.selectRow(at: selectedRow, animated: false, scrollPosition: .none)
113 cellsArray.append(MenuTableViewCell.instance(menuItem: .wolne_lektury))
114 wlRow = cellsArray.count - 1
115 cellsArray.append(MenuLineTableViewCell.instance())
116 if Constants.donateEnabled {
117 cellsArray.append(MenuTableViewCell.instance(menuItem: .premium))
120 cellsArray.append(MenuTableViewCell.instance(menuItem: .catalog))
121 cellsArray.append(MenuTableViewCell.instance(menuItem: .audiobooks))
122 cellsArray.append(MenuLineTableViewCell.instance())
123 cellsArray.append(MenuTableViewCell.instance(menuItem: .downloaded))
124 cellsArray.append(MenuLineTableViewCell.instance())
125 cellsArray.append(MenuTableViewCell.instance(menuItem: .news))
126 cellsArray.append(MenuTableViewCell.instance(menuItem: .settings))
127 cellsArray.append(MenuTableViewCell.instance(menuItem: .about))
129 tableView.reloadData()
134 func selectItem(menuItem: MenuItem){
136 for cell in cellsArray {
137 if let cell = cell as? MenuTableViewCell, cell.menuItem == menuItem {
138 tableView.selectRow(at: IndexPath(row: index, section: 0), animated: false, scrollPosition: .none)
145 func refreshBottomView() {
147 userBottomView.isHidden = true
148 guestBottomView.isHidden = true
150 if syncManager.isLoggedIn(){
152 var titleText = NSMutableAttributedString(string: "")
154 if let user = DatabaseManager.shared.rlmApplication?.user {
156 titleText = NSMutableAttributedString(attributedString: NSAttributedString(string: "logged_as".localized + "\n", font: UIFont.systemFont(ofSize: 10)))
157 titleText.append(NSAttributedString(string: user.username, font: UIFont.systemFont(ofSize: 12)))
158 loggedInLabel.attributedText = titleText
161 userBottomView.isHidden = false
162 bottomViewHeightConstraint.constant = userBottomView.frame.size.height
165 guestBottomView.isHidden = false
166 bottomViewHeightConstraint.constant = guestBottomView.frame.size.height
171 appDelegate.showWindowHud()
172 syncManager.getPreview { [weak self] (result) in
174 guard let strongSelf = self else { return }
175 strongSelf.appDelegate.hideWindowHud()
177 case .success(let model):
178 let array = model as! [BookModel]
182 strongSelf.appDelegate.mainNavigator.presentPremiumBook(bookSlug: book.slug)
185 strongSelf.presentNoPremiereAlert()
188 case .failure/*(let error)*/:
189 strongSelf.presentToast(message: "fetching_premium_failed".localized)
194 func presentNoPremiereAlert() {
196 let message = syncManager.isLoggedIn() ? "no_prapremiere_message_logged".localized : "no_prapremiere_message".localized
198 let alertController = UIAlertController(
199 title: "no_prapremiere_title".localized,
201 preferredStyle: UIAlertControllerStyle.alert
204 if syncManager.isLoggedIn() {
205 let action = UIAlertAction(title: "OK".localized, style: UIAlertActionStyle.cancel, handler: nil)
206 alertController.addAction(action)
209 let cancelAction = UIAlertAction(title: "no_thanks".localized, style: UIAlertActionStyle.cancel, handler: nil)
210 alertController.addAction(cancelAction)
212 let okAction = UIAlertAction(title: "become_a_friend".localized, style: UIAlertActionStyle.default) { [weak self]
213 (result : UIAlertAction) -> Void in
214 self?.onBecomeAFriendClick()
216 alertController.addAction(okAction)
218 present(alertController, animated: true, completion: nil)
221 func onBecomeAFriendClick() {
222 if syncManager.isLoggedIn() {
230 func showPremiumForm() {
234 func showPayPalForm() {
236 if syncManager.isLoggedIn() {
237 appDelegate.mainNavigator.presentPayPalForm()
240 presentToast(message: "login_first".localized)
244 func showLoginFirst() {
246 let alertController = UIAlertController(
247 title: "login".localized,
248 message: "login_first".localized,
249 preferredStyle: UIAlertControllerStyle.alert
252 let cancelAction = UIAlertAction(title: "no_thanks".localized, style: UIAlertActionStyle.cancel, handler: nil)
253 alertController.addAction(cancelAction)
255 let loginAction = UIAlertAction(title: "login".localized, style: UIAlertActionStyle.default) { [weak self]
256 (result : UIAlertAction) -> Void in
257 self?.onLoginClicked()
259 alertController.addAction(loginAction)
261 self.present(alertController, animated: true, completion: nil)
264 func onLoginClicked() {
265 appDelegate.login(fromViewController: self)
270 appDelegate.sideMenuNavigationController = nil
273 @IBAction func logoutButtonAction(_ sender: Any) {
275 appDelegate.mainNavigator.reset()
278 @IBAction func loginButtonAction(_ sender: Any) {
283 extension MenuViewController: UITableViewDataSource{
284 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
285 return cellsArray[indexPath.row]
288 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
289 return cellsArray.count
291 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
292 return cellsArray[indexPath.row].getHeight()
296 extension MenuViewController: UITableViewDelegate{
297 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
299 if let cell = cellsArray[indexPath.row] as? MenuTableViewCell{
300 switch cell.menuItem{
302 appDelegate.mainNavigator.presentLibrary(dismissSideMenu: true)
305 if let selectedRow = selectedRow{
306 tableView.selectRow(at: selectedRow, animated: false, scrollPosition: .none)
310 appDelegate.mainNavigator.presentBookList(listViewControllerType: .audiobooks)
312 appDelegate.mainNavigator.presentDownloaded()
314 appDelegate.mainNavigator.presentSearch()
316 appDelegate.mainNavigator.presentNews()
318 appDelegate.mainNavigator.presentSettings()
320 appDelegate.mainNavigator.presentAbout()
322 appDelegate.mainNavigator.presentBookList(listViewControllerType: .reading_now)
324 appDelegate.mainNavigator.presentBookList(listViewControllerType: .favourites)
326 appDelegate.mainNavigator.presentBookList(listViewControllerType: .completed)
330 selectedRow = indexPath
332 else if cellsArray[indexPath.row] is MenuSupportUsTableViewCell{
333 selectedRow = indexPath
336 if let selectedRow = selectedRow{
337 tableView.selectRow(at: selectedRow, animated: false, scrollPosition: .none)
343 extension MenuViewController: MenuSupportUsTableViewCellDelegate{
344 func menuSupportUsButtonTapped(){
345 appDelegate.mainNavigator.presentSupportUs()