CCMore.swift 18 KB

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