NCMore.swift 16 KB

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