NCMore.swift 15 KB

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