CCMore.swift 17 KB

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