NCMore.swift 17 KB

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