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