NCMore.swift 17 KB

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