NCMore.swift 15 KB

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