CCMore.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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 {
  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. themingBackground.image = UIImage.init(named: "themingBackground")
  45. // create tap gesture recognizer
  46. let tapQuota = UITapGestureRecognizer(target: self, action: #selector(tapLabelQuotaExternalSite))
  47. labelQuotaExternalSite.isUserInteractionEnabled = true
  48. labelQuotaExternalSite.addGestureRecognizer(tapQuota)
  49. let tapImageLogo = UITapGestureRecognizer(target: self, action: #selector(tapImageLogoManageAccount))
  50. themingBackground.isUserInteractionEnabled = true
  51. themingBackground.addGestureRecognizer(tapImageLogo)
  52. // Notification
  53. NotificationCenter.default.addObserver(self, selector: #selector(self.changeTheming), name: NSNotification.Name(rawValue: "changeTheming"), object: nil)
  54. NotificationCenter.default.addObserver(self, selector: #selector(self.changeUserProfile), name: NSNotification.Name(rawValue: "changeUserProfile"), object: nil)
  55. }
  56. // Apparirà
  57. override func viewWillAppear(_ animated: Bool) {
  58. super.viewWillAppear(animated)
  59. // Clear
  60. functionMenu.removeAll()
  61. settingsMenu.removeAll()
  62. quotaMenu.removeAll()
  63. labelQuotaExternalSite.text = ""
  64. // ITEM : Transfer
  65. var item = OCExternalSites.init()
  66. item.name = "_transfers_"
  67. item.icon = "moreTransfers"
  68. item.url = "segueTransfers"
  69. functionMenu.append(item)
  70. // ITEM : Activity
  71. item = OCExternalSites.init()
  72. if NCBrandOptions.sharedInstance.use_recent_activity_title == true {
  73. item.name = "_recent_activity_"
  74. } else {
  75. item.name = "_activity_"
  76. }
  77. item.icon = "moreActivity"
  78. item.url = "segueActivity"
  79. functionMenu.append(item)
  80. // ITEM : Notification [CUSTOM]
  81. if NCBrandOptions.sharedInstance.use_notification_on_menu_more == true {
  82. item = OCExternalSites.init()
  83. item.name = "_notifications_"
  84. item.icon = "notification"
  85. item.url = "openCCNotification"
  86. functionMenu.append(item)
  87. }
  88. // ITEM : Local storage
  89. item = OCExternalSites.init()
  90. item.name = "_local_storage_"
  91. item.icon = "moreLocalStorage"
  92. item.url = "segueLocalStorage"
  93. functionMenu.append(item)
  94. // ITEM : External
  95. menuExternalSite = NCManageDatabase.sharedInstance.getAllExternalSites(predicate: NSPredicate(format: "(account == '\(appDelegate.activeAccount!)')"))
  96. //menuExternalSite = NCManageDatabase.sharedInstance.getAllExternalSitesWithPredicate(NSPredicate(format: "(account == '\(appDelegate.activeAccount!)')"))
  97. for table in menuExternalSite! {
  98. item = OCExternalSites.init()
  99. item.name = table.name
  100. item.url = table.url
  101. item.icon = table.icon
  102. if (table.type == "link") {
  103. item.icon = "moreExternalSite"
  104. functionMenu.append(item)
  105. }
  106. if (table.type == "settings") {
  107. item.icon = "moreSettingsExternalSite"
  108. settingsMenu.append(item)
  109. }
  110. if (table.type == "quota") {
  111. quotaMenu.append(item)
  112. }
  113. }
  114. // ITEM : Settings
  115. item = OCExternalSites.init()
  116. item.name = "_settings_"
  117. item.icon = "moreSettings"
  118. item.url = "segueSettings"
  119. settingsMenu.append(item)
  120. if (quotaMenu.count > 0) {
  121. let item = quotaMenu[0]
  122. labelQuotaExternalSite.text = item.name
  123. }
  124. // ITEM : Logout [CUSTOM]
  125. if NCBrandOptions.sharedInstance.use_logout_on_menu_more == true {
  126. item = OCExternalSites.init()
  127. item.name = "_logout_"
  128. item.icon = "logout"
  129. item.url = "logout"
  130. settingsMenu.append(item)
  131. }
  132. // User data & Theming
  133. changeUserProfile()
  134. changeTheming()
  135. // Title
  136. self.navigationItem.title = NSLocalizedString("_more_", comment: "")
  137. // Aspect
  138. appDelegate.aspectNavigationControllerBar(self.navigationController?.navigationBar, encrypted: false, online: appDelegate.reachability.isReachable(), hidden: false)
  139. appDelegate.aspectTabBar(self.tabBarController?.tabBar, hidden: false)
  140. // +
  141. appDelegate.plusButtonVisibile(true)
  142. self.navigationController?.setNavigationBarHidden(true, animated: animated)
  143. tableView.reloadData()
  144. }
  145. override func viewWillDisappear(_ animated: Bool) {
  146. super.viewWillDisappear(animated)
  147. self.navigationController?.setNavigationBarHidden(false, animated: animated)
  148. }
  149. func changeTheming() {
  150. // Theming Background
  151. let theminBackgroundFile = UIImage.init(contentsOfFile: "\(appDelegate.directoryUser!)/themingBackground.png")
  152. if (theminBackgroundFile != nil) {
  153. themingBackground.image = theminBackgroundFile
  154. } else {
  155. themingBackground.image = UIImage.init(named: "themingBackground")
  156. }
  157. if (self.isViewLoaded && (self.view.window != nil)) {
  158. appDelegate.changeTheming(self)
  159. }
  160. }
  161. func changeUserProfile() {
  162. let themingAvatarFile : UIImage? = UIImage.init(contentsOfFile: "\(appDelegate.directoryUser!)/avatar.png")
  163. if (themingAvatarFile != nil) {
  164. themingAvatar.image = themingAvatarFile
  165. } else {
  166. themingAvatar.image = UIImage.init(named: "moreAvatar")
  167. }
  168. // Display Name user & Quota
  169. tabAccount = NCManageDatabase.sharedInstance.getAccountActive()
  170. if (tabAccount != nil) {
  171. if let displayName = tabAccount?.displayName {
  172. if displayName.isEmpty {
  173. labelUsername.text = tabAccount!.user
  174. }
  175. else{
  176. labelUsername.text = tabAccount!.displayName
  177. }
  178. }
  179. else{
  180. labelUsername.text = tabAccount!.user
  181. }
  182. // fix CCMore.swift line 208 Version 2.17.2 (00005)
  183. if (tabAccount?.quotaRelative != nil && tabAccount?.quotaTotal != nil && tabAccount?.quotaUsed != nil) {
  184. progressQuota.progress = Float((tabAccount?.quotaRelative)!) / 100
  185. progressQuota.progressTintColor = NCBrandColor.sharedInstance.brand
  186. let quota : String = CCUtility.transformedSize(Double((tabAccount?.quotaTotal)!))
  187. let quotaUsed : String = CCUtility.transformedSize(Double((tabAccount?.quotaUsed)!))
  188. labelQuota.text = String.localizedStringWithFormat(NSLocalizedString("_quota_using_", comment: ""), quotaUsed, quota)
  189. }
  190. }
  191. }
  192. func numberOfSections(in tableView: UITableView) -> Int {
  193. return 2
  194. }
  195. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  196. if (section == 0) {
  197. return 10
  198. } else {
  199. return 30
  200. }
  201. }
  202. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  203. var cont = 0
  204. // Menu Normal
  205. if (section == 0) {
  206. cont = functionMenu.count
  207. }
  208. // Menu Settings
  209. if (section == 1) {
  210. cont = settingsMenu.count
  211. }
  212. return cont
  213. }
  214. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  215. let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CCCellMore
  216. // change color selection and disclosure indicator
  217. let selectionColor : UIView = UIView.init()
  218. selectionColor.backgroundColor = NCBrandColor.sharedInstance.getColorSelectBackgrond()
  219. cell.selectedBackgroundView = selectionColor
  220. cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
  221. // Menu Normal
  222. if (indexPath.section == 0) {
  223. let item = functionMenu[indexPath.row]
  224. cell.imageIcon?.image = UIImage.init(named: item.icon)
  225. cell.labelText?.text = NSLocalizedString(item.name, comment: "")
  226. cell.labelText.textColor = NCBrandColor.sharedInstance.moreNormal
  227. }
  228. // Menu Settings
  229. if (indexPath.section == 1) {
  230. let item = settingsMenu[indexPath.row]
  231. cell.imageIcon?.image = UIImage.init(named: item.icon)
  232. cell.labelText?.text = NSLocalizedString(item.name, comment: "")
  233. cell.labelText.textColor = NCBrandColor.sharedInstance.moreSettings
  234. }
  235. return cell
  236. }
  237. // method to run when table view cell is tapped
  238. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  239. var item: OCExternalSites = OCExternalSites.init()
  240. // Menu Function
  241. if indexPath.section == 0 {
  242. item = functionMenu[indexPath.row]
  243. }
  244. // Menu Settings
  245. if indexPath.section == 1 {
  246. item = settingsMenu[indexPath.row]
  247. }
  248. // Action
  249. if item.url.contains("segue") && !item.url.contains("//") {
  250. self.navigationController?.performSegue(withIdentifier: item.url, sender: self)
  251. } else if item.url.contains("open") && !item.url.contains("//") {
  252. let nameStoryboard = item.url.substring(from: item.url.index(item.url.startIndex, offsetBy: 4))
  253. let storyboard = UIStoryboard(name: nameStoryboard, bundle: nil)
  254. let controller = storyboard.instantiateViewController(withIdentifier: nameStoryboard)
  255. self.present(controller, animated: true, completion: nil)
  256. } else if item.url.contains("//") {
  257. if (self.splitViewController?.isCollapsed)! {
  258. let webVC = SwiftWebVC(urlString: item.url, hideToolbar: false)
  259. self.navigationController?.pushViewController(webVC, animated: true)
  260. self.navigationController?.navigationBar.isHidden = false
  261. } else {
  262. let webVC = SwiftModalWebVC(urlString: item.url)
  263. self.present(webVC, animated: true, completion: nil)
  264. }
  265. } else if item.url == "logout" {
  266. let alertController = UIAlertController(title: "", message: NSLocalizedString("_want_delete_", comment: ""), preferredStyle: .alert)
  267. let actionYes = UIAlertAction(title: NSLocalizedString("_yes_delete_", comment: ""), style: .default) { (action:UIAlertAction) in
  268. let manageAccount = CCManageAccount()
  269. manageAccount.delete(self.appDelegate.activeAccount)
  270. self.loginWeb = CCLoginWeb()
  271. self.loginWeb.delegate = self
  272. self.loginWeb.loginType = loginAddForced
  273. self.loginWeb.presentModalWithDefaultTheme(self)
  274. }
  275. let actionNo = UIAlertAction(title: NSLocalizedString("_no_delete_", comment: ""), style: .default) { (action:UIAlertAction) in
  276. print("You've pressed No button");
  277. }
  278. alertController.addAction(actionYes)
  279. alertController.addAction(actionNo)
  280. self.present(alertController, animated: true, completion:nil)
  281. }
  282. }
  283. func tapLabelQuotaExternalSite() {
  284. if (quotaMenu.count > 0) {
  285. let item = quotaMenu[0]
  286. if (self.splitViewController?.isCollapsed)! {
  287. let webVC = SwiftWebVC(urlString: item.url, hideToolbar: false)
  288. self.navigationController?.pushViewController(webVC, animated: true)
  289. self.navigationController?.navigationBar.isHidden = false
  290. } else {
  291. let webVC = SwiftModalWebVC(urlString: item.url)
  292. self.present(webVC, animated: true, completion: nil)
  293. }
  294. }
  295. }
  296. func tapImageLogoManageAccount() {
  297. let controller = CCManageAccount.init()
  298. self.navigationController?.pushViewController(controller, animated: true)
  299. }
  300. func loginSuccess(_ loginType: NSInteger) {
  301. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "initializeMain"), object: nil)
  302. appDelegate.selectedTabBarController(Int(k_tabBarApplicationIndexFile))
  303. }
  304. }
  305. class CCCellMore: UITableViewCell {
  306. @IBOutlet weak var labelText: UILabel!
  307. @IBOutlet weak var imageIcon: UIImageView!
  308. }