NCMore.swift 19 KB

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