CCMore.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. //
  2. // CCMore.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 03/04/17.
  6. // Copyright © 2017 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  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, CCLoginDelegate, CCLoginDelegateWeb {
  25. @IBOutlet weak var themingBackground: UIImageView!
  26. @IBOutlet weak var themingAvatar: UIImageView!
  27. @IBOutlet weak var labelUsername: UILabel!
  28. @IBOutlet weak var tableView: UITableView!
  29. @IBOutlet weak var labelQuota: UILabel!
  30. @IBOutlet weak var labelQuotaExternalSite: UILabel!
  31. @IBOutlet weak var progressQuota: UIProgressView!
  32. var functionMenu = [OCExternalSites]()
  33. var externalSiteMenu = [OCExternalSites]()
  34. var settingsMenu = [OCExternalSites]()
  35. var quotaMenu = [OCExternalSites]()
  36. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  37. var listExternalSite: [tableExternalSites]?
  38. var tabAccount : tableAccount?
  39. //var loginWeb : CCLoginWeb!
  40. override func viewDidLoad() {
  41. super.viewDidLoad()
  42. tableView.delegate = self
  43. tableView.dataSource = self
  44. tableView.separatorColor = NCBrandColor.sharedInstance.seperator
  45. if #available(iOS 11, *) {
  46. //tableView.contentInsetAdjustmentBehavior = .never
  47. }
  48. themingBackground.image = #imageLiteral(resourceName: "themingBackground")
  49. // create tap gesture recognizer
  50. let tapQuota = UITapGestureRecognizer(target: self, action: #selector(tapLabelQuotaExternalSite))
  51. labelQuotaExternalSite.isUserInteractionEnabled = true
  52. labelQuotaExternalSite.addGestureRecognizer(tapQuota)
  53. let tapImageLogo = UITapGestureRecognizer(target: self, action: #selector(tapImageLogoManageAccount))
  54. themingBackground.isUserInteractionEnabled = true
  55. themingBackground.addGestureRecognizer(tapImageLogo)
  56. // Notification
  57. NotificationCenter.default.addObserver(self, selector: #selector(self.changeTheming), name: NSNotification.Name(rawValue: "changeTheming"), object: nil)
  58. NotificationCenter.default.addObserver(self, selector: #selector(self.changeUserProfile), name: NSNotification.Name(rawValue: "changeUserProfile"), object: nil)
  59. }
  60. // Apparirà
  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 : External
  89. if NCBrandOptions.sharedInstance.disable_more_external_site == false {
  90. listExternalSite = NCManageDatabase.sharedInstance.getAllExternalSites()
  91. if listExternalSite != nil {
  92. for table in listExternalSite! {
  93. item = OCExternalSites.init()
  94. item.name = table.name
  95. item.url = table.url
  96. item.icon = table.icon
  97. if (table.type == "link") {
  98. item.icon = "world"
  99. externalSiteMenu.append(item)
  100. }
  101. if (table.type == "settings") {
  102. item.icon = "settings"
  103. settingsMenu.append(item)
  104. }
  105. if (table.type == "quota") {
  106. quotaMenu.append(item)
  107. }
  108. }
  109. }
  110. }
  111. // ITEM : Settings
  112. item = OCExternalSites.init()
  113. item.name = "_settings_"
  114. item.icon = "settings"
  115. item.url = "segueSettings"
  116. settingsMenu.append(item)
  117. if (quotaMenu.count > 0) {
  118. let item = quotaMenu[0]
  119. labelQuotaExternalSite.text = item.name
  120. }
  121. // User data & Theming
  122. changeUserProfile()
  123. changeTheming()
  124. // Title
  125. self.navigationItem.title = NSLocalizedString("_more_", comment: "")
  126. // Aspect
  127. appDelegate.aspectNavigationControllerBar(self.navigationController?.navigationBar, online: appDelegate.reachability.isReachable(), hidden: false)
  128. appDelegate.aspectTabBar(self.tabBarController?.tabBar, hidden: false)
  129. // +
  130. appDelegate.plusButtonVisibile(true)
  131. self.navigationController?.setNavigationBarHidden(true, animated: animated)
  132. tableView.reloadData()
  133. }
  134. override func viewWillDisappear(_ animated: Bool) {
  135. super.viewWillDisappear(animated)
  136. self.navigationController?.setNavigationBarHidden(false, animated: animated)
  137. }
  138. @objc func changeTheming() {
  139. self.view.backgroundColor = NCBrandColor.sharedInstance.brand
  140. let fileNamePath = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(appDelegate.activeUser, activeUrl: appDelegate.activeUrl) + "-themingBackground.png"
  141. if let theminBackgroundFile = UIImage.init(contentsOfFile: fileNamePath) {
  142. themingBackground.image = theminBackgroundFile
  143. } else {
  144. themingBackground.image = #imageLiteral(resourceName: "themingBackground")
  145. }
  146. if (self.isViewLoaded && (self.view.window != nil)) {
  147. appDelegate.changeTheming(self)
  148. }
  149. }
  150. @objc func changeUserProfile() {
  151. let fileNamePath = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(appDelegate.activeUser, activeUrl: appDelegate.activeUrl) + "-avatar.png"
  152. var quota: String = ""
  153. if let themingAvatarFile = UIImage.init(contentsOfFile: fileNamePath) {
  154. themingAvatar.image = themingAvatarFile
  155. } else {
  156. themingAvatar.image = UIImage.init(named: "moreAvatar")
  157. }
  158. // Display Name user & Quota
  159. guard let tabAccount = NCManageDatabase.sharedInstance.getAccountActive() else {
  160. return
  161. }
  162. if tabAccount.displayName.isEmpty {
  163. labelUsername.text = tabAccount.user
  164. }
  165. else{
  166. labelUsername.text = tabAccount.displayName
  167. }
  168. // Shadow labelUsername TEST BLUR
  169. /*
  170. labelUsername.layer.shadowColor = UIColor.black.cgColor
  171. labelUsername.layer.shadowRadius = 4
  172. labelUsername.layer.shadowOpacity = 0.8
  173. labelUsername.layer.shadowOffset = CGSize(width: 0, height: 0)
  174. labelUsername.layer.masksToBounds = false
  175. */
  176. if (tabAccount.quotaRelative > 0) {
  177. progressQuota.progress = Float(tabAccount.quotaRelative) / 100
  178. } else {
  179. progressQuota.progress = 0
  180. }
  181. progressQuota.progressTintColor = NCBrandColor.sharedInstance.brandElement
  182. switch Double(tabAccount.quotaTotal) {
  183. case Double(k_quota_space_not_computed):
  184. quota = "0"
  185. case Double(k_quota_space_unknown):
  186. quota = NSLocalizedString("_quota_space_unknown_", comment: "")
  187. case Double(k_quota_space_unlimited):
  188. quota = NSLocalizedString("_quota_space_unlimited_", comment: "")
  189. default:
  190. quota = CCUtility.transformedSize(Double(tabAccount.quotaTotal))
  191. }
  192. let quotaUsed : String = CCUtility.transformedSize(Double(tabAccount.quotaUsed))
  193. labelQuota.text = String.localizedStringWithFormat(NSLocalizedString("_quota_using_", comment: ""), quotaUsed, quota)
  194. }
  195. func numberOfSections(in tableView: UITableView) -> Int {
  196. if (externalSiteMenu.count == 0) {
  197. return 2
  198. } else {
  199. return 3
  200. }
  201. }
  202. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  203. if (section == 0) {
  204. return 0.1
  205. } else {
  206. return 30
  207. }
  208. }
  209. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  210. var cont = 0
  211. // Menu Normal
  212. if (section == 0) {
  213. cont = functionMenu.count
  214. } else {
  215. switch (numberOfSections(in: tableView)) {
  216. case 2:
  217. // Menu Settings
  218. if (section == 1) {
  219. cont = settingsMenu.count
  220. }
  221. case 3:
  222. // Menu External Site
  223. if (section == 1) {
  224. cont = externalSiteMenu.count
  225. }
  226. // Menu Settings
  227. if (section == 2) {
  228. cont = settingsMenu.count
  229. }
  230. default:
  231. cont = 0
  232. }
  233. }
  234. return cont
  235. }
  236. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  237. let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CCCellMore
  238. var item: OCExternalSites = OCExternalSites.init()
  239. // change color selection and disclosure indicator
  240. let selectionColor : UIView = UIView.init()
  241. selectionColor.backgroundColor = NCBrandColor.sharedInstance.getColorSelectBackgrond()
  242. cell.selectedBackgroundView = selectionColor
  243. cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
  244. // Menu Normal
  245. if (indexPath.section == 0) {
  246. item = functionMenu[indexPath.row]
  247. } else {
  248. // Menu External Site
  249. if (numberOfSections(in: tableView) == 3 && indexPath.section == 1) {
  250. item = externalSiteMenu[indexPath.row]
  251. }
  252. // Menu Settings
  253. if ((numberOfSections(in: tableView) == 2 && indexPath.section == 1) || (numberOfSections(in: tableView) == 3 && indexPath.section == 2)) {
  254. item = settingsMenu[indexPath.row]
  255. }
  256. }
  257. cell.imageIcon?.image = CCGraphics.changeThemingColorImage(UIImage.init(named: item.icon), multiplier: 2, color: NCBrandColor.sharedInstance.icon)
  258. cell.labelText?.text = NSLocalizedString(item.name, comment: "")
  259. cell.labelText.textColor = NCBrandColor.sharedInstance.textView
  260. return cell
  261. }
  262. // method to run when table view cell is tapped
  263. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  264. var item: OCExternalSites = OCExternalSites.init()
  265. // Menu Function
  266. if indexPath.section == 0 {
  267. item = functionMenu[indexPath.row]
  268. }
  269. // Menu External Site
  270. if (numberOfSections(in: tableView) == 3 && indexPath.section == 1) {
  271. item = externalSiteMenu[indexPath.row]
  272. }
  273. // Menu Settings
  274. if ((numberOfSections(in: tableView) == 2 && indexPath.section == 1) || (numberOfSections(in: tableView) == 3 && indexPath.section == 2)) {
  275. item = settingsMenu[indexPath.row]
  276. }
  277. // Action
  278. if item.url.contains("segue") && !item.url.contains("//") {
  279. self.navigationController?.performSegue(withIdentifier: item.url, sender: self)
  280. } else if item.url.contains("open") && !item.url.contains("//") {
  281. let nameStoryboard = String(item.url[..<item.url.index(item.url.startIndex, offsetBy: 4)])
  282. //let nameStoryboard = item.url.substring(from: item.url.index(item.url.startIndex, offsetBy: 4))
  283. let storyboard = UIStoryboard(name: nameStoryboard, bundle: nil)
  284. let controller = storyboard.instantiateViewController(withIdentifier: nameStoryboard)
  285. self.present(controller, animated: true, completion: nil)
  286. } else if item.url.contains("//") {
  287. if (self.splitViewController?.isCollapsed)! {
  288. let webVC = SwiftWebVC(urlString: item.url, hideToolbar: false)
  289. webVC.delegate = self
  290. self.navigationController?.pushViewController(webVC, animated: true)
  291. self.navigationController?.navigationBar.isHidden = false
  292. } else {
  293. let webVC = SwiftModalWebVC(urlString: item.url, theme: .dark, color: UIColor.clear, colorText: UIColor.black, doneButtonVisible: true)
  294. webVC.delegateWeb = self
  295. self.present(webVC, animated: true, completion: nil)
  296. }
  297. } else if item.url == "logout" {
  298. let alertController = UIAlertController(title: "", message: NSLocalizedString("_want_delete_", comment: ""), preferredStyle: .alert)
  299. let actionYes = UIAlertAction(title: NSLocalizedString("_yes_delete_", comment: ""), style: .default) { (action:UIAlertAction) in
  300. let manageAccount = CCManageAccount()
  301. manageAccount.delete(self.appDelegate.activeAccount)
  302. self.appDelegate.openLoginView(self, loginType: Int(k_login_Add_Forced), selector: Int(k_intro_login))
  303. }
  304. let actionNo = UIAlertAction(title: NSLocalizedString("_no_delete_", comment: ""), style: .default) { (action:UIAlertAction) in
  305. print("You've pressed No button");
  306. }
  307. alertController.addAction(actionYes)
  308. alertController.addAction(actionNo)
  309. self.present(alertController, animated: true, completion:nil)
  310. }
  311. }
  312. @objc func tapLabelQuotaExternalSite() {
  313. if (quotaMenu.count > 0) {
  314. let item = quotaMenu[0]
  315. if (self.splitViewController?.isCollapsed)! {
  316. let webVC = SwiftWebVC(urlString: item.url, hideToolbar: true)
  317. webVC.delegate = self
  318. self.navigationController?.pushViewController(webVC, animated: true)
  319. self.navigationController?.navigationBar.isHidden = false
  320. } else {
  321. let webVC = SwiftModalWebVC(urlString: item.url)
  322. webVC.delegateWeb = self
  323. self.present(webVC, animated: true, completion: nil)
  324. }
  325. }
  326. }
  327. @objc func tapImageLogoManageAccount() {
  328. let controller = CCManageAccount.init()
  329. self.navigationController?.pushViewController(controller, animated: true)
  330. }
  331. func loginSuccess(_ loginType: NSInteger) {
  332. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "initializeMain"), object: nil)
  333. appDelegate.selectedTabBarController(Int(k_tabBarApplicationIndexFile))
  334. }
  335. func loginClose() {
  336. appDelegate.activeLogin = nil
  337. }
  338. func loginWebClose() {
  339. appDelegate.activeLoginWeb = nil
  340. }
  341. }
  342. extension CCMore: SwiftModalWebVCDelegate, SwiftWebVCDelegate{
  343. public func didStartLoading() {
  344. print("Started loading.")
  345. }
  346. public func didReceiveServerRedirectForProvisionalNavigation(url: URL) {
  347. let urlString: String = url.absoluteString.lowercased()
  348. // Protocol close webVC
  349. if (urlString.contains(NCBrandOptions.sharedInstance.webCloseViewProtocolPersonalized) == true) {
  350. if (self.presentingViewController != nil) {
  351. self.dismiss(animated: true, completion: nil)
  352. } else {
  353. self.navigationController?.popToRootViewController(animated: true)
  354. }
  355. }
  356. }
  357. public func didFinishLoading(success: Bool) {
  358. print("Finished loading. Success: \(success).")
  359. }
  360. public func didFinishLoading(success: Bool, url: URL) {
  361. print("Finished loading. Success: \(success).")
  362. }
  363. public func decidePolicyForNavigationAction(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
  364. decisionHandler(.allow)
  365. }
  366. }
  367. class CCCellMore: UITableViewCell {
  368. @IBOutlet weak var labelText: UILabel!
  369. @IBOutlet weak var imageIcon: UIImageView!
  370. }