CCMore.swift 18 KB

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