O2AppViewController.swift 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. //
  2. // O2AppViewController.swift
  3. // O2Platform
  4. //
  5. // Created by 刘振兴 on 16/7/25.
  6. // Copyright © 2016年 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import WebKit
  10. import SwiftyJSON
  11. import Alamofire
  12. import ObjectMapper
  13. import AlamofireObjectMapper
  14. import Flutter
  15. import CocoaLumberjack
  16. class O2AppViewController: UIViewController{
  17. @IBOutlet weak var collectionView: UICollectionView!
  18. private let reuseIdentifier = "myCell"
  19. fileprivate let collectionViewDelegate = ZLCollectionView()
  20. var o2ALLApps:[O2App] = []
  21. var apps2:[[O2App]] = [[], [], []]
  22. override func viewDidLoad() {
  23. super.viewDidLoad()
  24. self.title = L10n.app
  25. navigationItem.rightBarButtonItem = UIBarButtonItem(title: L10n.edit, style: .plain, target: self, action: #selector(_forwardEditSegue))
  26. self.collectionViewDelegate.delegate = self
  27. self.collectionView.dataSource = self.collectionViewDelegate
  28. self.collectionView.delegate = self.collectionViewDelegate
  29. self.o2ALLApps = []
  30. self.apps2 = [[], [], []]
  31. //self.loadAppConfigDb()
  32. }
  33. override func viewWillAppear(_ animated: Bool) {
  34. super.viewWillAppear(animated)
  35. self.loadAppConfigDb()
  36. }
  37. func loadAppConfigDb() {
  38. let allApps = DBManager.shared.queryData()
  39. var nativeApps:[O2App] = []
  40. var portalApps:[O2App] = []
  41. allApps.forEach { (app) in
  42. if app.storyBoard == "webview" {
  43. portalApps.append(app)
  44. } else {
  45. nativeApps.append(app)
  46. }
  47. }
  48. o2ALLApps = allApps
  49. let mainApps = DBManager.shared.queryMainData()
  50. apps2 = [mainApps, nativeApps, portalApps]
  51. self.collectionViewDelegate.apps = apps2
  52. DispatchQueue.main.async {
  53. self.collectionView.reloadData()
  54. }
  55. }
  56. @objc private func _forwardEditSegue() {
  57. // self.performSegue(withIdentifier: "showAppEditSegue", sender: nil)
  58. if self.collectionViewDelegate.isEdit {
  59. self.collectionViewDelegate.isEdit = false
  60. self.navigationItem.rightBarButtonItem?.title = L10n.edit
  61. self._saveUpdate()
  62. self.collectionView.reloadData()
  63. } else {
  64. self.collectionViewDelegate.isEdit = true
  65. self.navigationItem.rightBarButtonItem?.title = L10n.done
  66. self.collectionView.reloadData()
  67. }
  68. }
  69. @objc private func _saveUpdate() {
  70. let mainApps = self.apps2[0]
  71. mainApps.forEachEnumerated { (i, app) in
  72. app.mainOrder = i
  73. DBManager.shared.updateData(app, 1)
  74. }
  75. var noMainApps: [O2App] = []
  76. o2ALLApps.forEach { (app) in
  77. if !mainApps.contains(where: { (a) -> Bool in
  78. return a.appId == app.appId
  79. }) {
  80. noMainApps.append(app)
  81. }
  82. }
  83. noMainApps.forEachEnumerated { (i, app) in
  84. app.order = i
  85. DBManager.shared.updateData(app, 0)
  86. }
  87. self.showMessage(msg: L10n.applicationsUpdateSuccess)
  88. }
  89. override func didReceiveMemoryWarning() {
  90. super.didReceiveMemoryWarning()
  91. }
  92. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  93. if segue.identifier == "showMailSegue" {
  94. segue.destination.modalPresentationStyle = .fullScreen
  95. if let nav = segue.destination as? ZLNavigationController {
  96. nav.viewControllers.forEach { (vc) in
  97. if vc is MailViewController {
  98. DDLogDebug("显示门户。。。。")
  99. (vc as! MailViewController).app = sender as? O2App
  100. }
  101. }
  102. }else if let mail = segue.destination as? MailViewController {
  103. mail.app = sender as? O2App
  104. }
  105. }
  106. }
  107. @IBAction func unBackAppsForApps(_ segue:UIStoryboardSegue){
  108. DDLogDebug("返回应用列表")
  109. }
  110. // MARK: - Flutter
  111. //打开flutter应用
  112. /**
  113. @param routeName flutter的路由 打开不同的页面
  114. **/
  115. func openFlutterApp(routeName: String) {
  116. let flutterViewController = O2FlutterViewController()
  117. DDLogDebug("init route:\(routeName)")
  118. flutterViewController.setInitialRoute(routeName)
  119. flutterViewController.modalPresentationStyle = .fullScreen
  120. self.present(flutterViewController, animated: false, completion: nil)
  121. }
  122. }
  123. extension O2AppViewController:ZLCollectionViewDelegate{
  124. func clickWithApp(_ app: O2App, section: Int) {
  125. if self.collectionViewDelegate.isEdit {
  126. if section == 0 {
  127. var main = self.apps2[0]
  128. main.removeAll { (a) -> Bool in
  129. return a.appId == app.appId
  130. }
  131. self.apps2[0] = main
  132. } else {
  133. if !self.collectionViewDelegate.isAdd2Main(app: app) {
  134. var main = self.apps2[0]
  135. main.append(app)
  136. self.apps2[0] = main
  137. }
  138. }
  139. self.collectionViewDelegate.apps = self.apps2
  140. self.collectionView.reloadData()
  141. } else {
  142. self.openApp(app: app)
  143. }
  144. }
  145. private func openApp(app: O2App) {
  146. if let flutter = app.storyBoard, flutter == "flutter" {
  147. openFlutterApp(routeName: app.appId!)
  148. }else {
  149. //设置返回标志,其它应用根据此返回标志调用返回unwindSegue
  150. AppConfigSettings.shared.appBackType = 2
  151. if let segueIdentifier = app.segueIdentifier,segueIdentifier != "" { // portal 门户 走这边
  152. if app.storyBoard! == "webview" { // 打开MailViewController
  153. DDLogDebug("open webview for 22222: "+app.title!+" url: "+app.vcName!)
  154. self.performSegue(withIdentifier: segueIdentifier, sender: app)
  155. }else {
  156. self.performSegue(withIdentifier: segueIdentifier, sender: nil)
  157. }
  158. } else {
  159. if app.storyBoard! == "webview" {
  160. DDLogError("321 open webview for : "+app.title!+" url: "+app.vcName!)
  161. } else {
  162. // 内置应用走这边 根据appkey 打开对应的storyboard
  163. if app.appId == "o2ai" {
  164. app.storyBoard = "ai"
  165. }
  166. let story = O2AppUtil.apps.first { (appInfo) -> Bool in
  167. return app.appId == appInfo.appId
  168. }
  169. var storyBoardName = app.storyBoard
  170. if story != nil {
  171. storyBoardName = story?.storyBoard
  172. }
  173. DDLogDebug("storyboard: \(storyBoardName!) , app:\(app.appId!)")
  174. ///新版云盘 2019-11-20
  175. if storyBoardName == "cloudStorage" {
  176. storyBoardName = "CloudFile"
  177. }
  178. ////
  179. let storyBoard = UIStoryboard(name: storyBoardName!, bundle: nil)
  180. var destVC:UIViewController!
  181. if let vcname = app.vcName,vcname.isEmpty == false {
  182. destVC = storyBoard.instantiateViewController(withIdentifier: app.vcName!)
  183. }else{
  184. destVC = storyBoard.instantiateInitialViewController()
  185. }
  186. if app.vcName == "todoTask" {
  187. if "taskcompleted" == app.appId {
  188. AppConfigSettings.shared.taskIndex = 2
  189. }else if "read" == app.appId {
  190. AppConfigSettings.shared.taskIndex = 1
  191. }else if "readcompleted" == app.appId {
  192. AppConfigSettings.shared.taskIndex = 3
  193. }else {
  194. AppConfigSettings.shared.taskIndex = 0
  195. }
  196. }
  197. destVC.modalPresentationStyle = .fullScreen
  198. if destVC.isKind(of: ZLNavigationController.self) {
  199. DDLogInfo("cloudFIle 进来了?")
  200. self.show(destVC, sender: nil)
  201. }else{
  202. self.navigationController?.pushViewController(destVC, animated: true)
  203. }
  204. }
  205. }
  206. }
  207. }
  208. }
  209. extension O2AppViewController:AppEditControllerUpdater {
  210. func appEditControllerUpdater() {
  211. self.loadAppConfigDb()
  212. }
  213. }