NCMore.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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: NCGlobal.shared.notificationCenterChangeUserProfile), object: nil)
  53. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterChangeTheming), object: nil)
  54. changeTheming()
  55. }
  56. override func viewWillAppear(_ animated: Bool) {
  57. super.viewWillAppear(animated)
  58. appDelegate.activeViewController = self
  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.shared.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.shared.getCapabilitiesServerInt(account: appDelegate.account, elements: NCElementsJSON.shared.capabilitiesVersionMajor)
  115. if serverVersionMajor >= NCGlobal.shared.nextcloudVersion15 {
  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.shared.disable_more_external_site == false {
  135. if let externalSites = NCManageDatabase.shared.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.shared.backgroundView
  162. tableView.backgroundColor = NCBrandColor.shared.backgroundView
  163. tableView.separatorColor = NCBrandColor.shared.separator
  164. tableView.reloadData()
  165. viewQuota.backgroundColor = NCBrandColor.shared.backgroundView
  166. progressQuota.progressTintColor = NCBrandColor.shared.brandElement
  167. }
  168. @objc func changeUserProfile() {
  169. // Display Name user & Quota
  170. var quota: String = ""
  171. guard let tabAccount = NCManageDatabase.shared.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 tabAccount.quotaTotal {
  181. case -1:
  182. quota = "0"
  183. case -2:
  184. quota = NSLocalizedString("_quota_space_unknown_", comment: "")
  185. case -3:
  186. quota = NSLocalizedString("_quota_space_unlimited_", comment: "")
  187. default:
  188. quota = CCUtility.transformedSize(tabAccount.quotaTotal)
  189. }
  190. let quotaUsed: String = CCUtility.transformedSize(tabAccount.quotaUsed)
  191. labelQuota.text = String.localizedStringWithFormat(NSLocalizedString("_quota_using_", comment: ""), quotaUsed, quota)
  192. DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
  193. self.tableView.reloadData()
  194. }
  195. }
  196. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  197. if indexPath.section == 0 {
  198. return 100
  199. } else {
  200. return 50
  201. }
  202. }
  203. func numberOfSections(in tableView: UITableView) -> Int {
  204. if (externalSiteMenu.count == 0) {
  205. return 3
  206. } else {
  207. return 4
  208. }
  209. }
  210. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  211. var cont = 0
  212. if (section == 0) {
  213. cont = tabAccount == nil ? 0 : 1
  214. } else if (section == 1) {
  215. // Menu Normal
  216. cont = functionMenu.count
  217. } else {
  218. switch (numberOfSections(in: tableView)) {
  219. case 3:
  220. // Menu Settings
  221. if (section == 2) {
  222. cont = settingsMenu.count
  223. }
  224. case 4:
  225. // Menu External Site
  226. if (section == 2) {
  227. cont = externalSiteMenu.count
  228. }
  229. // Menu Settings
  230. if (section == 3) {
  231. cont = settingsMenu.count
  232. }
  233. default:
  234. cont = 0
  235. }
  236. }
  237. return cont
  238. }
  239. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  240. var item = NCCommunicationExternalSite()
  241. // change color selection and disclosure indicator
  242. let selectionColor: UIView = UIView()
  243. selectionColor.backgroundColor = NCBrandColor.shared.select
  244. if (indexPath.section == 0) {
  245. let cell = tableView.dequeueReusableCell(withIdentifier: "userCell", for: indexPath) as! NCMoreUserCell
  246. cell.avatar.image = nil
  247. cell.icon.image = nil
  248. cell.status.text = ""
  249. cell.displayName.text = ""
  250. var fileNamePath = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(appDelegate.user, urlBase: appDelegate.urlBase) + "-" + appDelegate.user
  251. fileNamePath = fileNamePath + ".png"
  252. if let themingAvatarFile = UIImage.init(contentsOfFile: fileNamePath) {
  253. cell.avatar?.image = themingAvatarFile
  254. } else {
  255. cell.avatar?.image = UIImage.init(named: "moreAvatar")
  256. }
  257. cell.avatar?.layer.masksToBounds = true
  258. cell.avatar?.layer.cornerRadius = cell.avatar.frame.size.width / 2
  259. if let account = tabAccount {
  260. cell.displayName?.text = account.displayName
  261. cell.displayName.textColor = NCBrandColor.shared.textView
  262. }
  263. cell.selectedBackgroundView = selectionColor
  264. cell.backgroundColor = NCBrandColor.shared.backgroundForm
  265. cell.accessoryType = UITableViewCell.AccessoryType.disclosureIndicator
  266. if NCManageDatabase.shared.getCapabilitiesServerBool(account: appDelegate.account, elements: NCElementsJSON.shared.capabilitiesUserStatusEnabled, exists: false) {
  267. if let account = NCManageDatabase.shared.getAccount(predicate: NSPredicate(format: "account == %@", appDelegate.account)) {
  268. let status = NCUtility.shared.getUserStatus(userIcon: account.userStatusIcon, userStatus: account.userStatusStatus, userMessage: account.userStatusMessage)
  269. cell.icon.image = status.onlineStatus
  270. cell.status.text = status.statusMessage
  271. cell.status.textColor = NCBrandColor.shared.textView
  272. }
  273. }
  274. return cell
  275. } else {
  276. let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CCCellMore
  277. // Menu Normal
  278. if (indexPath.section == 1) {
  279. item = functionMenu[indexPath.row]
  280. }
  281. // Menu External Site
  282. if (numberOfSections(in: tableView) == 4 && indexPath.section == 2) {
  283. item = externalSiteMenu[indexPath.row]
  284. }
  285. // Menu Settings
  286. if ((numberOfSections(in: tableView) == 3 && indexPath.section == 2) || (numberOfSections(in: tableView) == 4 && indexPath.section == 3)) {
  287. item = settingsMenu[indexPath.row]
  288. }
  289. cell.imageIcon?.image = UIImage.init(named: item.icon)?.image(color: NCBrandColor.shared.icon, size: 25)
  290. cell.labelText?.text = NSLocalizedString(item.name, comment: "")
  291. cell.labelText.textColor = NCBrandColor.shared.textView
  292. cell.selectedBackgroundView = selectionColor
  293. cell.backgroundColor = NCBrandColor.shared.backgroundForm
  294. cell.accessoryType = UITableViewCell.AccessoryType.disclosureIndicator
  295. return cell
  296. }
  297. }
  298. // method to run when table view cell is tapped
  299. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  300. var item = NCCommunicationExternalSite()
  301. if indexPath.section == 0 {
  302. tapImageLogoManageAccount()
  303. return
  304. }
  305. // Menu Function
  306. if indexPath.section == 1 {
  307. item = functionMenu[indexPath.row]
  308. }
  309. // Menu External Site
  310. if (numberOfSections(in: tableView) == 4 && indexPath.section == 2) {
  311. item = externalSiteMenu[indexPath.row]
  312. }
  313. // Menu Settings
  314. if ((numberOfSections(in: tableView) == 3 && indexPath.section == 2) || (numberOfSections(in: tableView) == 4 && indexPath.section == 3)) {
  315. item = settingsMenu[indexPath.row]
  316. }
  317. // Action
  318. if item.url.contains("segue") && !item.url.contains("//") {
  319. self.navigationController?.performSegue(withIdentifier: item.url, sender: self)
  320. } else if item.url.contains("openStoryboard") && !item.url.contains("//") {
  321. let nameStoryboard = item.url.replacingOccurrences(of: "openStoryboard", with: "")
  322. let storyboard = UIStoryboard(name: nameStoryboard, bundle: nil)
  323. let controller = storyboard.instantiateInitialViewController()! //instantiateViewController(withIdentifier: nameStoryboard)
  324. self.present(controller, animated: true, completion: nil)
  325. } else if item.url.contains("//") {
  326. let browserWebVC = UIStoryboard(name: "NCBrowserWeb", bundle: nil).instantiateInitialViewController() as! NCBrowserWeb
  327. browserWebVC.urlBase = item.url
  328. browserWebVC.isHiddenButtonExit = true
  329. browserWebVC.titleBrowser = item.name
  330. self.navigationController?.pushViewController(browserWebVC, animated: true)
  331. self.navigationController?.navigationBar.isHidden = false
  332. } else if item.url == "logout" {
  333. let alertController = UIAlertController(title: "", message: NSLocalizedString("_want_delete_", comment: ""), preferredStyle: .alert)
  334. let actionYes = UIAlertAction(title: NSLocalizedString("_yes_delete_", comment: ""), style: .default) { (action: UIAlertAction) in
  335. let manageAccount = CCManageAccount()
  336. manageAccount.delete(self.appDelegate.account)
  337. self.appDelegate.openLogin(viewController:self, selector: NCGlobal.shared.introLogin, openLoginWeb: false)
  338. }
  339. let actionNo = UIAlertAction(title: NSLocalizedString("_no_delete_", comment: ""), style: .default) { (action: UIAlertAction) in
  340. print("You've pressed No button")
  341. }
  342. alertController.addAction(actionYes)
  343. alertController.addAction(actionNo)
  344. self.present(alertController, animated: true, completion: nil)
  345. }
  346. }
  347. @objc func tapLabelQuotaExternalSite() {
  348. if (quotaMenu.count > 0) {
  349. let item = quotaMenu[0]
  350. let browserWebVC = UIStoryboard(name: "NCBrowserWeb", bundle: nil).instantiateInitialViewController() as! NCBrowserWeb
  351. browserWebVC.urlBase = item.url
  352. browserWebVC.isHiddenButtonExit = true
  353. self.navigationController?.pushViewController(browserWebVC, animated: true)
  354. self.navigationController?.navigationBar.isHidden = false
  355. }
  356. }
  357. @objc func tapImageLogoManageAccount() {
  358. let controller = CCManageAccount.init()
  359. self.navigationController?.pushViewController(controller, animated: true)
  360. }
  361. }
  362. class CCCellMore: UITableViewCell {
  363. @IBOutlet weak var labelText: UILabel!
  364. @IBOutlet weak var imageIcon: UIImageView!
  365. }
  366. class NCMoreUserCell: UITableViewCell {
  367. @IBOutlet weak var displayName: UILabel!
  368. @IBOutlet weak var avatar: UIImageView!
  369. @IBOutlet weak var icon: UIImageView!
  370. @IBOutlet weak var status: UILabel!
  371. }