NCMore.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. //
  2. // NCMore.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 NextcloudKit
  25. class NCMore: 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. private var functionMenu: [NKExternalSite] = []
  32. private var externalSiteMenu: [NKExternalSite] = []
  33. private var settingsMenu: [NKExternalSite] = []
  34. private var quotaMenu: [NKExternalSite] = []
  35. // swiftlint:disable force_cast
  36. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  37. // swiftlint:enable force_cast
  38. private let applicationHandle = NCApplicationHandle()
  39. private var tabAccount: tableAccount?
  40. private struct Section {
  41. var items: [NKExternalSite]
  42. var type: SectionType
  43. enum SectionType {
  44. case account
  45. case moreApps
  46. case regular
  47. }
  48. }
  49. private var sections: [Section] = []
  50. // MARK: - View Life Cycle
  51. override func viewDidLoad() {
  52. super.viewDidLoad()
  53. self.navigationItem.title = NSLocalizedString("_more_", comment: "")
  54. view.backgroundColor = .systemGroupedBackground
  55. tableView.insetsContentViewsToSafeArea = false
  56. tableView.delegate = self
  57. tableView.dataSource = self
  58. tableView.backgroundColor = .systemGroupedBackground
  59. tableView.register(NCMoreUserCell.fromNib(), forCellReuseIdentifier: NCMoreUserCell.reuseIdentifier)
  60. tableView.register(NCMoreAppSuggestionsCell.fromNib(), forCellReuseIdentifier: NCMoreAppSuggestionsCell.reuseIdentifier)
  61. // create tap gesture recognizer
  62. let tapQuota = UITapGestureRecognizer(target: self, action: #selector(tapLabelQuotaExternalSite))
  63. labelQuotaExternalSite.isUserInteractionEnabled = true
  64. labelQuotaExternalSite.addGestureRecognizer(tapQuota)
  65. }
  66. override func viewWillAppear(_ animated: Bool) {
  67. super.viewWillAppear(animated)
  68. navigationController?.setGroupAppearance()
  69. appDelegate.activeViewController = self
  70. loadItems()
  71. tableView.reloadData()
  72. }
  73. // MARK: -
  74. func loadItems() {
  75. var item = NKExternalSite()
  76. var quota: String = ""
  77. // Clear
  78. functionMenu.removeAll()
  79. externalSiteMenu.removeAll()
  80. settingsMenu.removeAll()
  81. quotaMenu.removeAll()
  82. sections.removeAll()
  83. labelQuotaExternalSite.text = ""
  84. progressQuota.progressTintColor = NCBrandColor.shared.brandElement
  85. // ITEM : Transfer
  86. item = NKExternalSite()
  87. item.name = "_transfers_"
  88. item.icon = "arrow.left.arrow.right"
  89. item.url = "segueTransfers"
  90. item.order = 10
  91. functionMenu.append(item)
  92. // ITEM : Recent
  93. item = NKExternalSite()
  94. item.name = "_recent_"
  95. item.icon = "clock.arrow.circlepath"
  96. item.url = "segueRecent"
  97. item.order = 20
  98. functionMenu.append(item)
  99. // ITEM : Activity
  100. item = NKExternalSite()
  101. item.name = "_activity_"
  102. item.icon = "bolt"
  103. item.url = "segueActivity"
  104. item.order = 40
  105. functionMenu.append(item)
  106. // ITEM : Shares
  107. if NCGlobal.shared.capabilityFileSharingApiEnabled {
  108. item = NKExternalSite()
  109. item.name = "_list_shares_"
  110. item.icon = "share"
  111. item.url = "segueShares"
  112. item.order = 50
  113. functionMenu.append(item)
  114. }
  115. // ITEM : Offline
  116. item = NKExternalSite()
  117. item.name = "_manage_file_offline_"
  118. item.icon = "tray.and.arrow.down"
  119. item.url = "segueOffline"
  120. item.order = 60
  121. functionMenu.append(item)
  122. // ITEM : Groupfolders
  123. if NCGlobal.shared.capabilityGroupfoldersEnabled {
  124. item = NKExternalSite()
  125. item.name = "_group_folders_"
  126. item.icon = "person.2"
  127. item.url = "segueGroupfolders"
  128. item.order = 61
  129. functionMenu.append(item)
  130. }
  131. // ITEM : Scan
  132. item = NKExternalSite()
  133. item.name = "_scanned_images_"
  134. item.icon = "doc.text.viewfinder"
  135. item.url = "openStoryboardNCScan"
  136. item.order = 70
  137. functionMenu.append(item)
  138. // ITEM : Trash
  139. if NCGlobal.shared.capabilityServerVersionMajor >= NCGlobal.shared.nextcloudVersion15 {
  140. item = NKExternalSite()
  141. item.name = "_trash_view_"
  142. item.icon = "trash"
  143. item.url = "segueTrash"
  144. item.order = 80
  145. functionMenu.append(item)
  146. }
  147. // ITEM : HANDLE
  148. applicationHandle.loadItems(functionMenu: &functionMenu)
  149. // ORDER ITEM
  150. functionMenu = functionMenu.sorted(by: { $0.order < $1.order })
  151. // ITEM : Settings
  152. item = NKExternalSite()
  153. item.name = "_settings_"
  154. item.icon = "gear"
  155. item.url = "segueSettings"
  156. settingsMenu.append(item)
  157. if !quotaMenu.isEmpty {
  158. let item = quotaMenu[0]
  159. labelQuotaExternalSite.text = item.name
  160. }
  161. // Display Name user & Quota
  162. if let activeAccount = NCManageDatabase.shared.getActiveAccount() {
  163. self.tabAccount = activeAccount
  164. if activeAccount.quotaRelative > 0 {
  165. progressQuota.progress = Float(activeAccount.quotaRelative) / 100
  166. } else {
  167. progressQuota.progress = 0
  168. }
  169. switch activeAccount.quotaTotal {
  170. case -1:
  171. quota = "0"
  172. case -2:
  173. quota = NSLocalizedString("_quota_space_unknown_", comment: "")
  174. case -3:
  175. quota = NSLocalizedString("_quota_space_unlimited_", comment: "")
  176. default:
  177. quota = CCUtility.transformedSize(activeAccount.quotaTotal)
  178. }
  179. let quotaUsed: String = CCUtility.transformedSize(activeAccount.quotaUsed)
  180. labelQuota.text = String.localizedStringWithFormat(NSLocalizedString("_quota_using_", comment: ""), quotaUsed, quota)
  181. }
  182. // ITEM : External
  183. if NCBrandOptions.shared.disable_more_external_site == false {
  184. if let externalSites = NCManageDatabase.shared.getAllExternalSites(account: appDelegate.account) {
  185. for externalSite in externalSites {
  186. if !externalSite.name.isEmpty, !externalSite.url.isEmpty, let urlEncoded = externalSite.url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
  187. item = NKExternalSite()
  188. item.name = externalSite.name
  189. item.url = urlEncoded
  190. item.icon = "network"
  191. if externalSite.type == "settings" {
  192. item.icon = "gear"
  193. }
  194. externalSiteMenu.append(item)
  195. }
  196. }
  197. }
  198. }
  199. loadSections()
  200. }
  201. private func loadSections() {
  202. if tabAccount != nil {
  203. sections.append(Section(items: [NKExternalSite()], type: .account))
  204. }
  205. if !NCBrandOptions.shared.disable_show_more_nextcloud_apps_in_settings {
  206. sections.append(Section(items: [NKExternalSite()], type: .moreApps))
  207. }
  208. if !functionMenu.isEmpty {
  209. sections.append(Section(items: functionMenu, type: .regular))
  210. }
  211. if !externalSiteMenu.isEmpty {
  212. sections.append(Section(items: externalSiteMenu, type: .regular))
  213. }
  214. if !settingsMenu.isEmpty {
  215. sections.append(Section(items: settingsMenu, type: .regular))
  216. }
  217. }
  218. // MARK: - Action
  219. @objc func tapLabelQuotaExternalSite() {
  220. if !quotaMenu.isEmpty {
  221. let item = quotaMenu[0]
  222. if let browserWebVC = UIStoryboard(name: "NCBrowserWeb", bundle: nil).instantiateInitialViewController() as? NCBrowserWeb {
  223. browserWebVC.urlBase = item.url
  224. browserWebVC.isHiddenButtonExit = true
  225. self.navigationController?.pushViewController(browserWebVC, animated: true)
  226. self.navigationController?.navigationBar.isHidden = false
  227. }
  228. }
  229. }
  230. @objc func tapImageLogoManageAccount() {
  231. let controller = CCManageAccount()
  232. self.navigationController?.pushViewController(controller, animated: true)
  233. }
  234. // MARK: -
  235. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  236. if sections[indexPath.section].type == .account {
  237. return 75
  238. } else {
  239. return NCGlobal.shared.heightCellSettings
  240. }
  241. }
  242. func numberOfSections(in tableView: UITableView) -> Int {
  243. return sections.count
  244. }
  245. func tableView(_ tableView: UITableView, heightForHeaderInSection index: Int) -> CGFloat {
  246. let section = sections[index]
  247. if section.type == .account {
  248. return 10
  249. } else if section.type == .moreApps || sections[index - 1].type == .moreApps {
  250. return 1
  251. } else {
  252. return 20
  253. }
  254. }
  255. func tableView(_ tableView: UITableView, numberOfRowsInSection index: Int) -> Int {
  256. return sections[index].items.count
  257. }
  258. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  259. let section = sections[indexPath.section]
  260. if section.type == .account {
  261. guard let cell = tableView.dequeueReusableCell(withIdentifier: NCMoreUserCell.reuseIdentifier, for: indexPath) as? NCMoreUserCell else { return UITableViewCell() }
  262. cell.avatar.image = nil
  263. cell.icon.image = nil
  264. cell.status.text = ""
  265. cell.displayName.text = ""
  266. if let account = tabAccount {
  267. cell.avatar.image = NCUtility.shared.loadUserImage(for: account.user, displayName: account.displayName, userBaseUrl: appDelegate)
  268. if account.alias.isEmpty {
  269. cell.displayName?.text = account.displayName
  270. } else {
  271. cell.displayName?.text = account.displayName + " (" + account.alias + ")"
  272. }
  273. cell.displayName.textColor = .label
  274. }
  275. cell.accessoryType = UITableViewCell.AccessoryType.disclosureIndicator
  276. if NCGlobal.shared.capabilityUserStatusEnabled, let account = NCManageDatabase.shared.getAccount(predicate: NSPredicate(format: "account == %@", appDelegate.account)) {
  277. let status = NCUtility.shared.getUserStatus(userIcon: account.userStatusIcon, userStatus: account.userStatusStatus, userMessage: account.userStatusMessage)
  278. cell.icon.image = status.onlineStatus
  279. cell.status.text = status.statusMessage
  280. cell.status.textColor = .label
  281. cell.status.trailingBuffer = cell.status.frame.width
  282. if cell.status.labelShouldScroll() {
  283. cell.status.tapToScroll = true
  284. } else {
  285. cell.status.tapToScroll = false
  286. }
  287. }
  288. cell.layer.maskedCorners = [.layerMaxXMinYCorner, .layerMinXMinYCorner, .layerMaxXMaxYCorner, .layerMinXMaxYCorner]
  289. return cell
  290. } else if section.type == .moreApps {
  291. guard let cell = tableView.dequeueReusableCell(withIdentifier: NCMoreAppSuggestionsCell.reuseIdentifier, for: indexPath) as? NCMoreAppSuggestionsCell else { return UITableViewCell() }
  292. return cell
  293. } else {
  294. guard let cell = tableView.dequeueReusableCell(withIdentifier: CCCellMore.reuseIdentifier, for: indexPath) as? CCCellMore else { return UITableViewCell() }
  295. let item = sections[indexPath.section].items[indexPath.row]
  296. cell.imageIcon?.image = NCUtility.shared.loadImage(named: item.icon)
  297. cell.imageIcon?.contentMode = .scaleAspectFit
  298. cell.labelText?.text = NSLocalizedString(item.name, comment: "")
  299. cell.labelText.textColor = .label
  300. cell.accessoryType = UITableViewCell.AccessoryType.disclosureIndicator
  301. cell.separator.backgroundColor = .separator
  302. cell.separatorHeigth.constant = 0.4
  303. cell.removeCornerRadius()
  304. let rows = tableView.numberOfRows(inSection: indexPath.section)
  305. if indexPath.row == 0 {
  306. cell.applyCornerRadius()
  307. if indexPath.row == rows - 1 {
  308. cell.separator.backgroundColor = .clear
  309. cell.layer.maskedCorners = [.layerMaxXMinYCorner, .layerMinXMinYCorner, .layerMaxXMaxYCorner, .layerMinXMaxYCorner]
  310. } else {
  311. cell.layer.maskedCorners = [.layerMaxXMinYCorner, .layerMinXMinYCorner]
  312. }
  313. } else if indexPath.row == rows - 1 {
  314. cell.applyCornerRadius()
  315. cell.layer.maskedCorners = [.layerMaxXMaxYCorner, .layerMinXMaxYCorner]
  316. cell.separator.backgroundColor = .clear
  317. }
  318. return cell
  319. }
  320. }
  321. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  322. let item = sections[indexPath.section].items[indexPath.row]
  323. // Menu Function
  324. if sections[indexPath.section].type == .account {
  325. tapImageLogoManageAccount()
  326. return
  327. }
  328. // Action
  329. if item.url.contains("segue") && !item.url.contains("//") {
  330. self.navigationController?.performSegue(withIdentifier: item.url, sender: self)
  331. } else if item.url.contains("openStoryboard") && !item.url.contains("//") {
  332. let nameStoryboard = item.url.replacingOccurrences(of: "openStoryboard", with: "")
  333. let storyboard = UIStoryboard(name: nameStoryboard, bundle: nil)
  334. if let controller = storyboard.instantiateInitialViewController() {
  335. controller.modalPresentationStyle = UIModalPresentationStyle.pageSheet
  336. present(controller, animated: true, completion: nil)
  337. }
  338. } else if item.url.contains("//") {
  339. if let browserWebVC = UIStoryboard(name: "NCBrowserWeb", bundle: nil).instantiateInitialViewController() as? NCBrowserWeb {
  340. browserWebVC.urlBase = item.url
  341. browserWebVC.isHiddenButtonExit = true
  342. browserWebVC.titleBrowser = item.name
  343. self.navigationController?.pushViewController(browserWebVC, animated: true)
  344. self.navigationController?.navigationBar.isHidden = false
  345. }
  346. } else if item.url == "logout" {
  347. let alertController = UIAlertController(title: "", message: NSLocalizedString("_want_delete_", comment: ""), preferredStyle: .alert)
  348. let actionYes = UIAlertAction(title: NSLocalizedString("_yes_delete_", comment: ""), style: .default) { (_: UIAlertAction) in
  349. let manageAccount = CCManageAccount()
  350. manageAccount.delete(self.appDelegate.account)
  351. self.appDelegate.openLogin(viewController: self, selector: NCGlobal.shared.introLogin, openLoginWeb: false)
  352. }
  353. let actionNo = UIAlertAction(title: NSLocalizedString("_no_delete_", comment: ""), style: .default) { (_: UIAlertAction) in
  354. print("You've pressed No button")
  355. }
  356. alertController.addAction(actionYes)
  357. alertController.addAction(actionNo)
  358. self.present(alertController, animated: true, completion: nil)
  359. } else {
  360. applicationHandle.didSelectItem(item, viewController: self)
  361. }
  362. }
  363. }