NCMore.swift 20 KB

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