NCMore.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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: - Life Cycle
  38. override func viewDidLoad() {
  39. super.viewDidLoad()
  40. tableView.delegate = self
  41. tableView.dataSource = self
  42. self.navigationItem.title = NSLocalizedString("_more_", comment: "")
  43. tableView.register(UINib.init(nibName: "NCMoreUserCell", bundle: nil), forCellReuseIdentifier: "userCell")
  44. // create tap gesture recognizer
  45. let tapQuota = UITapGestureRecognizer(target: self, action: #selector(tapLabelQuotaExternalSite))
  46. labelQuotaExternalSite.isUserInteractionEnabled = true
  47. labelQuotaExternalSite.addGestureRecognizer(tapQuota)
  48. // Notification
  49. NotificationCenter.default.addObserver(self, selector: #selector(initializeMain), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterInitializeMain), object: nil)
  50. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterChangeTheming), object: nil)
  51. changeTheming()
  52. }
  53. override func viewWillAppear(_ animated: Bool) {
  54. super.viewWillAppear(animated)
  55. appDelegate.activeViewController = self
  56. loadItems()
  57. }
  58. // MARK: - NotificationCenter
  59. @objc func changeTheming() {
  60. view.backgroundColor = NCBrandColor.shared.backgroundForm
  61. progressQuota.progressTintColor = NCBrandColor.shared.brandElement
  62. tableView.backgroundColor = NCBrandColor.shared.backgroundForm
  63. tableView.separatorColor = NCBrandColor.shared.separator
  64. tableView.reloadData()
  65. }
  66. @objc func initializeMain() {
  67. loadItems()
  68. }
  69. // MARK: -
  70. func loadItems() {
  71. var item = NCCommunicationExternalSite()
  72. var quota: String = ""
  73. // Clear
  74. functionMenu.removeAll()
  75. externalSiteMenu.removeAll()
  76. settingsMenu.removeAll()
  77. quotaMenu.removeAll()
  78. labelQuotaExternalSite.text = ""
  79. // ITEM : Transfer
  80. item = NCCommunicationExternalSite()
  81. item.name = "_transfers_"
  82. item.icon = "load"
  83. item.url = "segueTransfers"
  84. functionMenu.append(item)
  85. // ITEM : Recent
  86. item = NCCommunicationExternalSite()
  87. item.name = "_recent_"
  88. item.icon = "recent"
  89. item.url = "segueRecent"
  90. functionMenu.append(item)
  91. // ITEM : Notification
  92. item = NCCommunicationExternalSite()
  93. item.name = "_notification_"
  94. item.icon = "notification"
  95. item.url = "segueNotification"
  96. functionMenu.append(item)
  97. // ITEM : Activity
  98. item = NCCommunicationExternalSite()
  99. item.name = "_activity_"
  100. item.icon = "activity"
  101. item.url = "segueActivity"
  102. functionMenu.append(item)
  103. // ITEM : Shares
  104. let isFilesSharingEnabled = NCManageDatabase.shared.getCapabilitiesServerBool(account: appDelegate.account, elements: NCElementsJSON.shared.capabilitiesFileSharingApiEnabled, exists: false)
  105. if isFilesSharingEnabled {
  106. item = NCCommunicationExternalSite()
  107. item.name = "_list_shares_"
  108. item.icon = "shareFill"
  109. item.url = "segueShares"
  110. functionMenu.append(item)
  111. }
  112. // ITEM : Offline
  113. item = NCCommunicationExternalSite()
  114. item.name = "_manage_file_offline_"
  115. item.icon = "offline"
  116. item.url = "segueOffline"
  117. functionMenu.append(item)
  118. // ITEM : Scan
  119. if #available(iOS 13.0, *) {
  120. item = NCCommunicationExternalSite()
  121. item.name = "_scanned_images_"
  122. item.icon = "doc.text.viewfinder"
  123. item.url = "openStoryboardScan"
  124. functionMenu.append(item)
  125. }
  126. // ITEM : Trash
  127. let serverVersionMajor = NCManageDatabase.shared.getCapabilitiesServerInt(account: appDelegate.account, elements: NCElementsJSON.shared.capabilitiesVersionMajor)
  128. if serverVersionMajor >= NCGlobal.shared.nextcloudVersion15 {
  129. item = NCCommunicationExternalSite()
  130. item.name = "_trash_view_"
  131. item.icon = "trash"
  132. item.url = "segueTrash"
  133. functionMenu.append(item)
  134. }
  135. // ITEM : Settings
  136. item = NCCommunicationExternalSite()
  137. item.name = "_settings_"
  138. item.icon = "settings"
  139. item.url = "segueSettings"
  140. settingsMenu.append(item)
  141. if (quotaMenu.count > 0) {
  142. let item = quotaMenu[0]
  143. labelQuotaExternalSite.text = item.name
  144. }
  145. // Display Name user & Quota
  146. if let tabAccount = NCManageDatabase.shared.getAccountActive() {
  147. self.tabAccount = tabAccount
  148. if (tabAccount.quotaRelative > 0) {
  149. progressQuota.progress = Float(tabAccount.quotaRelative) / 100
  150. } else {
  151. progressQuota.progress = 0
  152. }
  153. switch tabAccount.quotaTotal {
  154. case -1:
  155. quota = "0"
  156. case -2:
  157. quota = NSLocalizedString("_quota_space_unknown_", comment: "")
  158. case -3:
  159. quota = NSLocalizedString("_quota_space_unlimited_", comment: "")
  160. default:
  161. quota = CCUtility.transformedSize(tabAccount.quotaTotal)
  162. }
  163. let quotaUsed: String = CCUtility.transformedSize(tabAccount.quotaUsed)
  164. labelQuota.text = String.localizedStringWithFormat(NSLocalizedString("_quota_using_", comment: ""), quotaUsed, quota)
  165. }
  166. // ITEM : External
  167. if NCBrandOptions.shared.disable_more_external_site == false {
  168. if let externalSites = NCManageDatabase.shared.getAllExternalSites(account: appDelegate.account) {
  169. for externalSite in externalSites {
  170. if (externalSite.type == "link" && externalSite.name != "" && externalSite.url != "") {
  171. item = NCCommunicationExternalSite()
  172. item.name = externalSite.name
  173. item.url = externalSite.url
  174. item.icon = "world"
  175. externalSiteMenu.append(item)
  176. }
  177. if (externalSite.type == "settings") {
  178. item.icon = "settings"
  179. settingsMenu.append(item)
  180. }
  181. if (externalSite.type == "quota") {
  182. quotaMenu.append(item)
  183. }
  184. }
  185. tableView.reloadData()
  186. } else {
  187. tableView.reloadData()
  188. }
  189. } else {
  190. tableView.reloadData()
  191. }
  192. }
  193. // MARK: - Action
  194. @objc func tapLabelQuotaExternalSite() {
  195. if (quotaMenu.count > 0) {
  196. let item = quotaMenu[0]
  197. let browserWebVC = UIStoryboard(name: "NCBrowserWeb", bundle: nil).instantiateInitialViewController() as! NCBrowserWeb
  198. browserWebVC.urlBase = item.url
  199. browserWebVC.isHiddenButtonExit = true
  200. self.navigationController?.pushViewController(browserWebVC, animated: true)
  201. self.navigationController?.navigationBar.isHidden = false
  202. }
  203. }
  204. @objc func tapImageLogoManageAccount() {
  205. let controller = CCManageAccount.init()
  206. self.navigationController?.pushViewController(controller, animated: true)
  207. }
  208. // MARK: -
  209. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  210. if indexPath.section == 0 {
  211. return 100
  212. } else {
  213. return 50
  214. }
  215. }
  216. func numberOfSections(in tableView: UITableView) -> Int {
  217. if (externalSiteMenu.count == 0) {
  218. return 3
  219. } else {
  220. return 4
  221. }
  222. }
  223. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  224. var cont = 0
  225. if (section == 0) {
  226. cont = tabAccount == nil ? 0 : 1
  227. } else if (section == 1) {
  228. // Menu Normal
  229. cont = functionMenu.count
  230. } else {
  231. switch (numberOfSections(in: tableView)) {
  232. case 3:
  233. // Menu Settings
  234. if (section == 2) {
  235. cont = settingsMenu.count
  236. }
  237. case 4:
  238. // Menu External Site
  239. if (section == 2) {
  240. cont = externalSiteMenu.count
  241. }
  242. // Menu Settings
  243. if (section == 3) {
  244. cont = settingsMenu.count
  245. }
  246. default:
  247. cont = 0
  248. }
  249. }
  250. return cont
  251. }
  252. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  253. var item = NCCommunicationExternalSite()
  254. // change color selection and disclosure indicator
  255. let selectionColor: UIView = UIView()
  256. selectionColor.backgroundColor = NCBrandColor.shared.select
  257. if (indexPath.section == 0) {
  258. let cell = tableView.dequeueReusableCell(withIdentifier: "userCell", for: indexPath) as! NCMoreUserCell
  259. cell.avatar.image = nil
  260. cell.icon.image = nil
  261. cell.status.text = ""
  262. cell.displayName.text = ""
  263. let fileNamePath = String(CCUtility.getDirectoryUserData()) + "/" + String(CCUtility.getStringUser(appDelegate.user, urlBase: appDelegate.urlBase)) + "-" + appDelegate.user + ".png"
  264. if let themingAvatarFile = UIImage.init(contentsOfFile: fileNamePath) {
  265. cell.avatar?.image = themingAvatarFile
  266. } else {
  267. cell.avatar?.image = UIImage.init(named: "moreAvatar")
  268. }
  269. cell.avatar?.layer.masksToBounds = true
  270. cell.avatar?.layer.cornerRadius = cell.avatar.frame.size.width / 2
  271. if let account = tabAccount {
  272. cell.displayName?.text = account.displayName
  273. cell.displayName.textColor = NCBrandColor.shared.textView
  274. }
  275. cell.selectedBackgroundView = selectionColor
  276. cell.backgroundColor = NCBrandColor.shared.backgroundView
  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.textView
  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 = NCCollectionCommon.shared.loadImage(named: item.icon)
  302. cell.labelText?.text = NSLocalizedString(item.name, comment: "")
  303. cell.labelText.textColor = NCBrandColor.shared.textView
  304. cell.selectedBackgroundView = selectionColor
  305. cell.backgroundColor = NCBrandColor.shared.backgroundView
  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. let controller = storyboard.instantiateInitialViewController()! //instantiateViewController(withIdentifier: nameStoryboard)
  336. self.present(controller, animated: true, completion: nil)
  337. } else if item.url.contains("//") {
  338. let browserWebVC = UIStoryboard(name: "NCBrowserWeb", bundle: nil).instantiateInitialViewController() as! NCBrowserWeb
  339. browserWebVC.urlBase = item.url
  340. browserWebVC.isHiddenButtonExit = true
  341. browserWebVC.titleBrowser = item.name
  342. self.navigationController?.pushViewController(browserWebVC, animated: true)
  343. self.navigationController?.navigationBar.isHidden = false
  344. } else if item.url == "logout" {
  345. let alertController = UIAlertController(title: "", message: NSLocalizedString("_want_delete_", comment: ""), preferredStyle: .alert)
  346. let actionYes = UIAlertAction(title: NSLocalizedString("_yes_delete_", comment: ""), style: .default) { (action: UIAlertAction) in
  347. let manageAccount = CCManageAccount()
  348. manageAccount.delete(self.appDelegate.account)
  349. self.appDelegate.openLogin(viewController:self, selector: NCGlobal.shared.introLogin, openLoginWeb: false)
  350. }
  351. let actionNo = UIAlertAction(title: NSLocalizedString("_no_delete_", comment: ""), style: .default) { (action: UIAlertAction) in
  352. print("You've pressed No button")
  353. }
  354. alertController.addAction(actionYes)
  355. alertController.addAction(actionNo)
  356. self.present(alertController, animated: true, completion: nil)
  357. }
  358. }
  359. }
  360. class CCCellMore: UITableViewCell {
  361. @IBOutlet weak var labelText: UILabel!
  362. @IBOutlet weak var imageIcon: UIImageView!
  363. }
  364. class NCMoreUserCell: UITableViewCell {
  365. @IBOutlet weak var displayName: UILabel!
  366. @IBOutlet weak var avatar: UIImageView!
  367. @IBOutlet weak var icon: UIImageView!
  368. @IBOutlet weak var status: UILabel!
  369. }