NCMore.swift 18 KB

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