CCMore.swift 18 KB

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