CCMore.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. //
  2. // CCMore.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 03/04/17.
  6. // Copyright © 2017 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  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. import NCCommunication
  25. class CCMore: UIViewController, UITableViewDelegate, UITableViewDataSource {
  26. @IBOutlet weak var tableView: UITableView!
  27. @IBOutlet weak var labelQuota: UILabel!
  28. @IBOutlet weak var labelQuotaExternalSite: UILabel!
  29. @IBOutlet weak var progressQuota: UIProgressView!
  30. @IBOutlet weak var viewQuota: UIView!
  31. var functionMenu: [NCCommunicationExternalSite] = []
  32. var externalSiteMenu: [NCCommunicationExternalSite] = []
  33. var settingsMenu: [NCCommunicationExternalSite] = []
  34. var quotaMenu: [NCCommunicationExternalSite] = []
  35. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  36. var tabAccount: tableAccount?
  37. required init?(coder aDecoder: NSCoder) {
  38. super.init(coder: aDecoder)
  39. appDelegate.activeMore = self
  40. }
  41. override func viewDidLoad() {
  42. super.viewDidLoad()
  43. tableView.delegate = self
  44. tableView.dataSource = self
  45. self.navigationItem.title = NSLocalizedString("_more_", comment: "")
  46. // create tap gesture recognizer
  47. let tapQuota = UITapGestureRecognizer(target: self, action: #selector(tapLabelQuotaExternalSite))
  48. labelQuotaExternalSite.isUserInteractionEnabled = true
  49. labelQuotaExternalSite.addGestureRecognizer(tapQuota)
  50. // Notification
  51. NotificationCenter.default.addObserver(self, selector: #selector(changeUserProfile), name: NSNotification.Name(rawValue: k_notificationCenter_changeUserProfile), object: nil)
  52. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: k_notificationCenter_changeTheming), object: nil)
  53. changeTheming()
  54. }
  55. override func viewWillAppear(_ animated: Bool) {
  56. super.viewWillAppear(animated)
  57. var item = NCCommunicationExternalSite()
  58. // Clear
  59. functionMenu.removeAll()
  60. externalSiteMenu.removeAll()
  61. settingsMenu.removeAll()
  62. quotaMenu.removeAll()
  63. labelQuotaExternalSite.text = ""
  64. // ITEM : Transfer
  65. item = NCCommunicationExternalSite()
  66. item.name = "_transfers_"
  67. item.icon = "load"
  68. item.url = "segueTransfers"
  69. functionMenu.append(item)
  70. // ITEM : Recent
  71. item = NCCommunicationExternalSite()
  72. item.name = "_recent_"
  73. item.icon = "recent"
  74. item.url = "segueRecent"
  75. functionMenu.append(item)
  76. // ITEM : Notification
  77. item = NCCommunicationExternalSite()
  78. item.name = "_notification_"
  79. item.icon = "notification"
  80. item.url = "segueNotification"
  81. functionMenu.append(item)
  82. // ITEM : Activity
  83. item = NCCommunicationExternalSite()
  84. item.name = "_activity_"
  85. item.icon = "activity"
  86. item.url = "segueActivity"
  87. functionMenu.append(item)
  88. // ITEM : Shares
  89. item = NCCommunicationExternalSite()
  90. item.name = "_list_shares_"
  91. item.icon = "shareFill"
  92. item.url = "segueShares"
  93. functionMenu.append(item)
  94. // ITEM : Offline
  95. item = NCCommunicationExternalSite()
  96. item.name = "_manage_file_offline_"
  97. item.icon = "offline"
  98. item.url = "segueOffline"
  99. functionMenu.append(item)
  100. // ITEM : Scan
  101. item = NCCommunicationExternalSite()
  102. item.name = "_scanned_images_"
  103. item.icon = "scan"
  104. item.url = "openStoryboardScan"
  105. functionMenu.append(item)
  106. // ITEM : Trash
  107. let serverVersionMajor = NCManageDatabase.sharedInstance.getCapabilitiesServerInt(account: appDelegate.account, elements: NCElementsJSON.shared.capabilitiesVersionMajor)
  108. if serverVersionMajor >= Int(k_trash_version_available) {
  109. item = NCCommunicationExternalSite()
  110. item.name = "_trash_view_"
  111. item.icon = "trash"
  112. item.url = "segueTrash"
  113. functionMenu.append(item)
  114. }
  115. // ITEM : Settings
  116. item = NCCommunicationExternalSite()
  117. item.name = "_settings_"
  118. item.icon = "settings"
  119. item.url = "segueSettings"
  120. settingsMenu.append(item)
  121. if (quotaMenu.count > 0) {
  122. let item = quotaMenu[0]
  123. labelQuotaExternalSite.text = item.name
  124. }
  125. changeUserProfile()
  126. // ITEM : External
  127. if NCBrandOptions.sharedInstance.disable_more_external_site == false {
  128. if let externalSites = NCManageDatabase.sharedInstance.getAllExternalSites(account: appDelegate.account) {
  129. for externalSite in externalSites {
  130. if (externalSite.type == "link" && externalSite.name != "" && externalSite.url != "") {
  131. item = NCCommunicationExternalSite()
  132. item.name = externalSite.name
  133. item.url = externalSite.url
  134. item.icon = "world"
  135. externalSiteMenu.append(item)
  136. }
  137. if (externalSite.type == "settings") {
  138. item.icon = "settings"
  139. settingsMenu.append(item)
  140. }
  141. if (externalSite.type == "quota") {
  142. quotaMenu.append(item)
  143. }
  144. }
  145. tableView.reloadData()
  146. } else {
  147. tableView.reloadData()
  148. }
  149. } else {
  150. tableView.reloadData()
  151. }
  152. }
  153. @objc func changeTheming() {
  154. view.backgroundColor = NCBrandColor.sharedInstance.backgroundForm
  155. tableView.backgroundColor = NCBrandColor.sharedInstance.backgroundForm
  156. tableView.separatorColor = NCBrandColor.sharedInstance.separator
  157. tableView.reloadData()
  158. viewQuota.backgroundColor = NCBrandColor.sharedInstance.backgroundForm
  159. progressQuota.progressTintColor = NCBrandColor.sharedInstance.brandElement
  160. }
  161. @objc func changeUserProfile() {
  162. // Display Name user & Quota
  163. var quota: String = ""
  164. guard let tabAccount = NCManageDatabase.sharedInstance.getAccountActive() else {
  165. return
  166. }
  167. self.tabAccount = tabAccount
  168. if (tabAccount.quotaRelative > 0) {
  169. progressQuota.progress = Float(tabAccount.quotaRelative) / 100
  170. } else {
  171. progressQuota.progress = 0
  172. }
  173. switch Double(tabAccount.quotaTotal) {
  174. case Double(-1):
  175. quota = "0"
  176. case Double(-2):
  177. quota = NSLocalizedString("_quota_space_unknown_", comment: "")
  178. case Double(-3):
  179. quota = NSLocalizedString("_quota_space_unlimited_", comment: "")
  180. default:
  181. quota = CCUtility.transformedSize(Double(tabAccount.quotaTotal))
  182. }
  183. let quotaUsed: String = CCUtility.transformedSize(Double(tabAccount.quotaUsed))
  184. labelQuota.text = String.localizedStringWithFormat(NSLocalizedString("_quota_using_", comment: ""), quotaUsed, quota)
  185. }
  186. func numberOfSections(in tableView: UITableView) -> Int {
  187. if (externalSiteMenu.count == 0) {
  188. return 3
  189. } else {
  190. return 4
  191. }
  192. }
  193. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  194. var cont = 0
  195. if (section == 0) {
  196. cont = tabAccount == nil ? 0 : 1
  197. } else if (section == 1) {
  198. // Menu Normal
  199. cont = functionMenu.count
  200. } else {
  201. switch (numberOfSections(in: tableView)) {
  202. case 3:
  203. // Menu Settings
  204. if (section == 2) {
  205. cont = settingsMenu.count
  206. }
  207. case 4:
  208. // Menu External Site
  209. if (section == 2) {
  210. cont = externalSiteMenu.count
  211. }
  212. // Menu Settings
  213. if (section == 3) {
  214. cont = settingsMenu.count
  215. }
  216. default:
  217. cont = 0
  218. }
  219. }
  220. return cont
  221. }
  222. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  223. let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CCCellMore
  224. var item = NCCommunicationExternalSite()
  225. // change color selection and disclosure indicator
  226. let selectionColor: UIView = UIView()
  227. selectionColor.backgroundColor = NCBrandColor.sharedInstance.select
  228. cell.selectedBackgroundView = selectionColor
  229. cell.backgroundColor = NCBrandColor.sharedInstance.backgroundCell
  230. cell.accessoryType = UITableViewCell.AccessoryType.disclosureIndicator
  231. if (indexPath.section == 0) {
  232. let fileNamePath = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(appDelegate.user, urlBase: appDelegate.urlBase) + "-" + appDelegate.user + ".png"
  233. if let themingAvatarFile = UIImage.init(contentsOfFile: fileNamePath) {
  234. cell.imageIcon?.image = themingAvatarFile
  235. } else {
  236. cell.imageIcon?.image = UIImage.init(named: "moreAvatar")
  237. }
  238. cell.imageIcon?.layer.masksToBounds = true
  239. cell.imageIcon?.layer.cornerRadius = cell.imageIcon.frame.size.width / 2
  240. if let account = tabAccount {
  241. cell.labelText?.text = account.displayName
  242. cell.labelText.textColor = NCBrandColor.sharedInstance.textView
  243. }
  244. return cell
  245. } else {
  246. // Menu Normal
  247. if (indexPath.section == 1) {
  248. item = functionMenu[indexPath.row]
  249. }
  250. // Menu External Site
  251. if (numberOfSections(in: tableView) == 4 && indexPath.section == 2) {
  252. item = externalSiteMenu[indexPath.row]
  253. }
  254. // Menu Settings
  255. if ((numberOfSections(in: tableView) == 3 && indexPath.section == 2) || (numberOfSections(in: tableView) == 4 && indexPath.section == 3)) {
  256. item = settingsMenu[indexPath.row]
  257. }
  258. cell.imageIcon?.image = CCGraphics.changeThemingColorImage(UIImage.init(named: item.icon), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon)
  259. cell.labelText?.text = NSLocalizedString(item.name, comment: "")
  260. cell.labelText.textColor = NCBrandColor.sharedInstance.textView
  261. }
  262. return cell
  263. }
  264. // method to run when table view cell is tapped
  265. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  266. var item = NCCommunicationExternalSite()
  267. if indexPath.section == 0 {
  268. tapImageLogoManageAccount()
  269. return
  270. }
  271. // Menu Function
  272. if indexPath.section == 1 {
  273. item = functionMenu[indexPath.row]
  274. }
  275. // Menu External Site
  276. if (numberOfSections(in: tableView) == 4 && indexPath.section == 2) {
  277. item = externalSiteMenu[indexPath.row]
  278. }
  279. // Menu Settings
  280. if ((numberOfSections(in: tableView) == 3 && indexPath.section == 2) || (numberOfSections(in: tableView) == 4 && indexPath.section == 3)) {
  281. item = settingsMenu[indexPath.row]
  282. }
  283. // Action
  284. if item.url.contains("segue") && !item.url.contains("//") {
  285. self.navigationController?.performSegue(withIdentifier: item.url, sender: self)
  286. } else if item.url.contains("openStoryboard") && !item.url.contains("//") {
  287. let nameStoryboard = item.url.replacingOccurrences(of: "openStoryboard", with: "")
  288. let storyboard = UIStoryboard(name: nameStoryboard, bundle: nil)
  289. let controller = storyboard.instantiateInitialViewController()! //instantiateViewController(withIdentifier: nameStoryboard)
  290. self.present(controller, animated: true, completion: nil)
  291. } else if item.url.contains("//") {
  292. let browserWebVC = UIStoryboard(name: "NCBrowserWeb", bundle: nil).instantiateInitialViewController() as! NCBrowserWeb
  293. browserWebVC.urlBase = item.url
  294. browserWebVC.isHiddenButtonExit = true
  295. self.navigationController?.pushViewController(browserWebVC, animated: true)
  296. self.navigationController?.navigationBar.isHidden = false
  297. } else if item.url == "logout" {
  298. let alertController = UIAlertController(title: "", message: NSLocalizedString("_want_delete_", comment: ""), preferredStyle: .alert)
  299. let actionYes = UIAlertAction(title: NSLocalizedString("_yes_delete_", comment: ""), style: .default) { (action: UIAlertAction) in
  300. let manageAccount = CCManageAccount()
  301. manageAccount.delete(self.appDelegate.account)
  302. self.appDelegate.openLoginView(self, selector: Int(k_intro_login), openLoginWeb: false)
  303. }
  304. let actionNo = UIAlertAction(title: NSLocalizedString("_no_delete_", comment: ""), style: .default) { (action: UIAlertAction) in
  305. print("You've pressed No button")
  306. }
  307. alertController.addAction(actionYes)
  308. alertController.addAction(actionNo)
  309. self.present(alertController, animated: true, completion: nil)
  310. }
  311. }
  312. @objc func tapLabelQuotaExternalSite() {
  313. if (quotaMenu.count > 0) {
  314. let item = quotaMenu[0]
  315. let browserWebVC = UIStoryboard(name: "NCBrowserWeb", bundle: nil).instantiateInitialViewController() as! NCBrowserWeb
  316. browserWebVC.urlBase = item.url
  317. browserWebVC.isHiddenButtonExit = true
  318. self.navigationController?.pushViewController(browserWebVC, animated: true)
  319. self.navigationController?.navigationBar.isHidden = false
  320. }
  321. }
  322. @objc func tapImageLogoManageAccount() {
  323. let controller = CCManageAccount.init()
  324. self.navigationController?.pushViewController(controller, animated: true)
  325. }
  326. }
  327. class CCCellMore: UITableViewCell {
  328. @IBOutlet weak var labelText: UILabel!
  329. @IBOutlet weak var imageIcon: UIImageView!
  330. }