CCMore.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. //
  2. // CCMore.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 03/04/17.
  6. // Copyright © 2017 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import UIKit
  24. class CCMore: UIViewController, UITableViewDelegate, UITableViewDataSource, CCLoginDelegate, CCLoginDelegateWeb {
  25. @IBOutlet weak var themingBackground: UIImageView!
  26. @IBOutlet weak var themingAvatar: UIImageView!
  27. @IBOutlet weak var labelUsername: UILabel!
  28. @IBOutlet weak var tableView: UITableView!
  29. @IBOutlet weak var labelQuota: UILabel!
  30. @IBOutlet weak var labelQuotaExternalSite: UILabel!
  31. @IBOutlet weak var progressQuota: UIProgressView!
  32. var functionMenu = [OCExternalSites]()
  33. var settingsMenu = [OCExternalSites]()
  34. var quotaMenu = [OCExternalSites]()
  35. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  36. var menuExternalSite: [tableExternalSites]?
  37. var tabAccount : tableAccount?
  38. //var loginWeb : CCLoginWeb!
  39. override func viewDidLoad() {
  40. super.viewDidLoad()
  41. tableView.delegate = self
  42. tableView.dataSource = self
  43. tableView.separatorColor = NCBrandColor.sharedInstance.seperator
  44. if #available(iOS 11, *) {
  45. //tableView.contentInsetAdjustmentBehavior = .never
  46. }
  47. themingBackground.image = #imageLiteral(resourceName: "themingBackground")
  48. // create tap gesture recognizer
  49. let tapQuota = UITapGestureRecognizer(target: self, action: #selector(tapLabelQuotaExternalSite))
  50. labelQuotaExternalSite.isUserInteractionEnabled = true
  51. labelQuotaExternalSite.addGestureRecognizer(tapQuota)
  52. let tapImageLogo = UITapGestureRecognizer(target: self, action: #selector(tapImageLogoManageAccount))
  53. themingBackground.isUserInteractionEnabled = true
  54. themingBackground.addGestureRecognizer(tapImageLogo)
  55. // Notification
  56. NotificationCenter.default.addObserver(self, selector: #selector(self.changeTheming), name: NSNotification.Name(rawValue: "changeTheming"), object: nil)
  57. NotificationCenter.default.addObserver(self, selector: #selector(self.changeUserProfile), name: NSNotification.Name(rawValue: "changeUserProfile"), object: nil)
  58. }
  59. // Apparirà
  60. override func viewWillAppear(_ animated: Bool) {
  61. super.viewWillAppear(animated)
  62. // Clear
  63. functionMenu.removeAll()
  64. settingsMenu.removeAll()
  65. quotaMenu.removeAll()
  66. labelQuotaExternalSite.text = ""
  67. var item = OCExternalSites.init()
  68. // ITEM : Transfer
  69. item = OCExternalSites.init()
  70. item.name = "_transfers_"
  71. item.icon = "load"
  72. item.url = "segueTransfers"
  73. functionMenu.append(item)
  74. // ITEM : Activity
  75. item = OCExternalSites.init()
  76. item.name = "_activity_"
  77. item.icon = "activity"
  78. item.url = "segueActivity"
  79. functionMenu.append(item)
  80. // ITEM : Shares
  81. item = OCExternalSites.init()
  82. item.name = "_list_shares_"
  83. item.icon = "share"
  84. item.url = "segueShares"
  85. functionMenu.append(item)
  86. // ITEM : External
  87. if NCBrandOptions.sharedInstance.disable_more_external_site == false {
  88. menuExternalSite = NCManageDatabase.sharedInstance.getAllExternalSites()
  89. if menuExternalSite != nil {
  90. for table in menuExternalSite! {
  91. item = OCExternalSites.init()
  92. item.name = table.name
  93. item.url = table.url
  94. item.icon = table.icon
  95. if (table.type == "link") {
  96. item.icon = "world"
  97. functionMenu.append(item)
  98. }
  99. if (table.type == "settings") {
  100. item.icon = "settings"
  101. settingsMenu.append(item)
  102. }
  103. if (table.type == "quota") {
  104. quotaMenu.append(item)
  105. }
  106. }
  107. }
  108. }
  109. // ITEM : Settings
  110. item = OCExternalSites.init()
  111. item.name = "_settings_"
  112. item.icon = "settings"
  113. item.url = "segueSettings"
  114. settingsMenu.append(item)
  115. if (quotaMenu.count > 0) {
  116. let item = quotaMenu[0]
  117. labelQuotaExternalSite.text = item.name
  118. }
  119. // User data & Theming
  120. changeUserProfile()
  121. changeTheming()
  122. // Title
  123. self.navigationItem.title = NSLocalizedString("_more_", comment: "")
  124. // Aspect
  125. appDelegate.aspectNavigationControllerBar(self.navigationController?.navigationBar, online: appDelegate.reachability.isReachable(), hidden: false)
  126. appDelegate.aspectTabBar(self.tabBarController?.tabBar, hidden: false)
  127. // +
  128. appDelegate.plusButtonVisibile(true)
  129. self.navigationController?.setNavigationBarHidden(true, animated: animated)
  130. tableView.reloadData()
  131. }
  132. override func viewWillDisappear(_ animated: Bool) {
  133. super.viewWillDisappear(animated)
  134. self.navigationController?.setNavigationBarHidden(false, animated: animated)
  135. }
  136. @objc func changeTheming() {
  137. self.view.backgroundColor = NCBrandColor.sharedInstance.brand
  138. let fileNamePath = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(appDelegate.activeUser, activeUrl: appDelegate.activeUrl) + "-themingBackground.png"
  139. if let theminBackgroundFile = UIImage.init(contentsOfFile: fileNamePath) {
  140. themingBackground.image = theminBackgroundFile
  141. } else {
  142. themingBackground.image = #imageLiteral(resourceName: "themingBackground")
  143. }
  144. if (self.isViewLoaded && (self.view.window != nil)) {
  145. appDelegate.changeTheming(self)
  146. }
  147. }
  148. @objc func changeUserProfile() {
  149. let fileNamePath = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(appDelegate.activeUser, activeUrl: appDelegate.activeUrl) + "-avatar.png"
  150. if let themingAvatarFile = UIImage.init(contentsOfFile: fileNamePath) {
  151. themingAvatar.image = themingAvatarFile
  152. } else {
  153. themingAvatar.image = UIImage.init(named: "moreAvatar")
  154. }
  155. // Display Name user & Quota
  156. guard let tabAccount = NCManageDatabase.sharedInstance.getAccountActive() else {
  157. return
  158. }
  159. if tabAccount.displayName.isEmpty {
  160. labelUsername.text = tabAccount.user
  161. }
  162. else{
  163. labelUsername.text = tabAccount.displayName
  164. }
  165. // Shadow labelUsername TEST BLUR
  166. /*
  167. labelUsername.layer.shadowColor = UIColor.black.cgColor
  168. labelUsername.layer.shadowRadius = 4
  169. labelUsername.layer.shadowOpacity = 0.8
  170. labelUsername.layer.shadowOffset = CGSize(width: 0, height: 0)
  171. labelUsername.layer.masksToBounds = false
  172. */
  173. if (tabAccount.quotaRelative > 0) {
  174. progressQuota.progress = Float(tabAccount.quotaRelative) / 100
  175. } else {
  176. progressQuota.progress = 0
  177. }
  178. progressQuota.progressTintColor = NCBrandColor.sharedInstance.brandElement
  179. let quota : String = CCUtility.transformedSize(Double(tabAccount.quotaTotal))
  180. let quotaUsed : String = CCUtility.transformedSize(Double(tabAccount.quotaUsed))
  181. labelQuota.text = String.localizedStringWithFormat(NSLocalizedString("_quota_using_", comment: ""), quotaUsed, quota)
  182. }
  183. func numberOfSections(in tableView: UITableView) -> Int {
  184. return 2
  185. }
  186. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  187. if (section == 0) {
  188. return 10
  189. } else {
  190. return 30
  191. }
  192. }
  193. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  194. var cont = 0
  195. // Menu Normal
  196. if (section == 0) {
  197. cont = functionMenu.count
  198. }
  199. // Menu Settings
  200. if (section == 1) {
  201. cont = settingsMenu.count
  202. }
  203. return cont
  204. }
  205. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  206. let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CCCellMore
  207. // change color selection and disclosure indicator
  208. let selectionColor : UIView = UIView.init()
  209. selectionColor.backgroundColor = NCBrandColor.sharedInstance.getColorSelectBackgrond()
  210. cell.selectedBackgroundView = selectionColor
  211. cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
  212. // Menu Normal
  213. if (indexPath.section == 0) {
  214. let item = functionMenu[indexPath.row]
  215. cell.imageIcon?.image = CCGraphics.changeThemingColorImage(UIImage.init(named: item.icon), multiplier: 2, color: NCBrandColor.sharedInstance.icon)
  216. cell.labelText?.text = NSLocalizedString(item.name, comment: "")
  217. cell.labelText.textColor = NCBrandColor.sharedInstance.textView
  218. }
  219. // Menu Settings
  220. if (indexPath.section == 1) {
  221. let item = settingsMenu[indexPath.row]
  222. cell.imageIcon?.image = CCGraphics.changeThemingColorImage(UIImage.init(named: item.icon), multiplier: 2, color: NCBrandColor.sharedInstance.icon)
  223. cell.labelText?.text = NSLocalizedString(item.name, comment: "")
  224. cell.labelText.textColor = NCBrandColor.sharedInstance.textView
  225. }
  226. return cell
  227. }
  228. // method to run when table view cell is tapped
  229. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  230. var item: OCExternalSites = OCExternalSites.init()
  231. // Menu Function
  232. if indexPath.section == 0 {
  233. item = functionMenu[indexPath.row]
  234. }
  235. // Menu Settings
  236. if indexPath.section == 1 {
  237. item = settingsMenu[indexPath.row]
  238. }
  239. // Action
  240. if item.url.contains("segue") && !item.url.contains("//") {
  241. self.navigationController?.performSegue(withIdentifier: item.url, sender: self)
  242. } else if item.url.contains("open") && !item.url.contains("//") {
  243. let nameStoryboard = String(item.url[..<item.url.index(item.url.startIndex, offsetBy: 4)])
  244. //let nameStoryboard = item.url.substring(from: item.url.index(item.url.startIndex, offsetBy: 4))
  245. let storyboard = UIStoryboard(name: nameStoryboard, bundle: nil)
  246. let controller = storyboard.instantiateViewController(withIdentifier: nameStoryboard)
  247. self.present(controller, animated: true, completion: nil)
  248. } else if item.url.contains("//") {
  249. if (self.splitViewController?.isCollapsed)! {
  250. let webVC = SwiftWebVC(urlString: item.url, hideToolbar: true)
  251. webVC.delegate = self
  252. self.navigationController?.pushViewController(webVC, animated: true)
  253. self.navigationController?.navigationBar.isHidden = false
  254. } else {
  255. let webVC = SwiftModalWebVC(urlString: item.url)
  256. webVC.delegateWeb = self
  257. self.present(webVC, animated: true, completion: nil)
  258. }
  259. } else if item.url == "logout" {
  260. let alertController = UIAlertController(title: "", message: NSLocalizedString("_want_delete_", comment: ""), preferredStyle: .alert)
  261. let actionYes = UIAlertAction(title: NSLocalizedString("_yes_delete_", comment: ""), style: .default) { (action:UIAlertAction) in
  262. let manageAccount = CCManageAccount()
  263. manageAccount.delete(self.appDelegate.activeAccount)
  264. self.appDelegate.openLoginView(self, loginType: loginAddForced)
  265. }
  266. let actionNo = UIAlertAction(title: NSLocalizedString("_no_delete_", comment: ""), style: .default) { (action:UIAlertAction) in
  267. print("You've pressed No button");
  268. }
  269. alertController.addAction(actionYes)
  270. alertController.addAction(actionNo)
  271. self.present(alertController, animated: true, completion:nil)
  272. }
  273. }
  274. @objc func tapLabelQuotaExternalSite() {
  275. if (quotaMenu.count > 0) {
  276. let item = quotaMenu[0]
  277. if (self.splitViewController?.isCollapsed)! {
  278. let webVC = SwiftWebVC(urlString: item.url, hideToolbar: true)
  279. webVC.delegate = self
  280. self.navigationController?.pushViewController(webVC, animated: true)
  281. self.navigationController?.navigationBar.isHidden = false
  282. } else {
  283. let webVC = SwiftModalWebVC(urlString: item.url)
  284. webVC.delegateWeb = self
  285. self.present(webVC, animated: true, completion: nil)
  286. }
  287. }
  288. }
  289. @objc func tapImageLogoManageAccount() {
  290. let controller = CCManageAccount.init()
  291. self.navigationController?.pushViewController(controller, animated: true)
  292. }
  293. func loginSuccess(_ loginType: NSInteger) {
  294. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "initializeMain"), object: nil)
  295. appDelegate.selectedTabBarController(Int(k_tabBarApplicationIndexFile))
  296. }
  297. func loginClose() {
  298. appDelegate.activeLogin = nil
  299. }
  300. func loginWebClose() {
  301. appDelegate.activeLoginWeb = nil
  302. }
  303. }
  304. extension CCMore: SwiftModalWebVCDelegate, SwiftWebVCDelegate{
  305. public func didStartLoading() {
  306. print("Started loading.")
  307. }
  308. public func didReceiveServerRedirectForProvisionalNavigation(url: URL) {
  309. let urlString: String = url.absoluteString.lowercased()
  310. // Protocol close webVC
  311. if (urlString.contains(NCBrandOptions.sharedInstance.webCloseViewProtocolPersonalized) == true) {
  312. if (self.presentingViewController != nil) {
  313. self.dismiss(animated: true, completion: nil)
  314. } else {
  315. self.navigationController?.popToRootViewController(animated: true)
  316. }
  317. }
  318. }
  319. public func didFinishLoading(success: Bool) {
  320. print("Finished loading. Success: \(success).")
  321. }
  322. public func didFinishLoading(success: Bool, url: URL) {
  323. print("Finished loading. Success: \(success).")
  324. }
  325. public func decidePolicyForNavigationAction(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
  326. decisionHandler(.allow)
  327. }
  328. }
  329. class CCCellMore: UITableViewCell {
  330. @IBOutlet weak var labelText: UILabel!
  331. @IBOutlet weak var imageIcon: UIImageView!
  332. }