NCMore.swift 17 KB

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