NCMore.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. var moreAppsMenu: [NKExternalSite] = []
  32. var functionMenu: [NKExternalSite] = []
  33. var externalSiteMenu: [NKExternalSite] = []
  34. var settingsMenu: [NKExternalSite] = []
  35. var quotaMenu: [NKExternalSite] = []
  36. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  37. let applicationHandle = NCApplicationHandle()
  38. var tabAccount: tableAccount?
  39. // MARK: - View Life Cycle
  40. override func viewDidLoad() {
  41. super.viewDidLoad()
  42. self.navigationItem.title = NSLocalizedString("_more_", comment: "")
  43. view.backgroundColor = .systemGroupedBackground
  44. tableView.insetsContentViewsToSafeArea = false
  45. tableView.delegate = self
  46. tableView.dataSource = self
  47. tableView.backgroundColor = .systemGroupedBackground
  48. tableView.register(NCMoreUserCell.fromNib(), forCellReuseIdentifier: NCMoreUserCell.reuseIdentifier)
  49. tableView.register(NCMoreAppSuggestionsCell.fromNib(), forCellReuseIdentifier: NCMoreAppSuggestionsCell.reuseIdentifier)
  50. // create tap gesture recognizer
  51. let tapQuota = UITapGestureRecognizer(target: self, action: #selector(tapLabelQuotaExternalSite))
  52. labelQuotaExternalSite.isUserInteractionEnabled = true
  53. labelQuotaExternalSite.addGestureRecognizer(tapQuota)
  54. // Notification
  55. NotificationCenter.default.addObserver(self, selector: #selector(initialize), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterInitialize), object: nil)
  56. }
  57. override func viewWillAppear(_ animated: Bool) {
  58. super.viewWillAppear(animated)
  59. navigationController?.setGroupeAppreance()
  60. appDelegate.activeViewController = self
  61. loadItems()
  62. tableView.reloadData()
  63. }
  64. // MARK: - NotificationCenter
  65. @objc func initialize() {
  66. loadItems()
  67. }
  68. // MARK: -
  69. func loadItems() {
  70. var item = NKExternalSite()
  71. var quota: String = ""
  72. // Clear
  73. moreAppsMenu.removeAll()
  74. functionMenu.removeAll()
  75. externalSiteMenu.removeAll()
  76. settingsMenu.removeAll()
  77. quotaMenu.removeAll()
  78. labelQuotaExternalSite.text = ""
  79. progressQuota.progressTintColor = NCBrandColor.shared.brandElement
  80. // ITEM : More apps
  81. item = NKExternalSite()
  82. moreAppsMenu.append(item)
  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. externalSiteMenu.append(item)
  137. externalSiteMenu.append(item)
  138. // ITEM : Trash
  139. if NCGlobal.shared.capabilityServerVersionMajor >= NCGlobal.shared.nextcloudVersion15 {
  140. item = NKExternalSite()
  141. item.name = "_trash_view_"
  142. item.icon = "trash"
  143. item.url = "segueTrash"
  144. item.order = 80
  145. functionMenu.append(item)
  146. }
  147. // ITEM : HANDLE
  148. applicationHandle.loadItems(functionMenu: &functionMenu)
  149. // ORDER ITEM
  150. functionMenu = functionMenu.sorted(by: { $0.order < $1.order })
  151. // ITEM : Settings
  152. item = NKExternalSite()
  153. item.name = "_settings_"
  154. item.icon = "gear"
  155. item.url = "segueSettings"
  156. settingsMenu.append(item)
  157. if quotaMenu.count > 0 {
  158. let item = quotaMenu[0]
  159. labelQuotaExternalSite.text = item.name
  160. }
  161. // Display Name user & Quota
  162. if let activeAccount = NCManageDatabase.shared.getActiveAccount() {
  163. self.tabAccount = activeAccount
  164. if activeAccount.quotaRelative > 0 {
  165. progressQuota.progress = Float(activeAccount.quotaRelative) / 100
  166. } else {
  167. progressQuota.progress = 0
  168. }
  169. switch activeAccount.quotaTotal {
  170. case -1:
  171. quota = "0"
  172. case -2:
  173. quota = NSLocalizedString("_quota_space_unknown_", comment: "")
  174. case -3:
  175. quota = NSLocalizedString("_quota_space_unlimited_", comment: "")
  176. default:
  177. quota = CCUtility.transformedSize(activeAccount.quotaTotal)
  178. }
  179. let quotaUsed: String = CCUtility.transformedSize(activeAccount.quotaUsed)
  180. labelQuota.text = String.localizedStringWithFormat(NSLocalizedString("_quota_using_", comment: ""), quotaUsed, quota)
  181. }
  182. // ITEM : External
  183. if NCBrandOptions.shared.disable_more_external_site == false {
  184. if let externalSites = NCManageDatabase.shared.getAllExternalSites(account: appDelegate.account) {
  185. for externalSite in externalSites {
  186. if (externalSite.name != "" && externalSite.url != ""), let urlEncoded = externalSite.url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
  187. item = NKExternalSite()
  188. item.name = externalSite.name
  189. item.url = urlEncoded
  190. item.icon = "network"
  191. if externalSite.type == "settings" {
  192. item.icon = "gear"
  193. }
  194. externalSiteMenu.append(item)
  195. }
  196. }
  197. }
  198. }
  199. }
  200. // MARK: - Action
  201. @objc func tapLabelQuotaExternalSite() {
  202. if quotaMenu.count > 0 {
  203. let item = quotaMenu[0]
  204. let browserWebVC = UIStoryboard(name: "NCBrowserWeb", bundle: nil).instantiateInitialViewController() as! NCBrowserWeb
  205. browserWebVC.urlBase = item.url
  206. browserWebVC.isHiddenButtonExit = true
  207. self.navigationController?.pushViewController(browserWebVC, animated: true)
  208. self.navigationController?.navigationBar.isHidden = false
  209. }
  210. }
  211. @objc func tapImageLogoManageAccount() {
  212. let controller = CCManageAccount()
  213. self.navigationController?.pushViewController(controller, animated: true)
  214. }
  215. // MARK: -
  216. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  217. if indexPath.section == 0 {
  218. return 75
  219. } else {
  220. return NCGlobal.shared.heightCellSettings
  221. }
  222. }
  223. func numberOfSections(in tableView: UITableView) -> Int {
  224. if externalSiteMenu.isEmpty {
  225. return 4
  226. } else {
  227. return 5
  228. }
  229. }
  230. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  231. if section == 0 {
  232. return 10
  233. } else if section == 1 || section == 2 {
  234. return 1
  235. } else {
  236. return 20
  237. }
  238. }
  239. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  240. var cont = 0
  241. if section == 0 {
  242. cont = tabAccount == nil ? 0 : 1
  243. } else if section == 1 {
  244. // Menu Normal
  245. cont = moreAppsMenu.count
  246. } else if section == 2 {
  247. cont = functionMenu.count
  248. } else if section == 3 {
  249. cont = numberOfSections(in: tableView) == 4 ? settingsMenu.count : externalSiteMenu.count
  250. } else if section == 4 {
  251. cont = settingsMenu.count
  252. // } else {
  253. // switch numberOfSections(in: tableView) {
  254. // case 4:
  255. // // Menu Settings
  256. // if section == 3 {
  257. // cont = settingsMenu.count
  258. // }
  259. // case 5:
  260. // // Menu External Site
  261. // if section == 3 {
  262. // cont = externalSiteMenu.count
  263. // }
  264. // // Menu Settings
  265. // if section == 4 {
  266. // cont = settingsMenu.count
  267. // }
  268. // default:
  269. // cont = 0
  270. }
  271. return cont
  272. }
  273. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  274. var item = NKExternalSite()
  275. if indexPath.section == 0 {
  276. let cell = tableView.dequeueReusableCell(withIdentifier: NCMoreUserCell.reuseIdentifier, for: indexPath) as! NCMoreUserCell
  277. cell.avatar.image = nil
  278. cell.icon.image = nil
  279. cell.status.text = ""
  280. cell.displayName.text = ""
  281. if let account = tabAccount {
  282. cell.avatar.image = NCUtility.shared.loadUserImage(
  283. for: account.user,
  284. displayName: account.displayName,
  285. userBaseUrl: appDelegate)
  286. if account.alias == "" {
  287. cell.displayName?.text = account.displayName
  288. } else {
  289. cell.displayName?.text = account.displayName + " (" + account.alias + ")"
  290. }
  291. cell.displayName.textColor = .label
  292. }
  293. cell.accessoryType = UITableViewCell.AccessoryType.disclosureIndicator
  294. if NCGlobal.shared.capabilityUserStatusEnabled, let account = NCManageDatabase.shared.getAccount(predicate: NSPredicate(format: "account == %@", appDelegate.account)) {
  295. let status = NCUtility.shared.getUserStatus(userIcon: account.userStatusIcon, userStatus: account.userStatusStatus, userMessage: account.userStatusMessage)
  296. cell.icon.image = status.onlineStatus
  297. cell.status.text = status.statusMessage
  298. cell.status.textColor = .label
  299. cell.status.trailingBuffer = cell.status.frame.width
  300. if cell.status.labelShouldScroll() {
  301. cell.status.tapToScroll = true
  302. } else {
  303. cell.status.tapToScroll = false
  304. }
  305. }
  306. cell.layer.maskedCorners = [.layerMaxXMinYCorner, .layerMinXMinYCorner, .layerMaxXMaxYCorner, .layerMinXMaxYCorner]
  307. return cell
  308. } else if indexPath.section == 1 {
  309. let cell = tableView.dequeueReusableCell(withIdentifier: NCMoreAppSuggestionsCell.reuseIdentifier, for: indexPath) as! NCMoreAppSuggestionsCell
  310. return cell
  311. } else {
  312. let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CCCellMore
  313. // Menu Normal
  314. if indexPath.section == 2 {
  315. item = functionMenu[indexPath.row]
  316. }
  317. // Menu External Site
  318. if numberOfSections(in: tableView) == 5 && indexPath.section == 3 {
  319. item = externalSiteMenu[indexPath.row]
  320. }
  321. // Menu Settings
  322. if (numberOfSections(in: tableView) == 4 && indexPath.section == 3) || (numberOfSections(in: tableView) == 5 && indexPath.section == 4) {
  323. item = settingsMenu[indexPath.row]
  324. }
  325. cell.imageIcon?.image = NCUtility.shared.loadImage(named: item.icon)
  326. cell.imageIcon?.contentMode = .scaleAspectFit
  327. cell.labelText?.text = NSLocalizedString(item.name, comment: "")
  328. cell.labelText.textColor = .label
  329. cell.accessoryType = UITableViewCell.AccessoryType.disclosureIndicator
  330. cell.separator.backgroundColor = .separator
  331. cell.separatorHeigth.constant = 0.4
  332. let rows = tableView.numberOfRows(inSection: indexPath.section)
  333. if indexPath.row == 0 {
  334. if indexPath.row == rows - 1 {
  335. cell.separator.backgroundColor = .clear
  336. cell.layer.maskedCorners = [.layerMaxXMinYCorner, .layerMinXMinYCorner, .layerMaxXMaxYCorner, .layerMinXMaxYCorner]
  337. } else {
  338. cell.layer.maskedCorners = [.layerMaxXMinYCorner, .layerMinXMinYCorner]
  339. }
  340. } else if indexPath.row == rows - 1 {
  341. cell.layer.maskedCorners = [.layerMaxXMaxYCorner, .layerMinXMaxYCorner]
  342. cell.separator.backgroundColor = .clear
  343. } else {
  344. cell.layer.cornerRadius = 0
  345. }
  346. return cell
  347. }
  348. }
  349. // method to run when table view cell is tapped
  350. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  351. var item = NKExternalSite()
  352. // Menu Function
  353. if indexPath.section == 0 {
  354. tapImageLogoManageAccount()
  355. return
  356. }
  357. // Menu More Apps
  358. if indexPath.section == 1 {
  359. item = moreAppsMenu[indexPath.row]
  360. }
  361. // Menu Function
  362. if indexPath.section == 2 {
  363. item = functionMenu[indexPath.row]
  364. }
  365. // Menu External Site
  366. if numberOfSections(in: tableView) == 5 && indexPath.section == 3 {
  367. item = externalSiteMenu[indexPath.row]
  368. }
  369. // Menu Settings
  370. if (numberOfSections(in: tableView) == 4 && indexPath.section == 3) || (numberOfSections(in: tableView) == 5 && indexPath.section == 4) {
  371. item = settingsMenu[indexPath.row]
  372. }
  373. // Action
  374. if item.url.contains("segue") && !item.url.contains("//") {
  375. self.navigationController?.performSegue(withIdentifier: item.url, sender: self)
  376. } else if item.url.contains("openStoryboard") && !item.url.contains("//") {
  377. let nameStoryboard = item.url.replacingOccurrences(of: "openStoryboard", with: "")
  378. let storyboard = UIStoryboard(name: nameStoryboard, bundle: nil)
  379. if let controller = storyboard.instantiateInitialViewController() {
  380. controller.modalPresentationStyle = UIModalPresentationStyle.pageSheet
  381. present(controller, animated: true, completion: nil)
  382. }
  383. } else if item.url.contains("//") {
  384. let browserWebVC = UIStoryboard(name: "NCBrowserWeb", bundle: nil).instantiateInitialViewController() as! NCBrowserWeb
  385. browserWebVC.urlBase = item.url
  386. browserWebVC.isHiddenButtonExit = true
  387. browserWebVC.titleBrowser = item.name
  388. self.navigationController?.pushViewController(browserWebVC, animated: true)
  389. self.navigationController?.navigationBar.isHidden = false
  390. } else if item.url == "logout" {
  391. let alertController = UIAlertController(title: "", message: NSLocalizedString("_want_delete_", comment: ""), preferredStyle: .alert)
  392. let actionYes = UIAlertAction(title: NSLocalizedString("_yes_delete_", comment: ""), style: .default) { (_: UIAlertAction) in
  393. let manageAccount = CCManageAccount()
  394. manageAccount.delete(self.appDelegate.account)
  395. self.appDelegate.openLogin(viewController: self, selector: NCGlobal.shared.introLogin, openLoginWeb: false)
  396. }
  397. let actionNo = UIAlertAction(title: NSLocalizedString("_no_delete_", comment: ""), style: .default) { (_: UIAlertAction) in
  398. print("You've pressed No button")
  399. }
  400. alertController.addAction(actionYes)
  401. alertController.addAction(actionNo)
  402. self.present(alertController, animated: true, completion: nil)
  403. } else {
  404. applicationHandle.didSelectItem(item, viewController: self)
  405. }
  406. }
  407. }