NCMore.swift 17 KB

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