NCMore.swift 18 KB

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