CCMore.swift 17 KB

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