CCMore.swift 15 KB

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