NCMore.swift 19 KB

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