NCMore.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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 NCCommunication
  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 functionMenu: [NCCommunicationExternalSite] = []
  32. var externalSiteMenu: [NCCommunicationExternalSite] = []
  33. var settingsMenu: [NCCommunicationExternalSite] = []
  34. var quotaMenu: [NCCommunicationExternalSite] = []
  35. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  36. var tabAccount: tableAccount?
  37. // MARK: - View Life Cycle
  38. override func viewDidLoad() {
  39. super.viewDidLoad()
  40. self.navigationItem.title = NSLocalizedString("_more_", comment: "")
  41. view.backgroundColor = NCBrandColor.shared.systemGroupedBackground
  42. tableView.delegate = self
  43. tableView.dataSource = self
  44. tableView.backgroundColor = NCBrandColor.shared.systemGroupedBackground
  45. tableView.separatorColor = NCBrandColor.shared.separator
  46. tableView.register(UINib.init(nibName: "NCMoreUserCell", bundle: nil), forCellReuseIdentifier: "userCell")
  47. // create tap gesture recognizer
  48. let tapQuota = UITapGestureRecognizer(target: self, action: #selector(tapLabelQuotaExternalSite))
  49. labelQuotaExternalSite.isUserInteractionEnabled = true
  50. labelQuotaExternalSite.addGestureRecognizer(tapQuota)
  51. // Notification
  52. NotificationCenter.default.addObserver(self, selector: #selector(initialize), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterInitialize), object: nil)
  53. }
  54. override func viewWillAppear(_ animated: Bool) {
  55. super.viewWillAppear(animated)
  56. appDelegate.activeViewController = self
  57. loadItems()
  58. }
  59. // MARK: - NotificationCenter
  60. @objc func initialize() {
  61. loadItems()
  62. }
  63. // MARK: -
  64. func loadItems() {
  65. var item = NCCommunicationExternalSite()
  66. var quota: String = ""
  67. // Clear
  68. functionMenu.removeAll()
  69. externalSiteMenu.removeAll()
  70. settingsMenu.removeAll()
  71. quotaMenu.removeAll()
  72. labelQuotaExternalSite.text = ""
  73. progressQuota.progressTintColor = NCBrandColor.shared.brandElement
  74. // ITEM : Transfer
  75. item = NCCommunicationExternalSite()
  76. item.name = "_transfers_"
  77. item.icon = "arrow.left.arrow.right"
  78. item.url = "segueTransfers"
  79. functionMenu.append(item)
  80. // ITEM : Recent
  81. item = NCCommunicationExternalSite()
  82. item.name = "_recent_"
  83. item.icon = "recent"
  84. item.url = "segueRecent"
  85. functionMenu.append(item)
  86. // ITEM : Notification
  87. item = NCCommunicationExternalSite()
  88. item.name = "_notification_"
  89. item.icon = "bell"
  90. item.url = "segueNotification"
  91. functionMenu.append(item)
  92. // ITEM : Activity
  93. item = NCCommunicationExternalSite()
  94. item.name = "_activity_"
  95. item.icon = "bolt"
  96. item.url = "segueActivity"
  97. functionMenu.append(item)
  98. // ITEM : Shares
  99. let isFilesSharingEnabled = NCManageDatabase.shared.getCapabilitiesServerBool(account: appDelegate.account, elements: NCElementsJSON.shared.capabilitiesFileSharingApiEnabled, exists: false)
  100. if isFilesSharingEnabled {
  101. item = NCCommunicationExternalSite()
  102. item.name = "_list_shares_"
  103. item.icon = "share"
  104. item.url = "segueShares"
  105. functionMenu.append(item)
  106. }
  107. // ITEM : Offline
  108. item = NCCommunicationExternalSite()
  109. item.name = "_manage_file_offline_"
  110. item.icon = "tray.and.arrow.down"
  111. item.url = "segueOffline"
  112. functionMenu.append(item)
  113. // ITEM : Scan
  114. if #available(iOS 13.0, *) {
  115. item = NCCommunicationExternalSite()
  116. item.name = "_scanned_images_"
  117. item.icon = "doc.text.viewfinder"
  118. item.url = "openStoryboardScan"
  119. functionMenu.append(item)
  120. }
  121. // ITEM : Trash
  122. let serverVersionMajor = NCManageDatabase.shared.getCapabilitiesServerInt(account: appDelegate.account, elements: NCElementsJSON.shared.capabilitiesVersionMajor)
  123. if serverVersionMajor >= NCGlobal.shared.nextcloudVersion15 {
  124. item = NCCommunicationExternalSite()
  125. item.name = "_trash_view_"
  126. item.icon = "trash"
  127. item.url = "segueTrash"
  128. functionMenu.append(item)
  129. }
  130. // ITEM : Settings
  131. item = NCCommunicationExternalSite()
  132. item.name = "_settings_"
  133. item.icon = "gear"
  134. item.url = "segueSettings"
  135. settingsMenu.append(item)
  136. if (quotaMenu.count > 0) {
  137. let item = quotaMenu[0]
  138. labelQuotaExternalSite.text = item.name
  139. }
  140. // Display Name user & Quota
  141. if let activeAccount = NCManageDatabase.shared.getActiveAccount() {
  142. self.tabAccount = activeAccount
  143. if (activeAccount.quotaRelative > 0) {
  144. progressQuota.progress = Float(activeAccount.quotaRelative) / 100
  145. } else {
  146. progressQuota.progress = 0
  147. }
  148. switch activeAccount.quotaTotal {
  149. case -1:
  150. quota = "0"
  151. case -2:
  152. quota = NSLocalizedString("_quota_space_unknown_", comment: "")
  153. case -3:
  154. quota = NSLocalizedString("_quota_space_unlimited_", comment: "")
  155. default:
  156. quota = CCUtility.transformedSize(activeAccount.quotaTotal)
  157. }
  158. let quotaUsed: String = CCUtility.transformedSize(activeAccount.quotaUsed)
  159. labelQuota.text = String.localizedStringWithFormat(NSLocalizedString("_quota_using_", comment: ""), quotaUsed, quota)
  160. }
  161. // ITEM : External
  162. if NCBrandOptions.shared.disable_more_external_site == false {
  163. if let externalSites = NCManageDatabase.shared.getAllExternalSites(account: appDelegate.account) {
  164. for externalSite in externalSites {
  165. if (externalSite.name != "" && externalSite.url != ""), let urlEncoded = externalSite.url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
  166. item = NCCommunicationExternalSite()
  167. item.name = externalSite.name
  168. item.url = urlEncoded
  169. item.icon = "network"
  170. if (externalSite.type == "settings") {
  171. item.icon = "gear"
  172. }
  173. externalSiteMenu.append(item)
  174. }
  175. }
  176. tableView.reloadData()
  177. } else {
  178. tableView.reloadData()
  179. }
  180. } else {
  181. tableView.reloadData()
  182. }
  183. }
  184. // MARK: - Action
  185. @objc func tapLabelQuotaExternalSite() {
  186. if (quotaMenu.count > 0) {
  187. let item = quotaMenu[0]
  188. let browserWebVC = UIStoryboard(name: "NCBrowserWeb", bundle: nil).instantiateInitialViewController() as! NCBrowserWeb
  189. browserWebVC.urlBase = item.url
  190. browserWebVC.isHiddenButtonExit = true
  191. self.navigationController?.pushViewController(browserWebVC, animated: true)
  192. self.navigationController?.navigationBar.isHidden = false
  193. }
  194. }
  195. @objc func tapImageLogoManageAccount() {
  196. let controller = CCManageAccount.init()
  197. self.navigationController?.pushViewController(controller, animated: true)
  198. }
  199. // MARK: -
  200. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  201. if indexPath.section == 0 {
  202. return 100
  203. } else {
  204. return NCGlobal.shared.heightCellSettings
  205. }
  206. }
  207. func numberOfSections(in tableView: UITableView) -> Int {
  208. if (externalSiteMenu.count == 0) {
  209. return 3
  210. } else {
  211. return 4
  212. }
  213. }
  214. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  215. var cont = 0
  216. if (section == 0) {
  217. cont = tabAccount == nil ? 0 : 1
  218. } else if (section == 1) {
  219. // Menu Normal
  220. cont = functionMenu.count
  221. } else {
  222. switch (numberOfSections(in: tableView)) {
  223. case 3:
  224. // Menu Settings
  225. if (section == 2) {
  226. cont = settingsMenu.count
  227. }
  228. case 4:
  229. // Menu External Site
  230. if (section == 2) {
  231. cont = externalSiteMenu.count
  232. }
  233. // Menu Settings
  234. if (section == 3) {
  235. cont = settingsMenu.count
  236. }
  237. default:
  238. cont = 0
  239. }
  240. }
  241. return cont
  242. }
  243. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  244. var item = NCCommunicationExternalSite()
  245. // change color selection and disclosure indicator
  246. let selectionColor: UIView = UIView()
  247. if (indexPath.section == 0) {
  248. let cell = tableView.dequeueReusableCell(withIdentifier: "userCell", for: indexPath) as! NCMoreUserCell
  249. cell.avatar.image = nil
  250. cell.icon.image = nil
  251. cell.status.text = ""
  252. cell.displayName.text = ""
  253. let fileNamePath = String(CCUtility.getDirectoryUserData()) + "/" + String(CCUtility.getStringUser(appDelegate.user, urlBase: appDelegate.urlBase)) + "-" + appDelegate.user + ".png"
  254. if let image = UIImage.init(contentsOfFile: fileNamePath) {
  255. cell.avatar?.image = NCUtility.shared.createAvatar(image: image, size: 50)
  256. } else {
  257. cell.avatar?.image = UIImage.init(named: "avatar")?.imageColor(NCBrandColor.shared.gray)
  258. }
  259. if let account = tabAccount {
  260. if account.alias == "" {
  261. cell.displayName?.text = account.displayName
  262. } else {
  263. cell.displayName?.text = account.displayName + " (" + account.alias + ")"
  264. }
  265. cell.displayName.textColor = NCBrandColor.shared.label
  266. }
  267. cell.selectedBackgroundView = selectionColor
  268. cell.backgroundColor = NCBrandColor.shared.secondarySystemGroupedBackground
  269. cell.accessoryType = UITableViewCell.AccessoryType.disclosureIndicator
  270. if NCManageDatabase.shared.getCapabilitiesServerBool(account: appDelegate.account, elements: NCElementsJSON.shared.capabilitiesUserStatusEnabled, exists: false) {
  271. if let account = NCManageDatabase.shared.getAccount(predicate: NSPredicate(format: "account == %@", appDelegate.account)) {
  272. let status = NCUtility.shared.getUserStatus(userIcon: account.userStatusIcon, userStatus: account.userStatusStatus, userMessage: account.userStatusMessage)
  273. cell.icon.image = status.onlineStatus
  274. cell.status.text = status.statusMessage
  275. cell.status.textColor = NCBrandColor.shared.label
  276. }
  277. }
  278. return cell
  279. } else {
  280. let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CCCellMore
  281. // Menu Normal
  282. if (indexPath.section == 1) {
  283. item = functionMenu[indexPath.row]
  284. }
  285. // Menu External Site
  286. if (numberOfSections(in: tableView) == 4 && indexPath.section == 2) {
  287. item = externalSiteMenu[indexPath.row]
  288. }
  289. // Menu Settings
  290. if ((numberOfSections(in: tableView) == 3 && indexPath.section == 2) || (numberOfSections(in: tableView) == 4 && indexPath.section == 3)) {
  291. item = settingsMenu[indexPath.row]
  292. }
  293. cell.imageIcon?.image = NCUtility.shared.loadImage(named: item.icon)
  294. cell.labelText?.text = NSLocalizedString(item.name, comment: "")
  295. cell.labelText.textColor = NCBrandColor.shared.label
  296. cell.selectedBackgroundView = selectionColor
  297. cell.backgroundColor = NCBrandColor.shared.secondarySystemGroupedBackground
  298. cell.accessoryType = UITableViewCell.AccessoryType.disclosureIndicator
  299. return cell
  300. }
  301. }
  302. // method to run when table view cell is tapped
  303. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  304. var item = NCCommunicationExternalSite()
  305. if indexPath.section == 0 {
  306. tapImageLogoManageAccount()
  307. return
  308. }
  309. // Menu Function
  310. if indexPath.section == 1 {
  311. item = functionMenu[indexPath.row]
  312. }
  313. // Menu External Site
  314. if (numberOfSections(in: tableView) == 4 && indexPath.section == 2) {
  315. item = externalSiteMenu[indexPath.row]
  316. }
  317. // Menu Settings
  318. if ((numberOfSections(in: tableView) == 3 && indexPath.section == 2) || (numberOfSections(in: tableView) == 4 && indexPath.section == 3)) {
  319. item = settingsMenu[indexPath.row]
  320. }
  321. // Action
  322. if item.url.contains("segue") && !item.url.contains("//") {
  323. self.navigationController?.performSegue(withIdentifier: item.url, sender: self)
  324. } else if item.url.contains("openStoryboard") && !item.url.contains("//") {
  325. let nameStoryboard = item.url.replacingOccurrences(of: "openStoryboard", with: "")
  326. let storyboard = UIStoryboard(name: nameStoryboard, bundle: nil)
  327. if let controller = storyboard.instantiateInitialViewController() {
  328. controller.modalPresentationStyle = UIModalPresentationStyle.pageSheet
  329. present(controller, animated: true, completion: nil)
  330. }
  331. } else if item.url.contains("//") {
  332. let browserWebVC = UIStoryboard(name: "NCBrowserWeb", bundle: nil).instantiateInitialViewController() as! NCBrowserWeb
  333. browserWebVC.urlBase = item.url
  334. browserWebVC.isHiddenButtonExit = true
  335. browserWebVC.titleBrowser = item.name
  336. self.navigationController?.pushViewController(browserWebVC, animated: true)
  337. self.navigationController?.navigationBar.isHidden = false
  338. } else if item.url == "logout" {
  339. let alertController = UIAlertController(title: "", message: NSLocalizedString("_want_delete_", comment: ""), preferredStyle: .alert)
  340. let actionYes = UIAlertAction(title: NSLocalizedString("_yes_delete_", comment: ""), style: .default) { (action: UIAlertAction) in
  341. let manageAccount = CCManageAccount()
  342. manageAccount.delete(self.appDelegate.account)
  343. self.appDelegate.openLogin(viewController:self, selector: NCGlobal.shared.introLogin, openLoginWeb: false)
  344. }
  345. let actionNo = UIAlertAction(title: NSLocalizedString("_no_delete_", comment: ""), style: .default) { (action: UIAlertAction) in
  346. print("You've pressed No button")
  347. }
  348. alertController.addAction(actionYes)
  349. alertController.addAction(actionNo)
  350. self.present(alertController, animated: true, completion: nil)
  351. }
  352. }
  353. }
  354. class CCCellMore: UITableViewCell {
  355. @IBOutlet weak var labelText: UILabel!
  356. @IBOutlet weak var imageIcon: UIImageView!
  357. }
  358. class NCMoreUserCell: UITableViewCell {
  359. @IBOutlet weak var displayName: UILabel!
  360. @IBOutlet weak var avatar: UIImageView!
  361. @IBOutlet weak var icon: UIImageView!
  362. @IBOutlet weak var status: UILabel!
  363. }