NCMore.swift 16 KB

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