NCMore.swift 20 KB

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