NCMore.swift 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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. // Clear
  72. functionMenu.removeAll()
  73. externalSiteMenu.removeAll()
  74. settingsMenu.removeAll()
  75. quotaMenu.removeAll()
  76. labelQuotaExternalSite.text = ""
  77. progressQuota.progressTintColor = NCBrandColor.shared.brandElement
  78. // ITEM : Transfer
  79. item = NKExternalSite()
  80. item.name = "_transfers_"
  81. item.icon = "arrow.left.arrow.right"
  82. item.url = "segueTransfers"
  83. item.order = 10
  84. functionMenu.append(item)
  85. // ITEM : Recent
  86. item = NKExternalSite()
  87. item.name = "_recent_"
  88. item.icon = "clock.arrow.circlepath"
  89. item.url = "segueRecent"
  90. item.order = 20
  91. functionMenu.append(item)
  92. // ITEM : Notification
  93. item = NKExternalSite()
  94. item.name = "_notifications_"
  95. item.icon = "bell"
  96. item.url = "segueNotification"
  97. item.order = 30
  98. functionMenu.append(item)
  99. // ITEM : Activity
  100. item = NKExternalSite()
  101. item.name = "_activity_"
  102. item.icon = "bolt"
  103. item.url = "segueActivity"
  104. item.order = 40
  105. functionMenu.append(item)
  106. // ITEM : Shares
  107. if NCGlobal.shared.capabilityFileSharingApiEnabled {
  108. item = NKExternalSite()
  109. item.name = "_list_shares_"
  110. item.icon = "share"
  111. item.url = "segueShares"
  112. item.order = 50
  113. functionMenu.append(item)
  114. }
  115. // ITEM : Offline
  116. item = NKExternalSite()
  117. item.name = "_manage_file_offline_"
  118. item.icon = "tray.and.arrow.down"
  119. item.url = "segueOffline"
  120. item.order = 60
  121. functionMenu.append(item)
  122. // ITEM : Groupfolders
  123. let hasAccessibleGroupFolders = NCManageDatabase.shared.getCapabilitiesServerBool(account: appDelegate.account, elements: NCElementsJSON.shared.capabilitiesGroupfoldersEnabled, exists: false)
  124. if hasAccessibleGroupFolders {
  125. item = NKExternalSite()
  126. item.name = "_group_folders_"
  127. item.icon = "person.2"
  128. item.url = "segueGroupfolders"
  129. item.order = 61
  130. functionMenu.append(item)
  131. }
  132. // ITEM : Scan
  133. item = NKExternalSite()
  134. item.name = "_scanned_images_"
  135. item.icon = "doc.text.viewfinder"
  136. item.url = "openStoryboardNCScan"
  137. item.order = 70
  138. functionMenu.append(item)
  139. // ITEM : Trash
  140. if NCGlobal.shared.capabilityServerVersionMajor >= NCGlobal.shared.nextcloudVersion15 {
  141. item = NKExternalSite()
  142. item.name = "_trash_view_"
  143. item.icon = "trash"
  144. item.url = "segueTrash"
  145. item.order = 80
  146. functionMenu.append(item)
  147. }
  148. // ITEM : HANDLE
  149. applicationHandle.loadItems(functionMenu: &functionMenu)
  150. // ORDER ITEM
  151. functionMenu = functionMenu.sorted(by: { $0.order < $1.order })
  152. // ITEM : Settings
  153. item = NKExternalSite()
  154. item.name = "_settings_"
  155. item.icon = "gear"
  156. item.url = "segueSettings"
  157. settingsMenu.append(item)
  158. if quotaMenu.count > 0 {
  159. let item = quotaMenu[0]
  160. labelQuotaExternalSite.text = item.name
  161. }
  162. // Display Name user & Quota
  163. if let activeAccount = NCManageDatabase.shared.getActiveAccount() {
  164. self.tabAccount = activeAccount
  165. if activeAccount.quotaRelative > 0 {
  166. progressQuota.progress = Float(activeAccount.quotaRelative) / 100
  167. } else {
  168. progressQuota.progress = 0
  169. }
  170. switch activeAccount.quotaTotal {
  171. case -1:
  172. quota = "0"
  173. case -2:
  174. quota = NSLocalizedString("_quota_space_unknown_", comment: "")
  175. case -3:
  176. quota = NSLocalizedString("_quota_space_unlimited_", comment: "")
  177. default:
  178. quota = CCUtility.transformedSize(activeAccount.quotaTotal)
  179. }
  180. let quotaUsed: String = CCUtility.transformedSize(activeAccount.quotaUsed)
  181. labelQuota.text = String.localizedStringWithFormat(NSLocalizedString("_quota_using_", comment: ""), quotaUsed, quota)
  182. }
  183. // ITEM : External
  184. if NCBrandOptions.shared.disable_more_external_site == false {
  185. if let externalSites = NCManageDatabase.shared.getAllExternalSites(account: appDelegate.account) {
  186. for externalSite in externalSites {
  187. if (externalSite.name != "" && externalSite.url != ""), let urlEncoded = externalSite.url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
  188. item = NKExternalSite()
  189. item.name = externalSite.name
  190. item.url = urlEncoded
  191. item.icon = "network"
  192. if externalSite.type == "settings" {
  193. item.icon = "gear"
  194. }
  195. externalSiteMenu.append(item)
  196. }
  197. }
  198. }
  199. }
  200. }
  201. // MARK: - Action
  202. @objc func tapLabelQuotaExternalSite() {
  203. if quotaMenu.count > 0 {
  204. let item = quotaMenu[0]
  205. let browserWebVC = UIStoryboard(name: "NCBrowserWeb", bundle: nil).instantiateInitialViewController() as! NCBrowserWeb
  206. browserWebVC.urlBase = item.url
  207. browserWebVC.isHiddenButtonExit = true
  208. self.navigationController?.pushViewController(browserWebVC, animated: true)
  209. self.navigationController?.navigationBar.isHidden = false
  210. }
  211. }
  212. @objc func tapImageLogoManageAccount() {
  213. let controller = CCManageAccount()
  214. self.navigationController?.pushViewController(controller, animated: true)
  215. }
  216. // MARK: -
  217. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  218. if indexPath.section == 0 {
  219. return 75
  220. } else {
  221. return NCGlobal.shared.heightCellSettings
  222. }
  223. }
  224. func numberOfSections(in tableView: UITableView) -> Int {
  225. if externalSiteMenu.count == 0 {
  226. return 3
  227. } else {
  228. return 4
  229. }
  230. }
  231. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  232. if section == 0 {
  233. return 10
  234. } else {
  235. return 20
  236. }
  237. }
  238. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  239. var cont = 0
  240. if section == 0 {
  241. cont = tabAccount == nil ? 0 : 1
  242. } else if section == 1 {
  243. // Menu Normal
  244. cont = functionMenu.count
  245. } else {
  246. switch numberOfSections(in: tableView) {
  247. case 3:
  248. // Menu Settings
  249. if section == 2 {
  250. cont = settingsMenu.count
  251. }
  252. case 4:
  253. // Menu External Site
  254. if section == 2 {
  255. cont = externalSiteMenu.count
  256. }
  257. // Menu Settings
  258. if section == 3 {
  259. cont = settingsMenu.count
  260. }
  261. default:
  262. cont = 0
  263. }
  264. }
  265. return cont
  266. }
  267. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  268. var item = NKExternalSite()
  269. // change color selection and disclosure indicator
  270. let selectionColor: UIView = UIView()
  271. if indexPath.section == 0 {
  272. let cell = tableView.dequeueReusableCell(withIdentifier: "userCell", 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.selectedBackgroundView = selectionColor
  290. cell.backgroundColor = .secondarySystemGroupedBackground
  291. cell.accessoryType = UITableViewCell.AccessoryType.disclosureIndicator
  292. if NCManageDatabase.shared.getCapabilitiesServerBool(account: appDelegate.account, elements: NCElementsJSON.shared.capabilitiesUserStatusEnabled, exists: false) {
  293. if let account = NCManageDatabase.shared.getAccount(predicate: NSPredicate(format: "account == %@", appDelegate.account)) {
  294. let status = NCUtility.shared.getUserStatus(userIcon: account.userStatusIcon, userStatus: account.userStatusStatus, userMessage: account.userStatusMessage)
  295. cell.icon.image = status.onlineStatus
  296. cell.status.text = status.statusMessage
  297. cell.status.textColor = .label
  298. cell.status.trailingBuffer = cell.status.frame.width
  299. if cell.status.labelShouldScroll() {
  300. cell.status.tapToScroll = true
  301. } else {
  302. cell.status.tapToScroll = false
  303. }
  304. }
  305. }
  306. cell.layer.cornerRadius = defaultCornerRadius
  307. cell.layer.maskedCorners = [.layerMaxXMinYCorner, .layerMinXMinYCorner, .layerMaxXMaxYCorner, .layerMinXMaxYCorner]
  308. return cell
  309. } else {
  310. let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CCCellMore
  311. // Menu Normal
  312. if indexPath.section == 1 {
  313. item = functionMenu[indexPath.row]
  314. }
  315. // Menu External Site
  316. if numberOfSections(in: tableView) == 4 && indexPath.section == 2 {
  317. item = externalSiteMenu[indexPath.row]
  318. }
  319. // Menu Settings
  320. if (numberOfSections(in: tableView) == 3 && indexPath.section == 2) || (numberOfSections(in: tableView) == 4 && indexPath.section == 3) {
  321. item = settingsMenu[indexPath.row]
  322. }
  323. cell.imageIcon?.image = NCUtility.shared.loadImage(named: item.icon)
  324. cell.imageIcon?.contentMode = .scaleAspectFit
  325. cell.labelText?.text = NSLocalizedString(item.name, comment: "")
  326. cell.labelText.textColor = .label
  327. cell.selectedBackgroundView = selectionColor
  328. cell.backgroundColor = .secondarySystemGroupedBackground
  329. cell.accessoryType = UITableViewCell.AccessoryType.disclosureIndicator
  330. cell.separator.backgroundColor = .separator
  331. cell.separatorHeigth.constant = 0.4
  332. cell.layer.cornerRadius = 0
  333. let rows = tableView.numberOfRows(inSection: indexPath.section)
  334. if indexPath.row == 0 {
  335. cell.layer.cornerRadius = defaultCornerRadius
  336. if indexPath.row == rows - 1 {
  337. cell.separator.backgroundColor = .clear
  338. cell.layer.maskedCorners = [.layerMaxXMinYCorner, .layerMinXMinYCorner, .layerMaxXMaxYCorner, .layerMinXMaxYCorner]
  339. } else {
  340. cell.layer.maskedCorners = [.layerMaxXMinYCorner, .layerMinXMinYCorner]
  341. }
  342. } else if indexPath.row == rows - 1 {
  343. cell.layer.cornerRadius = defaultCornerRadius
  344. cell.layer.maskedCorners = [.layerMaxXMaxYCorner, .layerMinXMaxYCorner]
  345. cell.separator.backgroundColor = .clear
  346. }
  347. return cell
  348. }
  349. }
  350. // method to run when table view cell is tapped
  351. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  352. var item = NKExternalSite()
  353. if indexPath.section == 0 {
  354. tapImageLogoManageAccount()
  355. return
  356. }
  357. // Menu Function
  358. if indexPath.section == 1 {
  359. item = functionMenu[indexPath.row]
  360. }
  361. // Menu External Site
  362. if numberOfSections(in: tableView) == 4 && indexPath.section == 2 {
  363. item = externalSiteMenu[indexPath.row]
  364. }
  365. // Menu Settings
  366. if (numberOfSections(in: tableView) == 3 && indexPath.section == 2) || (numberOfSections(in: tableView) == 4 && indexPath.section == 3) {
  367. item = settingsMenu[indexPath.row]
  368. }
  369. // Action
  370. if item.url.contains("segue") && !item.url.contains("//") {
  371. self.navigationController?.performSegue(withIdentifier: item.url, sender: self)
  372. } else if item.url.contains("openStoryboard") && !item.url.contains("//") {
  373. let nameStoryboard = item.url.replacingOccurrences(of: "openStoryboard", with: "")
  374. let storyboard = UIStoryboard(name: nameStoryboard, bundle: nil)
  375. if let controller = storyboard.instantiateInitialViewController() {
  376. controller.modalPresentationStyle = UIModalPresentationStyle.pageSheet
  377. present(controller, animated: true, completion: nil)
  378. }
  379. } else if item.url.contains("//") {
  380. let browserWebVC = UIStoryboard(name: "NCBrowserWeb", bundle: nil).instantiateInitialViewController() as! NCBrowserWeb
  381. browserWebVC.urlBase = item.url
  382. browserWebVC.isHiddenButtonExit = true
  383. browserWebVC.titleBrowser = item.name
  384. self.navigationController?.pushViewController(browserWebVC, animated: true)
  385. self.navigationController?.navigationBar.isHidden = false
  386. } else if item.url == "logout" {
  387. let alertController = UIAlertController(title: "", message: NSLocalizedString("_want_delete_", comment: ""), preferredStyle: .alert)
  388. let actionYes = UIAlertAction(title: NSLocalizedString("_yes_delete_", comment: ""), style: .default) { (_: UIAlertAction) in
  389. let manageAccount = CCManageAccount()
  390. manageAccount.delete(self.appDelegate.account)
  391. self.appDelegate.openLogin(viewController: self, selector: NCGlobal.shared.introLogin, openLoginWeb: false)
  392. }
  393. let actionNo = UIAlertAction(title: NSLocalizedString("_no_delete_", comment: ""), style: .default) { (_: UIAlertAction) in
  394. print("You've pressed No button")
  395. }
  396. alertController.addAction(actionYes)
  397. alertController.addAction(actionNo)
  398. self.present(alertController, animated: true, completion: nil)
  399. } else {
  400. applicationHandle.didSelectItem(item, viewController: self)
  401. }
  402. }
  403. }
  404. class CCCellMore: UITableViewCell {
  405. @IBOutlet weak var labelText: UILabel!
  406. @IBOutlet weak var imageIcon: UIImageView!
  407. @IBOutlet weak var separator: UIView!
  408. @IBOutlet weak var separatorHeigth: NSLayoutConstraint!
  409. override var frame: CGRect {
  410. get {
  411. return super.frame
  412. }
  413. set (newFrame) {
  414. var frame = newFrame
  415. let newWidth = frame.width * 0.90
  416. let space = (frame.width - newWidth) / 2
  417. frame.size.width = newWidth
  418. frame.origin.x += space
  419. super.frame = frame
  420. }
  421. }
  422. }
  423. class NCMoreUserCell: UITableViewCell {
  424. @IBOutlet weak var displayName: UILabel!
  425. @IBOutlet weak var avatar: UIImageView!
  426. @IBOutlet weak var icon: UIImageView!
  427. @IBOutlet weak var status: MarqueeLabel!
  428. override var frame: CGRect {
  429. get {
  430. return super.frame
  431. }
  432. set (newFrame) {
  433. var frame = newFrame
  434. let newWidth = frame.width * 0.90
  435. let space = (frame.width - newWidth) / 2
  436. frame.size.width = newWidth
  437. frame.origin.x += space
  438. super.frame = frame
  439. }
  440. }
  441. }