NCMore.swift 17 KB

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