NCMore.swift 17 KB

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