NCMore.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. required init?(coder aDecoder: NSCoder) {
  38. super.init(coder: aDecoder)
  39. appDelegate.activeMore = self
  40. }
  41. override func viewDidLoad() {
  42. super.viewDidLoad()
  43. tableView.delegate = self
  44. tableView.dataSource = self
  45. self.navigationItem.title = NSLocalizedString("_more_", comment: "")
  46. // create tap gesture recognizer
  47. let tapQuota = UITapGestureRecognizer(target: self, action: #selector(tapLabelQuotaExternalSite))
  48. labelQuotaExternalSite.isUserInteractionEnabled = true
  49. labelQuotaExternalSite.addGestureRecognizer(tapQuota)
  50. // Notification
  51. NotificationCenter.default.addObserver(self, selector: #selector(changeUserProfile), name: NSNotification.Name(rawValue: k_notificationCenter_changeUserProfile), object: nil)
  52. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: k_notificationCenter_changeTheming), object: nil)
  53. changeTheming()
  54. }
  55. override func viewWillAppear(_ animated: Bool) {
  56. super.viewWillAppear(animated)
  57. var item = NCCommunicationExternalSite()
  58. // Clear
  59. functionMenu.removeAll()
  60. externalSiteMenu.removeAll()
  61. settingsMenu.removeAll()
  62. quotaMenu.removeAll()
  63. labelQuotaExternalSite.text = ""
  64. // ITEM : Transfer
  65. item = NCCommunicationExternalSite()
  66. item.name = "_transfers_"
  67. item.icon = "load"
  68. item.url = "segueTransfers"
  69. functionMenu.append(item)
  70. // ITEM : Recent
  71. item = NCCommunicationExternalSite()
  72. item.name = "_recent_"
  73. item.icon = "recent"
  74. item.url = "segueRecent"
  75. functionMenu.append(item)
  76. // ITEM : Notification
  77. item = NCCommunicationExternalSite()
  78. item.name = "_notification_"
  79. item.icon = "notification"
  80. item.url = "segueNotification"
  81. functionMenu.append(item)
  82. // ITEM : Activity
  83. item = NCCommunicationExternalSite()
  84. item.name = "_activity_"
  85. item.icon = "activity"
  86. item.url = "segueActivity"
  87. functionMenu.append(item)
  88. // ITEM : Shares
  89. let isFilesSharingEnabled = NCManageDatabase.sharedInstance.getCapabilitiesServerBool(account: appDelegate.account, elements: NCElementsJSON.shared.capabilitiesFileSharingApiEnabled, exists: false)
  90. if isFilesSharingEnabled {
  91. item = NCCommunicationExternalSite()
  92. item.name = "_list_shares_"
  93. item.icon = "shareFill"
  94. item.url = "segueShares"
  95. functionMenu.append(item)
  96. }
  97. // ITEM : Offline
  98. item = NCCommunicationExternalSite()
  99. item.name = "_manage_file_offline_"
  100. item.icon = "offline"
  101. item.url = "segueOffline"
  102. functionMenu.append(item)
  103. // ITEM : Scan
  104. item = NCCommunicationExternalSite()
  105. item.name = "_scanned_images_"
  106. item.icon = "scan"
  107. item.url = "openStoryboardScan"
  108. functionMenu.append(item)
  109. // ITEM : Trash
  110. let serverVersionMajor = NCManageDatabase.sharedInstance.getCapabilitiesServerInt(account: appDelegate.account, elements: NCElementsJSON.shared.capabilitiesVersionMajor)
  111. if serverVersionMajor >= Int(k_trash_version_available) {
  112. item = NCCommunicationExternalSite()
  113. item.name = "_trash_view_"
  114. item.icon = "trash"
  115. item.url = "segueTrash"
  116. functionMenu.append(item)
  117. }
  118. // ITEM : Settings
  119. item = NCCommunicationExternalSite()
  120. item.name = "_settings_"
  121. item.icon = "settings"
  122. item.url = "segueSettings"
  123. settingsMenu.append(item)
  124. if (quotaMenu.count > 0) {
  125. let item = quotaMenu[0]
  126. labelQuotaExternalSite.text = item.name
  127. }
  128. changeUserProfile()
  129. // ITEM : External
  130. if NCBrandOptions.sharedInstance.disable_more_external_site == false {
  131. if let externalSites = NCManageDatabase.sharedInstance.getAllExternalSites(account: appDelegate.account) {
  132. for externalSite in externalSites {
  133. if (externalSite.type == "link" && externalSite.name != "" && externalSite.url != "") {
  134. item = NCCommunicationExternalSite()
  135. item.name = externalSite.name
  136. item.url = externalSite.url
  137. item.icon = "world"
  138. externalSiteMenu.append(item)
  139. }
  140. if (externalSite.type == "settings") {
  141. item.icon = "settings"
  142. settingsMenu.append(item)
  143. }
  144. if (externalSite.type == "quota") {
  145. quotaMenu.append(item)
  146. }
  147. }
  148. tableView.reloadData()
  149. } else {
  150. tableView.reloadData()
  151. }
  152. } else {
  153. tableView.reloadData()
  154. }
  155. }
  156. @objc func changeTheming() {
  157. view.backgroundColor = NCBrandColor.sharedInstance.backgroundForm
  158. tableView.backgroundColor = NCBrandColor.sharedInstance.backgroundForm
  159. tableView.separatorColor = NCBrandColor.sharedInstance.separator
  160. tableView.reloadData()
  161. viewQuota.backgroundColor = NCBrandColor.sharedInstance.backgroundForm
  162. progressQuota.progressTintColor = NCBrandColor.sharedInstance.brandElement
  163. }
  164. @objc func changeUserProfile() {
  165. // Display Name user & Quota
  166. var quota: String = ""
  167. guard let tabAccount = NCManageDatabase.sharedInstance.getAccountActive() else {
  168. return
  169. }
  170. self.tabAccount = tabAccount
  171. if (tabAccount.quotaRelative > 0) {
  172. progressQuota.progress = Float(tabAccount.quotaRelative) / 100
  173. } else {
  174. progressQuota.progress = 0
  175. }
  176. switch Double(tabAccount.quotaTotal) {
  177. case Double(-1):
  178. quota = "0"
  179. case Double(-2):
  180. quota = NSLocalizedString("_quota_space_unknown_", comment: "")
  181. case Double(-3):
  182. quota = NSLocalizedString("_quota_space_unlimited_", comment: "")
  183. default:
  184. quota = CCUtility.transformedSize(Double(tabAccount.quotaTotal))
  185. }
  186. let quotaUsed: String = CCUtility.transformedSize(Double(tabAccount.quotaUsed))
  187. labelQuota.text = String.localizedStringWithFormat(NSLocalizedString("_quota_using_", comment: ""), quotaUsed, quota)
  188. }
  189. func numberOfSections(in tableView: UITableView) -> Int {
  190. if (externalSiteMenu.count == 0) {
  191. return 3
  192. } else {
  193. return 4
  194. }
  195. }
  196. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  197. var cont = 0
  198. if (section == 0) {
  199. cont = tabAccount == nil ? 0 : 1
  200. } else if (section == 1) {
  201. // Menu Normal
  202. cont = functionMenu.count
  203. } else {
  204. switch (numberOfSections(in: tableView)) {
  205. case 3:
  206. // Menu Settings
  207. if (section == 2) {
  208. cont = settingsMenu.count
  209. }
  210. case 4:
  211. // Menu External Site
  212. if (section == 2) {
  213. cont = externalSiteMenu.count
  214. }
  215. // Menu Settings
  216. if (section == 3) {
  217. cont = settingsMenu.count
  218. }
  219. default:
  220. cont = 0
  221. }
  222. }
  223. return cont
  224. }
  225. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  226. let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CCCellMore
  227. var item = NCCommunicationExternalSite()
  228. // change color selection and disclosure indicator
  229. let selectionColor: UIView = UIView()
  230. selectionColor.backgroundColor = NCBrandColor.sharedInstance.select
  231. cell.selectedBackgroundView = selectionColor
  232. cell.backgroundColor = NCBrandColor.sharedInstance.backgroundCell
  233. cell.accessoryType = UITableViewCell.AccessoryType.disclosureIndicator
  234. if (indexPath.section == 0) {
  235. let fileNamePath = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(appDelegate.user, urlBase: appDelegate.urlBase) + "-" + appDelegate.user + ".png"
  236. if let themingAvatarFile = UIImage.init(contentsOfFile: fileNamePath) {
  237. cell.imageIcon?.image = themingAvatarFile
  238. } else {
  239. cell.imageIcon?.image = UIImage.init(named: "moreAvatar")
  240. }
  241. cell.imageIcon?.layer.masksToBounds = true
  242. cell.imageIcon?.layer.cornerRadius = cell.imageIcon.frame.size.width / 2
  243. if let account = tabAccount {
  244. cell.labelText?.text = account.displayName
  245. cell.labelText.textColor = NCBrandColor.sharedInstance.textView
  246. }
  247. return cell
  248. } else {
  249. // Menu Normal
  250. if (indexPath.section == 1) {
  251. item = functionMenu[indexPath.row]
  252. }
  253. // Menu External Site
  254. if (numberOfSections(in: tableView) == 4 && indexPath.section == 2) {
  255. item = externalSiteMenu[indexPath.row]
  256. }
  257. // Menu Settings
  258. if ((numberOfSections(in: tableView) == 3 && indexPath.section == 2) || (numberOfSections(in: tableView) == 4 && indexPath.section == 3)) {
  259. item = settingsMenu[indexPath.row]
  260. }
  261. cell.imageIcon?.image = CCGraphics.changeThemingColorImage(UIImage.init(named: item.icon), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon)
  262. cell.labelText?.text = NSLocalizedString(item.name, comment: "")
  263. cell.labelText.textColor = NCBrandColor.sharedInstance.textView
  264. }
  265. return cell
  266. }
  267. // method to run when table view cell is tapped
  268. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  269. var item = NCCommunicationExternalSite()
  270. if indexPath.section == 0 {
  271. tapImageLogoManageAccount()
  272. return
  273. }
  274. // Menu Function
  275. if indexPath.section == 1 {
  276. item = functionMenu[indexPath.row]
  277. }
  278. // Menu External Site
  279. if (numberOfSections(in: tableView) == 4 && indexPath.section == 2) {
  280. item = externalSiteMenu[indexPath.row]
  281. }
  282. // Menu Settings
  283. if ((numberOfSections(in: tableView) == 3 && indexPath.section == 2) || (numberOfSections(in: tableView) == 4 && indexPath.section == 3)) {
  284. item = settingsMenu[indexPath.row]
  285. }
  286. // Action
  287. if item.url.contains("segue") && !item.url.contains("//") {
  288. self.navigationController?.performSegue(withIdentifier: item.url, sender: self)
  289. } else if item.url.contains("openStoryboard") && !item.url.contains("//") {
  290. let nameStoryboard = item.url.replacingOccurrences(of: "openStoryboard", with: "")
  291. let storyboard = UIStoryboard(name: nameStoryboard, bundle: nil)
  292. let controller = storyboard.instantiateInitialViewController()! //instantiateViewController(withIdentifier: nameStoryboard)
  293. self.present(controller, animated: true, completion: nil)
  294. } else if item.url.contains("//") {
  295. let browserWebVC = UIStoryboard(name: "NCBrowserWeb", bundle: nil).instantiateInitialViewController() as! NCBrowserWeb
  296. browserWebVC.urlBase = item.url
  297. browserWebVC.isHiddenButtonExit = true
  298. browserWebVC.titleBrowser = item.name
  299. self.navigationController?.pushViewController(browserWebVC, animated: true)
  300. self.navigationController?.navigationBar.isHidden = false
  301. } else if item.url == "logout" {
  302. let alertController = UIAlertController(title: "", message: NSLocalizedString("_want_delete_", comment: ""), preferredStyle: .alert)
  303. let actionYes = UIAlertAction(title: NSLocalizedString("_yes_delete_", comment: ""), style: .default) { (action: UIAlertAction) in
  304. let manageAccount = CCManageAccount()
  305. manageAccount.delete(self.appDelegate.account)
  306. self.appDelegate.openLoginView(self, selector: Int(k_intro_login), openLoginWeb: false)
  307. }
  308. let actionNo = UIAlertAction(title: NSLocalizedString("_no_delete_", comment: ""), style: .default) { (action: UIAlertAction) in
  309. print("You've pressed No button")
  310. }
  311. alertController.addAction(actionYes)
  312. alertController.addAction(actionNo)
  313. self.present(alertController, animated: true, completion: nil)
  314. }
  315. }
  316. @objc func tapLabelQuotaExternalSite() {
  317. if (quotaMenu.count > 0) {
  318. let item = quotaMenu[0]
  319. let browserWebVC = UIStoryboard(name: "NCBrowserWeb", bundle: nil).instantiateInitialViewController() as! NCBrowserWeb
  320. browserWebVC.urlBase = item.url
  321. browserWebVC.isHiddenButtonExit = true
  322. self.navigationController?.pushViewController(browserWebVC, animated: true)
  323. self.navigationController?.navigationBar.isHidden = false
  324. }
  325. }
  326. @objc func tapImageLogoManageAccount() {
  327. let controller = CCManageAccount.init()
  328. self.navigationController?.pushViewController(controller, animated: true)
  329. }
  330. }
  331. class CCCellMore: UITableViewCell {
  332. @IBOutlet weak var labelText: UILabel!
  333. @IBOutlet weak var imageIcon: UIImageView!
  334. }