NCMore.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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.delegate = self
  45. tableView.dataSource = self
  46. tableView.backgroundColor = .systemGroupedBackground
  47. tableView.register(NCMoreUserCell.fromNib(), forCellReuseIdentifier: NCMoreUserCell.reuseIdentifier)
  48. tableView.register(NCMoreAppSuggestionsCell.fromNib(), forCellReuseIdentifier: NCMoreAppSuggestionsCell.reuseIdentifier)
  49. // create tap gesture recognizer
  50. let tapQuota = UITapGestureRecognizer(target: self, action: #selector(tapLabelQuotaExternalSite))
  51. labelQuotaExternalSite.isUserInteractionEnabled = true
  52. labelQuotaExternalSite.addGestureRecognizer(tapQuota)
  53. // Notification
  54. NotificationCenter.default.addObserver(self, selector: #selector(initialize), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterInitialize), object: nil)
  55. }
  56. override func viewWillAppear(_ animated: Bool) {
  57. super.viewWillAppear(animated)
  58. navigationController?.setGroupeAppreance()
  59. appDelegate.activeViewController = self
  60. loadItems()
  61. tableView.reloadData()
  62. }
  63. // MARK: - NotificationCenter
  64. @objc func initialize() {
  65. loadItems()
  66. }
  67. // MARK: -
  68. func loadItems() {
  69. var item = NKExternalSite()
  70. var quota: String = ""
  71. // Clear
  72. moreAppsMenu.removeAll()
  73. functionMenu.removeAll()
  74. externalSiteMenu.removeAll()
  75. settingsMenu.removeAll()
  76. quotaMenu.removeAll()
  77. labelQuotaExternalSite.text = ""
  78. progressQuota.progressTintColor = NCBrandColor.shared.brandElement
  79. // ITEM : More apps
  80. item = NKExternalSite()
  81. moreAppsMenu.append(item)
  82. // ITEM : Transfer
  83. item = NKExternalSite()
  84. item.name = "_transfers_"
  85. item.icon = "arrow.left.arrow.right"
  86. item.url = "segueTransfers"
  87. item.order = 10
  88. functionMenu.append(item)
  89. // ITEM : Recent
  90. item = NKExternalSite()
  91. item.name = "_recent_"
  92. item.icon = "clock.arrow.circlepath"
  93. item.url = "segueRecent"
  94. item.order = 20
  95. functionMenu.append(item)
  96. // ITEM : Activity
  97. item = NKExternalSite()
  98. item.name = "_activity_"
  99. item.icon = "bolt"
  100. item.url = "segueActivity"
  101. item.order = 40
  102. functionMenu.append(item)
  103. // ITEM : Shares
  104. if NCGlobal.shared.capabilityFileSharingApiEnabled {
  105. item = NKExternalSite()
  106. item.name = "_list_shares_"
  107. item.icon = "share"
  108. item.url = "segueShares"
  109. item.order = 50
  110. functionMenu.append(item)
  111. }
  112. // ITEM : Offline
  113. item = NKExternalSite()
  114. item.name = "_manage_file_offline_"
  115. item.icon = "tray.and.arrow.down"
  116. item.url = "segueOffline"
  117. item.order = 60
  118. functionMenu.append(item)
  119. // ITEM : Groupfolders
  120. if NCGlobal.shared.capabilityGroupfoldersEnabled {
  121. item = NKExternalSite()
  122. item.name = "_group_folders_"
  123. item.icon = "person.2"
  124. item.url = "segueGroupfolders"
  125. item.order = 61
  126. functionMenu.append(item)
  127. }
  128. // ITEM : Scan
  129. item = NKExternalSite()
  130. item.name = "_scanned_images_"
  131. item.icon = "doc.text.viewfinder"
  132. item.url = "openStoryboardNCScan"
  133. item.order = 70
  134. functionMenu.append(item)
  135. // ITEM : Trash
  136. if NCGlobal.shared.capabilityServerVersionMajor >= NCGlobal.shared.nextcloudVersion15 {
  137. item = NKExternalSite()
  138. item.name = "_trash_view_"
  139. item.icon = "trash"
  140. item.url = "segueTrash"
  141. item.order = 80
  142. functionMenu.append(item)
  143. }
  144. // ITEM : HANDLE
  145. applicationHandle.loadItems(functionMenu: &functionMenu)
  146. // ORDER ITEM
  147. functionMenu = functionMenu.sorted(by: { $0.order < $1.order })
  148. // ITEM : Settings
  149. item = NKExternalSite()
  150. item.name = "_settings_"
  151. item.icon = "gear"
  152. item.url = "segueSettings"
  153. settingsMenu.append(item)
  154. if quotaMenu.count > 0 {
  155. let item = quotaMenu[0]
  156. labelQuotaExternalSite.text = item.name
  157. }
  158. // Display Name user & Quota
  159. if let activeAccount = NCManageDatabase.shared.getActiveAccount() {
  160. self.tabAccount = activeAccount
  161. if activeAccount.quotaRelative > 0 {
  162. progressQuota.progress = Float(activeAccount.quotaRelative) / 100
  163. } else {
  164. progressQuota.progress = 0
  165. }
  166. switch activeAccount.quotaTotal {
  167. case -1:
  168. quota = "0"
  169. case -2:
  170. quota = NSLocalizedString("_quota_space_unknown_", comment: "")
  171. case -3:
  172. quota = NSLocalizedString("_quota_space_unlimited_", comment: "")
  173. default:
  174. quota = CCUtility.transformedSize(activeAccount.quotaTotal)
  175. }
  176. let quotaUsed: String = CCUtility.transformedSize(activeAccount.quotaUsed)
  177. labelQuota.text = String.localizedStringWithFormat(NSLocalizedString("_quota_using_", comment: ""), quotaUsed, quota)
  178. }
  179. // ITEM : External
  180. if NCBrandOptions.shared.disable_more_external_site == false {
  181. if let externalSites = NCManageDatabase.shared.getAllExternalSites(account: appDelegate.account) {
  182. for externalSite in externalSites {
  183. if (externalSite.name != "" && externalSite.url != ""), let urlEncoded = externalSite.url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
  184. item = NKExternalSite()
  185. item.name = externalSite.name
  186. item.url = urlEncoded
  187. item.icon = "network"
  188. if externalSite.type == "settings" {
  189. item.icon = "gear"
  190. }
  191. externalSiteMenu.append(item)
  192. }
  193. }
  194. }
  195. }
  196. }
  197. // MARK: - Action
  198. @objc func tapLabelQuotaExternalSite() {
  199. if quotaMenu.count > 0 {
  200. let item = quotaMenu[0]
  201. let browserWebVC = UIStoryboard(name: "NCBrowserWeb", bundle: nil).instantiateInitialViewController() as! NCBrowserWeb
  202. browserWebVC.urlBase = item.url
  203. browserWebVC.isHiddenButtonExit = true
  204. self.navigationController?.pushViewController(browserWebVC, animated: true)
  205. self.navigationController?.navigationBar.isHidden = false
  206. }
  207. }
  208. @objc func tapImageLogoManageAccount() {
  209. let controller = CCManageAccount()
  210. self.navigationController?.pushViewController(controller, animated: true)
  211. }
  212. // MARK: -
  213. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  214. if indexPath.section == 0 {
  215. return 75
  216. } else {
  217. return NCGlobal.shared.heightCellSettings
  218. }
  219. }
  220. func numberOfSections(in tableView: UITableView) -> Int {
  221. let defaultSections = 4
  222. if externalSiteMenu.count == 0 {
  223. return defaultSections
  224. } else {
  225. return defaultSections + 1
  226. }
  227. }
  228. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  229. if section == 0 {
  230. return 10
  231. } else if section == 1 || section == 2 {
  232. return 1
  233. } else {
  234. return 20
  235. }
  236. }
  237. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  238. var count = 0
  239. if section == 0 {
  240. count = tabAccount == nil ? 0 : 1
  241. } else if section == 1 {
  242. // Menu More apps
  243. count = moreAppsMenu.count
  244. } else if section == 2 {
  245. // Menu function
  246. count = functionMenu.count
  247. } else {
  248. switch numberOfSections(in: tableView) {
  249. case 3:
  250. // Menu Settings
  251. if section == 3 {
  252. count = settingsMenu.count
  253. }
  254. case 4:
  255. // Menu External Site
  256. if section == 4 {
  257. count = externalSiteMenu.count
  258. }
  259. // Menu Settings
  260. if section == 5 {
  261. count = settingsMenu.count
  262. }
  263. default:
  264. count = 0
  265. }
  266. }
  267. return count
  268. }
  269. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  270. var item = NKExternalSite()
  271. if indexPath.section == 0 {
  272. let cell = tableView.dequeueReusableCell(withIdentifier: NCMoreUserCell.reuseIdentifier, for: indexPath) as! NCMoreUserCell
  273. cell.avatar.image = nil
  274. cell.icon.image = nil
  275. cell.status.text = ""
  276. cell.displayName.text = ""
  277. if let account = tabAccount {
  278. cell.avatar.image = NCUtility.shared.loadUserImage(
  279. for: account.user,
  280. displayName: account.displayName,
  281. userBaseUrl: appDelegate)
  282. if account.alias == "" {
  283. cell.displayName?.text = account.displayName
  284. } else {
  285. cell.displayName?.text = account.displayName + " (" + account.alias + ")"
  286. }
  287. cell.displayName.textColor = .label
  288. }
  289. cell.accessoryType = UITableViewCell.AccessoryType.disclosureIndicator
  290. if NCGlobal.shared.capabilityUserStatusEnabled, let account = NCManageDatabase.shared.getAccount(predicate: NSPredicate(format: "account == %@", appDelegate.account)) {
  291. let status = NCUtility.shared.getUserStatus(userIcon: account.userStatusIcon, userStatus: account.userStatusStatus, userMessage: account.userStatusMessage)
  292. cell.icon.image = status.onlineStatus
  293. cell.status.text = status.statusMessage
  294. cell.status.textColor = .label
  295. cell.status.trailingBuffer = cell.status.frame.width
  296. if cell.status.labelShouldScroll() {
  297. cell.status.tapToScroll = true
  298. } else {
  299. cell.status.tapToScroll = false
  300. }
  301. }
  302. cell.layer.maskedCorners = [.layerMaxXMinYCorner, .layerMinXMinYCorner, .layerMaxXMaxYCorner, .layerMinXMaxYCorner]
  303. return cell
  304. } else if indexPath.section == 1 {
  305. let cell = tableView.dequeueReusableCell(withIdentifier: NCMoreAppSuggestionsCell.reuseIdentifier, for: indexPath) as! NCMoreAppSuggestionsCell
  306. return cell
  307. } else {
  308. let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CCCellMore
  309. // Menu More Apps
  310. if indexPath.section == 1 {
  311. item = moreAppsMenu[indexPath.row]
  312. }
  313. // Menu Function
  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. }