NCMore.swift 17 KB

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