NCMore.swift 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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. import MarqueeLabel
  26. class NCMore: UIViewController, UITableViewDelegate, UITableViewDataSource {
  27. @IBOutlet weak var tableView: UITableView!
  28. @IBOutlet weak var labelQuota: UILabel!
  29. @IBOutlet weak var labelQuotaExternalSite: UILabel!
  30. @IBOutlet weak var progressQuota: UIProgressView!
  31. @IBOutlet weak var viewQuota: UIView!
  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 defaultCornerRadius: CGFloat = 10.0
  38. let applicationHandle = NCApplicationHandle()
  39. var tabAccount: tableAccount?
  40. // MARK: - View Life Cycle
  41. override func viewDidLoad() {
  42. super.viewDidLoad()
  43. self.navigationItem.title = NSLocalizedString("_more_", comment: "")
  44. view.backgroundColor = .systemGroupedBackground
  45. tableView.delegate = self
  46. tableView.dataSource = self
  47. tableView.backgroundColor = .systemGroupedBackground
  48. tableView.register(UINib(nibName: "NCMoreUserCell", bundle: nil), forCellReuseIdentifier: "userCell")
  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. let serverVersionMajor = NCManageDatabase.shared.getCapabilitiesServerInt(account: appDelegate.account, elements: NCElementsJSON.shared.capabilitiesVersionMajor)
  72. // Clear
  73. functionMenu.removeAll()
  74. externalSiteMenu.removeAll()
  75. settingsMenu.removeAll()
  76. quotaMenu.removeAll()
  77. labelQuotaExternalSite.text = ""
  78. progressQuota.progressTintColor = NCBrandColor.shared.brandElement
  79. // ITEM : Transfer
  80. item = NKExternalSite()
  81. item.name = "_transfers_"
  82. item.icon = "arrow.left.arrow.right"
  83. item.url = "segueTransfers"
  84. item.order = 10
  85. functionMenu.append(item)
  86. // ITEM : Recent
  87. item = NKExternalSite()
  88. item.name = "_recent_"
  89. item.icon = "recent"
  90. item.url = "segueRecent"
  91. item.order = 20
  92. functionMenu.append(item)
  93. // ITEM : Notification
  94. item = NKExternalSite()
  95. item.name = "_notifications_"
  96. item.icon = "bell"
  97. item.url = "segueNotification"
  98. item.order = 30
  99. functionMenu.append(item)
  100. // ITEM : Activity
  101. item = NKExternalSite()
  102. item.name = "_activity_"
  103. item.icon = "bolt"
  104. item.url = "segueActivity"
  105. item.order = 40
  106. functionMenu.append(item)
  107. // ITEM : Shares
  108. let isFilesSharingEnabled = NCManageDatabase.shared.getCapabilitiesServerBool(account: appDelegate.account, elements: NCElementsJSON.shared.capabilitiesFileSharingApiEnabled, exists: false)
  109. if isFilesSharingEnabled {
  110. item = NKExternalSite()
  111. item.name = "_list_shares_"
  112. item.icon = "share"
  113. item.url = "segueShares"
  114. item.order = 50
  115. functionMenu.append(item)
  116. }
  117. // ITEM : Offline
  118. item = NKExternalSite()
  119. item.name = "_manage_file_offline_"
  120. item.icon = "tray.and.arrow.down"
  121. item.url = "segueOffline"
  122. item.order = 60
  123. functionMenu.append(item)
  124. // ITEM : Groupfolders
  125. let hasAccessibleGroupFolders = NCManageDatabase.shared.getCapabilitiesServerBool(account: appDelegate.account, elements: NCElementsJSON.shared.capabilitiesGroupfoldersEnabled, exists: false)
  126. if serverVersionMajor >= NCGlobal.shared.nextcloudVersion26 && hasAccessibleGroupFolders {
  127. item = NKExternalSite()
  128. item.name = "_group_folders_"
  129. item.icon = "groupfolders"
  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 = "scan"
  138. item.url = "openStoryboardNCScan"
  139. item.order = 70
  140. functionMenu.append(item)
  141. // ITEM : Trash
  142. if serverVersionMajor >= 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. }
  203. // MARK: - Action
  204. @objc func tapLabelQuotaExternalSite() {
  205. if quotaMenu.count > 0 {
  206. let item = quotaMenu[0]
  207. let browserWebVC = UIStoryboard(name: "NCBrowserWeb", bundle: nil).instantiateInitialViewController() as! NCBrowserWeb
  208. browserWebVC.urlBase = item.url
  209. browserWebVC.isHiddenButtonExit = true
  210. self.navigationController?.pushViewController(browserWebVC, animated: true)
  211. self.navigationController?.navigationBar.isHidden = false
  212. }
  213. }
  214. @objc func tapImageLogoManageAccount() {
  215. let controller = CCManageAccount()
  216. self.navigationController?.pushViewController(controller, animated: true)
  217. }
  218. // MARK: -
  219. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  220. if indexPath.section == 0 {
  221. return 75
  222. } else {
  223. return NCGlobal.shared.heightCellSettings
  224. }
  225. }
  226. func numberOfSections(in tableView: UITableView) -> Int {
  227. if externalSiteMenu.count == 0 {
  228. return 3
  229. } else {
  230. return 4
  231. }
  232. }
  233. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  234. if section == 0 {
  235. return 10
  236. } else {
  237. return 20
  238. }
  239. }
  240. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  241. var cont = 0
  242. if section == 0 {
  243. cont = tabAccount == nil ? 0 : 1
  244. } else if section == 1 {
  245. // Menu Normal
  246. cont = functionMenu.count
  247. } else {
  248. switch numberOfSections(in: tableView) {
  249. case 3:
  250. // Menu Settings
  251. if section == 2 {
  252. cont = settingsMenu.count
  253. }
  254. case 4:
  255. // Menu External Site
  256. if section == 2 {
  257. cont = externalSiteMenu.count
  258. }
  259. // Menu Settings
  260. if section == 3 {
  261. cont = settingsMenu.count
  262. }
  263. default:
  264. cont = 0
  265. }
  266. }
  267. return cont
  268. }
  269. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  270. var item = NKExternalSite()
  271. // change color selection and disclosure indicator
  272. let selectionColor: UIView = UIView()
  273. if indexPath.section == 0 {
  274. let cell = tableView.dequeueReusableCell(withIdentifier: "userCell", for: indexPath) as! NCMoreUserCell
  275. cell.avatar.image = nil
  276. cell.icon.image = nil
  277. cell.status.text = ""
  278. cell.displayName.text = ""
  279. if let account = tabAccount {
  280. cell.avatar.image = NCUtility.shared.loadUserImage(
  281. for: account.user,
  282. displayName: account.displayName,
  283. userBaseUrl: appDelegate)
  284. if account.alias == "" {
  285. cell.displayName?.text = account.displayName
  286. } else {
  287. cell.displayName?.text = account.displayName + " (" + account.alias + ")"
  288. }
  289. cell.displayName.textColor = .label
  290. }
  291. cell.selectedBackgroundView = selectionColor
  292. cell.backgroundColor = .secondarySystemGroupedBackground
  293. cell.accessoryType = UITableViewCell.AccessoryType.disclosureIndicator
  294. if NCManageDatabase.shared.getCapabilitiesServerBool(account: appDelegate.account, elements: NCElementsJSON.shared.capabilitiesUserStatusEnabled, exists: false) {
  295. if let account = NCManageDatabase.shared.getAccount(predicate: NSPredicate(format: "account == %@", appDelegate.account)) {
  296. let status = NCUtility.shared.getUserStatus(userIcon: account.userStatusIcon, userStatus: account.userStatusStatus, userMessage: account.userStatusMessage)
  297. cell.icon.image = status.onlineStatus
  298. cell.status.text = status.statusMessage
  299. cell.status.textColor = .label
  300. cell.status.trailingBuffer = cell.status.frame.width
  301. if cell.status.labelShouldScroll() {
  302. cell.status.tapToScroll = true
  303. } else {
  304. cell.status.tapToScroll = false
  305. }
  306. }
  307. }
  308. cell.layer.cornerRadius = defaultCornerRadius
  309. cell.layer.maskedCorners = [.layerMaxXMinYCorner, .layerMinXMinYCorner, .layerMaxXMaxYCorner, .layerMinXMaxYCorner]
  310. return cell
  311. } else {
  312. let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CCCellMore
  313. // Menu Normal
  314. if indexPath.section == 1 {
  315. item = functionMenu[indexPath.row]
  316. }
  317. // Menu External Site
  318. if numberOfSections(in: tableView) == 4 && indexPath.section == 2 {
  319. item = externalSiteMenu[indexPath.row]
  320. }
  321. // Menu Settings
  322. if (numberOfSections(in: tableView) == 3 && indexPath.section == 2) || (numberOfSections(in: tableView) == 4 && indexPath.section == 3) {
  323. item = settingsMenu[indexPath.row]
  324. }
  325. cell.imageIcon?.image = NCUtility.shared.loadImage(named: item.icon)
  326. cell.labelText?.text = NSLocalizedString(item.name, comment: "")
  327. cell.labelText.textColor = .label
  328. cell.selectedBackgroundView = selectionColor
  329. cell.backgroundColor = .secondarySystemGroupedBackground
  330. cell.accessoryType = UITableViewCell.AccessoryType.disclosureIndicator
  331. cell.separator.backgroundColor = .separator
  332. cell.separatorHeigth.constant = 0.4
  333. cell.layer.cornerRadius = 0
  334. let rows = tableView.numberOfRows(inSection: indexPath.section)
  335. if indexPath.row == 0 {
  336. cell.layer.cornerRadius = defaultCornerRadius
  337. if indexPath.row == rows - 1 {
  338. cell.separator.backgroundColor = .clear
  339. cell.layer.maskedCorners = [.layerMaxXMinYCorner, .layerMinXMinYCorner, .layerMaxXMaxYCorner, .layerMinXMaxYCorner]
  340. } else {
  341. cell.layer.maskedCorners = [.layerMaxXMinYCorner, .layerMinXMinYCorner]
  342. }
  343. } else if indexPath.row == rows - 1 {
  344. cell.layer.cornerRadius = defaultCornerRadius
  345. cell.layer.maskedCorners = [.layerMaxXMaxYCorner, .layerMinXMaxYCorner]
  346. cell.separator.backgroundColor = .clear
  347. }
  348. return cell
  349. }
  350. }
  351. // method to run when table view cell is tapped
  352. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  353. var item = NKExternalSite()
  354. if indexPath.section == 0 {
  355. tapImageLogoManageAccount()
  356. return
  357. }
  358. // Menu Function
  359. if indexPath.section == 1 {
  360. item = functionMenu[indexPath.row]
  361. }
  362. // Menu External Site
  363. if numberOfSections(in: tableView) == 4 && indexPath.section == 2 {
  364. item = externalSiteMenu[indexPath.row]
  365. }
  366. // Menu Settings
  367. if (numberOfSections(in: tableView) == 3 && indexPath.section == 2) || (numberOfSections(in: tableView) == 4 && indexPath.section == 3) {
  368. item = settingsMenu[indexPath.row]
  369. }
  370. // Action
  371. if item.url.contains("segue") && !item.url.contains("//") {
  372. self.navigationController?.performSegue(withIdentifier: item.url, sender: self)
  373. } else if item.url.contains("openStoryboard") && !item.url.contains("//") {
  374. let nameStoryboard = item.url.replacingOccurrences(of: "openStoryboard", with: "")
  375. let storyboard = UIStoryboard(name: nameStoryboard, bundle: nil)
  376. if let controller = storyboard.instantiateInitialViewController() {
  377. controller.modalPresentationStyle = UIModalPresentationStyle.pageSheet
  378. present(controller, animated: true, completion: nil)
  379. }
  380. } else if item.url.contains("//") {
  381. let browserWebVC = UIStoryboard(name: "NCBrowserWeb", bundle: nil).instantiateInitialViewController() as! NCBrowserWeb
  382. browserWebVC.urlBase = item.url
  383. browserWebVC.isHiddenButtonExit = true
  384. browserWebVC.titleBrowser = item.name
  385. self.navigationController?.pushViewController(browserWebVC, animated: true)
  386. self.navigationController?.navigationBar.isHidden = false
  387. } else if item.url == "logout" {
  388. let alertController = UIAlertController(title: "", message: NSLocalizedString("_want_delete_", comment: ""), preferredStyle: .alert)
  389. let actionYes = UIAlertAction(title: NSLocalizedString("_yes_delete_", comment: ""), style: .default) { (_: UIAlertAction) in
  390. let manageAccount = CCManageAccount()
  391. manageAccount.delete(self.appDelegate.account)
  392. self.appDelegate.openLogin(viewController: self, selector: NCGlobal.shared.introLogin, openLoginWeb: false)
  393. }
  394. let actionNo = UIAlertAction(title: NSLocalizedString("_no_delete_", comment: ""), style: .default) { (_: UIAlertAction) in
  395. print("You've pressed No button")
  396. }
  397. alertController.addAction(actionYes)
  398. alertController.addAction(actionNo)
  399. self.present(alertController, animated: true, completion: nil)
  400. } else {
  401. applicationHandle.didSelectItem(item, viewController: self)
  402. }
  403. }
  404. }
  405. class CCCellMore: UITableViewCell {
  406. @IBOutlet weak var labelText: UILabel!
  407. @IBOutlet weak var imageIcon: UIImageView!
  408. @IBOutlet weak var separator: UIView!
  409. @IBOutlet weak var separatorHeigth: NSLayoutConstraint!
  410. override var frame: CGRect {
  411. get {
  412. return super.frame
  413. }
  414. set (newFrame) {
  415. var frame = newFrame
  416. let newWidth = frame.width * 0.90
  417. let space = (frame.width - newWidth) / 2
  418. frame.size.width = newWidth
  419. frame.origin.x += space
  420. super.frame = frame
  421. }
  422. }
  423. }
  424. class NCMoreUserCell: UITableViewCell {
  425. @IBOutlet weak var displayName: UILabel!
  426. @IBOutlet weak var avatar: UIImageView!
  427. @IBOutlet weak var icon: UIImageView!
  428. @IBOutlet weak var status: MarqueeLabel!
  429. override var frame: CGRect {
  430. get {
  431. return super.frame
  432. }
  433. set (newFrame) {
  434. var frame = newFrame
  435. let newWidth = frame.width * 0.90
  436. let space = (frame.width - newWidth) / 2
  437. frame.size.width = newWidth
  438. frame.origin.x += space
  439. super.frame = frame
  440. }
  441. }
  442. }