CCMore.swift 15 KB

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