CCMore.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. // ITEM : Transfer
  68. var item = OCExternalSites.init()
  69. item.name = "_transfers_"
  70. item.icon = "moreTransfers"
  71. item.url = "segueTransfers"
  72. functionMenu.append(item)
  73. // ITEM : Activity
  74. item = OCExternalSites.init()
  75. item.name = "_activity_"
  76. item.icon = "moreActivity"
  77. item.url = "segueActivity"
  78. functionMenu.append(item)
  79. // ITEM : Shares
  80. item = OCExternalSites.init()
  81. item.name = "_list_shares_"
  82. item.icon = "moreShares"
  83. item.url = "segueShares"
  84. functionMenu.append(item)
  85. // ITEM : External
  86. if NCBrandOptions.sharedInstance.disable_more_external_site == false {
  87. menuExternalSite = NCManageDatabase.sharedInstance.getAllExternalSites(predicate: NSPredicate(format: "(account == '\(appDelegate.activeAccount!)')"))
  88. for table in menuExternalSite! {
  89. item = OCExternalSites.init()
  90. item.name = table.name
  91. item.url = table.url
  92. item.icon = table.icon
  93. if (table.type == "link") {
  94. item.icon = "moreExternalSite"
  95. functionMenu.append(item)
  96. }
  97. if (table.type == "settings") {
  98. item.icon = "moreSettingsExternalSite"
  99. settingsMenu.append(item)
  100. }
  101. if (table.type == "quota") {
  102. quotaMenu.append(item)
  103. }
  104. }
  105. }
  106. // ITEM : Settings
  107. item = OCExternalSites.init()
  108. item.name = "_settings_"
  109. item.icon = "moreSettings"
  110. item.url = "segueSettings"
  111. settingsMenu.append(item)
  112. if (quotaMenu.count > 0) {
  113. let item = quotaMenu[0]
  114. labelQuotaExternalSite.text = item.name
  115. }
  116. // User data & Theming
  117. changeUserProfile()
  118. changeTheming()
  119. // Title
  120. self.navigationItem.title = NSLocalizedString("_more_", comment: "")
  121. // Aspect
  122. appDelegate.aspectNavigationControllerBar(self.navigationController?.navigationBar, online: appDelegate.reachability.isReachable(), hidden: false)
  123. appDelegate.aspectTabBar(self.tabBarController?.tabBar, hidden: false)
  124. // +
  125. appDelegate.plusButtonVisibile(true)
  126. self.navigationController?.setNavigationBarHidden(true, animated: animated)
  127. tableView.reloadData()
  128. }
  129. override func viewWillDisappear(_ animated: Bool) {
  130. super.viewWillDisappear(animated)
  131. self.navigationController?.setNavigationBarHidden(false, animated: animated)
  132. }
  133. @objc func changeTheming() {
  134. self.view.backgroundColor = NCBrandColor.sharedInstance.brand
  135. if let theminBackgroundFile = UIImage.init(contentsOfFile: "\(appDelegate.directoryUser!)/themingBackground.png") {
  136. themingBackground.image = theminBackgroundFile
  137. } else {
  138. themingBackground.image = #imageLiteral(resourceName: "themingBackground")
  139. }
  140. if (self.isViewLoaded && (self.view.window != nil)) {
  141. appDelegate.changeTheming(self)
  142. }
  143. }
  144. @objc func changeUserProfile() {
  145. if let themingAvatarFile = UIImage.init(contentsOfFile: "\(appDelegate.directoryUser!)/avatar.png") {
  146. themingAvatar.image = themingAvatarFile
  147. } else {
  148. themingAvatar.image = UIImage.init(named: "moreAvatar")
  149. }
  150. // Display Name user & Quota
  151. guard let tabAccount = NCManageDatabase.sharedInstance.getAccountActive() else {
  152. return
  153. }
  154. if tabAccount.displayName.isEmpty {
  155. labelUsername.text = tabAccount.user
  156. }
  157. else{
  158. labelUsername.text = tabAccount.displayName
  159. }
  160. // Shadow labelUsername
  161. //labelUsername.shadowColor = UIColor(red:0.0, green:0.0, blue:0.0, alpha:0.4)
  162. //labelUsername.shadowOffset = CGSize(width: 2, height: 2)
  163. if (tabAccount.quotaRelative > 0) {
  164. progressQuota.progress = Float(tabAccount.quotaRelative) / 100
  165. } else {
  166. progressQuota.progress = 0
  167. }
  168. progressQuota.progressTintColor = NCBrandColor.sharedInstance.brandElement
  169. let quota : String = CCUtility.transformedSize(Double(tabAccount.quotaTotal))
  170. let quotaUsed : String = CCUtility.transformedSize(Double(tabAccount.quotaUsed))
  171. labelQuota.text = String.localizedStringWithFormat(NSLocalizedString("_quota_using_", comment: ""), quotaUsed, quota)
  172. }
  173. func numberOfSections(in tableView: UITableView) -> Int {
  174. return 2
  175. }
  176. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  177. if (section == 0) {
  178. return 10
  179. } else {
  180. return 30
  181. }
  182. }
  183. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  184. var cont = 0
  185. // Menu Normal
  186. if (section == 0) {
  187. cont = functionMenu.count
  188. }
  189. // Menu Settings
  190. if (section == 1) {
  191. cont = settingsMenu.count
  192. }
  193. return cont
  194. }
  195. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  196. let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CCCellMore
  197. // change color selection and disclosure indicator
  198. let selectionColor : UIView = UIView.init()
  199. selectionColor.backgroundColor = NCBrandColor.sharedInstance.getColorSelectBackgrond()
  200. cell.selectedBackgroundView = selectionColor
  201. cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
  202. // Menu Normal
  203. if (indexPath.section == 0) {
  204. let item = functionMenu[indexPath.row]
  205. cell.imageIcon?.image = UIImage.init(named: item.icon)
  206. cell.labelText?.text = NSLocalizedString(item.name, comment: "")
  207. cell.labelText.textColor = NCBrandColor.sharedInstance.textView
  208. }
  209. // Menu Settings
  210. if (indexPath.section == 1) {
  211. let item = settingsMenu[indexPath.row]
  212. cell.imageIcon?.image = UIImage.init(named: item.icon)
  213. cell.labelText?.text = NSLocalizedString(item.name, comment: "")
  214. cell.labelText.textColor = NCBrandColor.sharedInstance.textView
  215. }
  216. return cell
  217. }
  218. // method to run when table view cell is tapped
  219. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  220. var item: OCExternalSites = OCExternalSites.init()
  221. // Menu Function
  222. if indexPath.section == 0 {
  223. item = functionMenu[indexPath.row]
  224. }
  225. // Menu Settings
  226. if indexPath.section == 1 {
  227. item = settingsMenu[indexPath.row]
  228. }
  229. // Action
  230. if item.url.contains("segue") && !item.url.contains("//") {
  231. self.navigationController?.performSegue(withIdentifier: item.url, sender: self)
  232. } else if item.url.contains("open") && !item.url.contains("//") {
  233. let nameStoryboard = String(item.url[..<item.url.index(item.url.startIndex, offsetBy: 4)])
  234. //let nameStoryboard = item.url.substring(from: item.url.index(item.url.startIndex, offsetBy: 4))
  235. let storyboard = UIStoryboard(name: nameStoryboard, bundle: nil)
  236. let controller = storyboard.instantiateViewController(withIdentifier: nameStoryboard)
  237. self.present(controller, animated: true, completion: nil)
  238. } else if item.url.contains("//") {
  239. if (self.splitViewController?.isCollapsed)! {
  240. let webVC = SwiftWebVC(urlString: item.url, hideToolbar: true)
  241. webVC.delegate = self
  242. self.navigationController?.pushViewController(webVC, animated: true)
  243. self.navigationController?.navigationBar.isHidden = false
  244. } else {
  245. let webVC = SwiftModalWebVC(urlString: item.url)
  246. webVC.delegateWeb = self
  247. self.present(webVC, animated: true, completion: nil)
  248. }
  249. } else if item.url == "logout" {
  250. let alertController = UIAlertController(title: "", message: NSLocalizedString("_want_delete_", comment: ""), preferredStyle: .alert)
  251. let actionYes = UIAlertAction(title: NSLocalizedString("_yes_delete_", comment: ""), style: .default) { (action:UIAlertAction) in
  252. let manageAccount = CCManageAccount()
  253. manageAccount.delete(self.appDelegate.activeAccount)
  254. self.appDelegate.openLoginView(self, loginType: loginAddForced)
  255. }
  256. let actionNo = UIAlertAction(title: NSLocalizedString("_no_delete_", comment: ""), style: .default) { (action:UIAlertAction) in
  257. print("You've pressed No button");
  258. }
  259. alertController.addAction(actionYes)
  260. alertController.addAction(actionNo)
  261. self.present(alertController, animated: true, completion:nil)
  262. }
  263. }
  264. @objc func tapLabelQuotaExternalSite() {
  265. if (quotaMenu.count > 0) {
  266. let item = quotaMenu[0]
  267. if (self.splitViewController?.isCollapsed)! {
  268. let webVC = SwiftWebVC(urlString: item.url, hideToolbar: true)
  269. webVC.delegate = self
  270. self.navigationController?.pushViewController(webVC, animated: true)
  271. self.navigationController?.navigationBar.isHidden = false
  272. } else {
  273. let webVC = SwiftModalWebVC(urlString: item.url)
  274. webVC.delegateWeb = self
  275. self.present(webVC, animated: true, completion: nil)
  276. }
  277. }
  278. }
  279. @objc func tapImageLogoManageAccount() {
  280. let controller = CCManageAccount.init()
  281. self.navigationController?.pushViewController(controller, animated: true)
  282. }
  283. func loginSuccess(_ loginType: NSInteger) {
  284. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "initializeMain"), object: nil)
  285. appDelegate.selectedTabBarController(Int(k_tabBarApplicationIndexFile))
  286. }
  287. func loginClose() {
  288. appDelegate.activeLogin = nil
  289. }
  290. func loginWebClose() {
  291. appDelegate.activeLoginWeb = nil
  292. }
  293. }
  294. extension CCMore: SwiftModalWebVCDelegate, SwiftWebVCDelegate{
  295. public func didStartLoading() {
  296. print("Started loading.")
  297. }
  298. public func didReceiveServerRedirectForProvisionalNavigation(url: URL) {
  299. let urlString: String = url.absoluteString.lowercased()
  300. // Protocol close webVC
  301. if (urlString.contains(NCBrandOptions.sharedInstance.webCloseViewProtocolPersonalized) == true) {
  302. if (self.presentingViewController != nil) {
  303. self.dismiss(animated: true, completion: nil)
  304. } else {
  305. self.navigationController?.popToRootViewController(animated: true)
  306. }
  307. }
  308. }
  309. public func didFinishLoading(success: Bool) {
  310. print("Finished loading. Success: \(success).")
  311. }
  312. public func didFinishLoading(success: Bool, url: URL) {
  313. print("Finished loading. Success: \(success).")
  314. }
  315. }
  316. class CCCellMore: UITableViewCell {
  317. @IBOutlet weak var labelText: UILabel!
  318. @IBOutlet weak var imageIcon: UIImageView!
  319. }