NCMore.swift 16 KB

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