CCMore.swift 18 KB

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