CCMore.swift 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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, 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 : 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. // User data & Theming
  148. changeUserProfile()
  149. changeTheming()
  150. // Title
  151. self.navigationItem.title = NSLocalizedString("_more_", comment: "")
  152. // Aspect
  153. appDelegate.aspectNavigationControllerBar(self.navigationController?.navigationBar, online: appDelegate.reachability.isReachable(), hidden: false)
  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. if (self.isViewLoaded && (self.view.window != nil)) {
  173. appDelegate.changeTheming(self)
  174. }
  175. }
  176. @objc func changeUserProfile() {
  177. let fileNamePath = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(appDelegate.activeUser, activeUrl: appDelegate.activeUrl) + "-" + appDelegate.activeUser + ".png"
  178. var quota: String = ""
  179. if let themingAvatarFile = UIImage.init(contentsOfFile: fileNamePath) {
  180. themingAvatar.image = themingAvatarFile
  181. } else {
  182. themingAvatar.image = UIImage.init(named: "moreAvatar")
  183. }
  184. // Display Name user & Quota
  185. guard let tabAccount = NCManageDatabase.sharedInstance.getAccountActive() else {
  186. return
  187. }
  188. if tabAccount.displayName.isEmpty {
  189. labelUsername.text = tabAccount.user
  190. }
  191. else{
  192. labelUsername.text = tabAccount.displayName
  193. }
  194. // Shadow labelUsername TEST BLUR
  195. /*
  196. labelUsername.layer.shadowColor = UIColor.black.cgColor
  197. labelUsername.layer.shadowRadius = 4
  198. labelUsername.layer.shadowOpacity = 0.8
  199. labelUsername.layer.shadowOffset = CGSize(width: 0, height: 0)
  200. labelUsername.layer.masksToBounds = false
  201. */
  202. if (tabAccount.quotaRelative > 0) {
  203. progressQuota.progress = Float(tabAccount.quotaRelative) / 100
  204. } else {
  205. progressQuota.progress = 0
  206. }
  207. progressQuota.progressTintColor = NCBrandColor.sharedInstance.brandElement
  208. switch Double(tabAccount.quotaTotal) {
  209. case Double(k_quota_space_not_computed):
  210. quota = "0"
  211. case Double(k_quota_space_unknown):
  212. quota = NSLocalizedString("_quota_space_unknown_", comment: "")
  213. case Double(k_quota_space_unlimited):
  214. quota = NSLocalizedString("_quota_space_unlimited_", comment: "")
  215. default:
  216. quota = CCUtility.transformedSize(Double(tabAccount.quotaTotal))
  217. }
  218. let quotaUsed : String = CCUtility.transformedSize(Double(tabAccount.quotaUsed))
  219. labelQuota.text = String.localizedStringWithFormat(NSLocalizedString("_quota_using_", comment: ""), quotaUsed, quota)
  220. }
  221. func numberOfSections(in tableView: UITableView) -> Int {
  222. if (externalSiteMenu.count == 0) {
  223. return 2
  224. } else {
  225. return 3
  226. }
  227. }
  228. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  229. if (section == 0) {
  230. return 0.1
  231. } else {
  232. return 30
  233. }
  234. }
  235. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  236. var cont = 0
  237. // Menu Normal
  238. if (section == 0) {
  239. cont = functionMenu.count
  240. } else {
  241. switch (numberOfSections(in: tableView)) {
  242. case 2:
  243. // Menu Settings
  244. if (section == 1) {
  245. cont = settingsMenu.count
  246. }
  247. case 3:
  248. // Menu External Site
  249. if (section == 1) {
  250. cont = externalSiteMenu.count
  251. }
  252. // Menu Settings
  253. if (section == 2) {
  254. cont = settingsMenu.count
  255. }
  256. default:
  257. cont = 0
  258. }
  259. }
  260. return cont
  261. }
  262. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  263. let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CCCellMore
  264. var item: OCExternalSites = OCExternalSites.init()
  265. // change color selection and disclosure indicator
  266. let selectionColor : UIView = UIView.init()
  267. selectionColor.backgroundColor = NCBrandColor.sharedInstance.getColorSelectBackgrond()
  268. cell.selectedBackgroundView = selectionColor
  269. cell.accessoryType = UITableViewCell.AccessoryType.disclosureIndicator
  270. // Menu Normal
  271. if (indexPath.section == 0) {
  272. item = functionMenu[indexPath.row]
  273. } else {
  274. // Menu External Site
  275. if (numberOfSections(in: tableView) == 3 && indexPath.section == 1) {
  276. item = externalSiteMenu[indexPath.row]
  277. }
  278. // Menu Settings
  279. if ((numberOfSections(in: tableView) == 2 && indexPath.section == 1) || (numberOfSections(in: tableView) == 3 && indexPath.section == 2)) {
  280. item = settingsMenu[indexPath.row]
  281. }
  282. }
  283. cell.imageIcon?.image = CCGraphics.changeThemingColorImage(UIImage.init(named: item.icon), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon)
  284. cell.labelText?.text = NSLocalizedString(item.name, comment: "")
  285. cell.labelText.textColor = NCBrandColor.sharedInstance.textView
  286. return cell
  287. }
  288. // method to run when table view cell is tapped
  289. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  290. var item: OCExternalSites = OCExternalSites.init()
  291. // Menu Function
  292. if indexPath.section == 0 {
  293. item = functionMenu[indexPath.row]
  294. }
  295. // Menu External Site
  296. if (numberOfSections(in: tableView) == 3 && indexPath.section == 1) {
  297. item = externalSiteMenu[indexPath.row]
  298. }
  299. // Menu Settings
  300. if ((numberOfSections(in: tableView) == 2 && indexPath.section == 1) || (numberOfSections(in: tableView) == 3 && indexPath.section == 2)) {
  301. item = settingsMenu[indexPath.row]
  302. }
  303. // Action
  304. if item.url.contains("segue") && !item.url.contains("//") {
  305. self.navigationController?.performSegue(withIdentifier: item.url, sender: self)
  306. } else if item.url.contains("openStoryboard") && !item.url.contains("//") {
  307. let nameStoryboard = item.url.replacingOccurrences(of: "openStoryboard", with: "")
  308. let storyboard = UIStoryboard(name: nameStoryboard, bundle: nil)
  309. let controller = storyboard.instantiateInitialViewController()! //instantiateViewController(withIdentifier: nameStoryboard)
  310. self.present(controller, animated: true, completion: nil)
  311. } else if item.url.contains("//") {
  312. if (self.splitViewController?.isCollapsed)! {
  313. let webVC = SwiftWebVC(urlString: item.url, hideToolbar: false)
  314. webVC.delegate = self
  315. self.navigationController?.pushViewController(webVC, animated: true)
  316. self.navigationController?.navigationBar.isHidden = false
  317. } else {
  318. let webVC = SwiftModalWebVC(urlString: item.url, colorText: UIColor.white, colorDoneButton: UIColor.black, doneButtonVisible: true)
  319. webVC.delegateWeb = self
  320. self.present(webVC, animated: true, completion: nil)
  321. }
  322. } else if item.url == "logout" {
  323. let alertController = UIAlertController(title: "", message: NSLocalizedString("_want_delete_", comment: ""), preferredStyle: .alert)
  324. let actionYes = UIAlertAction(title: NSLocalizedString("_yes_delete_", comment: ""), style: .default) { (action:UIAlertAction) in
  325. let manageAccount = CCManageAccount()
  326. manageAccount.delete(self.appDelegate.activeAccount)
  327. self.appDelegate.openLoginView(self, delegate: self, loginType: Int(k_login_Add_Forced), selector: Int(k_intro_login))
  328. }
  329. let actionNo = UIAlertAction(title: NSLocalizedString("_no_delete_", comment: ""), style: .default) { (action:UIAlertAction) in
  330. print("You've pressed No button");
  331. }
  332. alertController.addAction(actionYes)
  333. alertController.addAction(actionNo)
  334. self.present(alertController, animated: true, completion:nil)
  335. }
  336. }
  337. @objc func tapLabelQuotaExternalSite() {
  338. if (quotaMenu.count > 0) {
  339. let item = quotaMenu[0]
  340. if (self.splitViewController?.isCollapsed)! {
  341. let webVC = SwiftWebVC(urlString: item.url, hideToolbar: true)
  342. webVC.delegate = self
  343. self.navigationController?.pushViewController(webVC, animated: true)
  344. self.navigationController?.navigationBar.isHidden = false
  345. } else {
  346. let webVC = SwiftModalWebVC(urlString: item.url, colorText: UIColor.white, colorDoneButton: UIColor.black, doneButtonVisible: true, hideToolbar: false)
  347. webVC.delegateWeb = self
  348. self.present(webVC, animated: true, completion: nil)
  349. }
  350. }
  351. }
  352. @objc func tapImageLogoManageAccount() {
  353. let controller = CCManageAccount.init()
  354. self.navigationController?.pushViewController(controller, animated: true)
  355. }
  356. func loginSuccess(_ loginType: NSInteger) {
  357. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "initializeMain"), object: nil, userInfo: nil)
  358. appDelegate.selectedTabBarController(Int(k_tabBarApplicationIndexFile))
  359. }
  360. }
  361. extension CCMore: SwiftModalWebVCDelegate, SwiftWebVCDelegate{
  362. public func didStartLoading() {
  363. //print("Started loading.")
  364. }
  365. public func didReceiveServerRedirectForProvisionalNavigation(url: URL) {
  366. let urlString: String = url.absoluteString.lowercased()
  367. // Protocol close webVC
  368. if (urlString.contains(NCBrandOptions.sharedInstance.webCloseViewProtocolPersonalized) == true) {
  369. if (self.presentingViewController != nil) {
  370. self.dismiss(animated: true, completion: nil)
  371. } else {
  372. self.navigationController?.popToRootViewController(animated: true)
  373. }
  374. }
  375. }
  376. public func didFinishLoading(success: Bool) {
  377. //print("Finished loading. Success: \(success).")
  378. }
  379. public func didFinishLoading(success: Bool, url: URL) {
  380. //print("Finished loading. Success: \(success).")
  381. }
  382. public func webDismiss() {
  383. //print("Web dismiss.")
  384. }
  385. }
  386. class CCCellMore: UITableViewCell {
  387. @IBOutlet weak var labelText: UILabel!
  388. @IBOutlet weak var imageIcon: UIImageView!
  389. }