SplashScreenViewModel.swift 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. //
  2. // SplashScreenViewModel.swift
  3. // Learn Genie
  4. //
  5. // Created by Suraj Kumar Mandal on 01/09/21.
  6. //
  7. import Foundation
  8. import Alamofire
  9. import SSZipArchive
  10. import RealmSwift
  11. class SplashScreenViewModel {
  12. var delegate : SplashScreenViewProtocol?
  13. func generateCookie() {
  14. if let delegate = delegate {
  15. let url = ApiUrl.BASE_URL + ApiUrl.API_AUTH
  16. let params: [String:Any]?
  17. params = [
  18. "username" : "admin",
  19. "password" : "mahindrapass123"
  20. ]
  21. print(params!)
  22. AF.request(url, method: HTTPMethod.post, parameters: params).responseData { (responseObject) -> Void in
  23. if let responseStatus = responseObject.response?.statusCode {
  24. if responseStatus != 200 {
  25. // error
  26. print("error...")
  27. } else {
  28. // view all cookies
  29. print(HTTPCookieStorage.shared.cookies!)
  30. for cookie in HTTPCookieStorage.shared.cookies! {
  31. print(cookie.value)
  32. UserDefaultsConstant.setValueInUserDefaults(objValue: cookie.value, for: "cookieValue")
  33. //self.getRole()
  34. //delegate.groupLeave()
  35. delegate.callDataAPI()
  36. }
  37. }
  38. }
  39. }
  40. }
  41. }
  42. func getRole() {
  43. if let delegate = delegate {
  44. DBManager.sharedInstance.deleteAllDatabase()
  45. let url = "\(ApiUrl.BASE_URL + ApiUrl.API_ROLE).json"
  46. let cookieValue = UserDefaultsConstant.getValueFromUserDefults(for: "cookieValue") ?? ""
  47. let headers: HTTPHeaders = [
  48. "Content-Type": "application/json",
  49. "Accept": "application/json",
  50. "Cookie": "\(Constant.CookieName) = \(cookieValue)"
  51. ]
  52. print(headers)
  53. AF.request(url, method: HTTPMethod.post, encoding: JSONEncoding.default, headers: headers).responseJSON { response in
  54. switch response.result {
  55. case .success(let value):
  56. let roleData = value as! [NSDictionary]
  57. for item in roleData {
  58. let id = item.value(forKey: "id") as! NSNumber;
  59. let name = item.value(forKey: "name") as? String ?? "";
  60. let authority = item.value(forKey: "authority") as? String ?? "";
  61. var roleModel = [RoleModel]()
  62. let newRoleModel = RoleModel(id: Int(truncating: id), name: name, authority: authority)
  63. roleModel.append(newRoleModel)
  64. DBManager.sharedInstance.addData(objs: roleModel)
  65. }
  66. let realmEntries = DBManager.sharedInstance.database.objects(RoleModel.self)
  67. print("role count")
  68. print(realmEntries.count)
  69. print(roleData.count)
  70. // if both counts doesn't match
  71. let db_count = realmEntries.count - roleData.count
  72. if db_count > 0 {
  73. let array = Array(1...db_count)
  74. for value in array {
  75. var roleModel = [RoleModel]()
  76. let data = DBManager.sharedInstance.getObjects(type: RoleModel.self)? [realmEntries.count - value] as! RoleModel
  77. roleModel.append(data)
  78. DBManager.sharedInstance.deleteFromDb(object: roleModel)
  79. }
  80. }
  81. //self.getLanguage()
  82. delegate.groupLeave()
  83. case .failure(let error):
  84. print(error)
  85. }
  86. }
  87. }
  88. }
  89. func getLanguage() {
  90. if let delegate = delegate {
  91. let url = "\(ApiUrl.BASE_URL + ApiUrl.API_LANGUAGE).json"
  92. let cookieValue = UserDefaultsConstant.getValueFromUserDefults(for: "cookieValue") ?? ""
  93. let headers: HTTPHeaders = [
  94. "Content-Type": "application/json",
  95. "Accept": "application/json",
  96. "Cookie": "\(Constant.CookieName) = \(cookieValue)"
  97. ]
  98. print(headers)
  99. AF.request(url, method: HTTPMethod.post, encoding: JSONEncoding.default, headers: headers).responseJSON { response in
  100. switch response.result {
  101. case .success(let value):
  102. let languageArray = value as! [NSDictionary]
  103. for item in languageArray {
  104. let id = item.value(forKey: "id") as! NSNumber;
  105. let shortName = item.value(forKey: "shortName") as? String ?? "" ;
  106. let dateCreated = item.value(forKey: "dateCreated") as? String ?? "" ;
  107. let lastUpdated = item.value(forKey: "lastUpdated") as? String ?? "" ;
  108. let name = item.value(forKey: "name") as? String ?? "" ;
  109. var languageModel = [LanguageModel]()
  110. let newLanguageModel = LanguageModel(id: Int(truncating: id), shortName: shortName, dateCreated: dateCreated, lastUpdated: lastUpdated, name: name)
  111. languageModel.append(newLanguageModel)
  112. DBManager.sharedInstance.addData(objs: languageModel)
  113. }
  114. let realmEntries = DBManager.sharedInstance.database.objects(LanguageModel.self)
  115. print("language count")
  116. print(realmEntries.count)
  117. print(languageArray.count)
  118. // if both counts doesn't match
  119. let db_count = realmEntries.count - languageArray.count
  120. if db_count > 0 {
  121. let array = Array(1...db_count)
  122. for value in array {
  123. var languageModel = [LanguageModel]()
  124. let data = DBManager.sharedInstance.getObjects(type: LanguageModel.self)? [realmEntries.count - value] as! LanguageModel
  125. languageModel.append(data)
  126. DBManager.sharedInstance.deleteFromDb(object: languageModel)
  127. }
  128. }
  129. //self.getTranslation(offset: 0)
  130. delegate.groupLeave()
  131. case .failure(let error):
  132. print(error)
  133. }
  134. }
  135. }
  136. }
  137. func getTranslation(offset:Int) {
  138. if let delegate = delegate {
  139. let max = 10
  140. let url = "\(ApiUrl.BASE_URL + ApiUrl.API_TRANSLATION).json?max=\(max)&offset=\(offset)"
  141. let cookieValue = UserDefaultsConstant.getValueFromUserDefults(for: "cookieValue") ?? ""
  142. let headers: HTTPHeaders = [
  143. "Content-Type": "application/json",
  144. "Accept": "application/json",
  145. "Cookie": "\(Constant.CookieName) = \(cookieValue)"
  146. ]
  147. print(headers)
  148. //let params: [String:Any]?
  149. let params = [
  150. "max" : max,
  151. "offset" : offset
  152. ]
  153. print(params)
  154. AF.request(url, method: .get, encoding: URLEncoding.httpBody, headers: headers).responseJSON { response in
  155. switch response.result {
  156. case .success(let value):
  157. let translationArray = value as! [NSDictionary]
  158. if translationArray != [] {
  159. print(translationArray)
  160. for item in translationArray {
  161. let id = item.value(forKey: "id") as! NSNumber;
  162. let originalText = item.value(forKey: "originalText") as? String ?? "" ;
  163. let language = item.value(forKey: "language") as? NSObject;
  164. let languageId = language?.value(forKey: "id") as! NSNumber;
  165. let translatedText = item.value(forKey: "translatedText") as? String ?? "" ;
  166. var translationModel = [TranslationModel]()
  167. let newtranslationModel = TranslationModel(id: Int(truncating: id), originalText: originalText, languageId: Int(truncating: languageId), translatedText: translatedText)
  168. translationModel.append(newtranslationModel)
  169. DBManager.sharedInstance.addData(objs: translationModel)
  170. }
  171. let realmEntries = DBManager.sharedInstance.database.objects(TranslationModel.self)
  172. print("translation count")
  173. print(realmEntries.count)
  174. self.getTranslation(offset: offset + max)
  175. } else {
  176. //Next API call
  177. //self.getState(offset: 0)
  178. delegate.groupLeave()
  179. }
  180. case .failure(let error):
  181. print(error)
  182. }
  183. }
  184. }
  185. }
  186. func getState(offset:Int) {
  187. if let delegate = delegate {
  188. let max = 10
  189. let url = "\(ApiUrl.BASE_URL + ApiUrl.API_STATE).json?max=\(max)&offset=\(offset)"
  190. let cookieValue = UserDefaultsConstant.getValueFromUserDefults(for: "cookieValue") ?? ""
  191. let headers: HTTPHeaders = [
  192. "Content-Type": "application/json",
  193. "Accept": "application/json",
  194. "Cookie": "\(Constant.CookieName) = \(cookieValue)"
  195. ]
  196. print(headers)
  197. print(url)
  198. AF.request(url, method: .get, encoding: URLEncoding.httpBody, headers: headers).responseJSON { response in
  199. switch response.result {
  200. case .success(let value):
  201. let translationArray = value as! [NSDictionary]
  202. if translationArray != [] {
  203. print(translationArray)
  204. for item in translationArray {
  205. let id = item.value(forKey: "id") as! NSNumber;
  206. let stateName = item.value(forKey: "stateName") as? String ?? "" ;
  207. let stateNameHindi = item.value(forKey: "stateNameHindi") as? String ?? "" ;
  208. var stateModel = [StateModel]()
  209. let newStateModel = StateModel(id: Int(truncating: id), stateName: stateName, stateNameHindi: stateNameHindi)
  210. stateModel.append(newStateModel)
  211. DBManager.sharedInstance.addData(objs: stateModel)
  212. }
  213. let realmEntries = DBManager.sharedInstance.database.objects(StateModel.self)
  214. print("state count")
  215. print(realmEntries.count)
  216. self.getState(offset: offset + max)
  217. } else {
  218. //Next API call
  219. //self.getDistrict(offset: 0)
  220. delegate.groupLeave()
  221. }
  222. case .failure(let error):
  223. print(error)
  224. }
  225. }
  226. }
  227. }
  228. func getDistrict(offset:Int) {
  229. if let delegate = delegate {
  230. let max = 10
  231. let url = "\(ApiUrl.BASE_URL + ApiUrl.API_DISTRICT).json?max=\(max)&offset=\(offset)"
  232. let cookieValue = UserDefaultsConstant.getValueFromUserDefults(for: "cookieValue") ?? ""
  233. let headers: HTTPHeaders = [
  234. "Content-Type": "application/json",
  235. "Accept": "application/json",
  236. "Cookie": "\(Constant.CookieName) = \(cookieValue)"
  237. ]
  238. print(headers)
  239. print(url)
  240. AF.request(url, method: .get, encoding: URLEncoding.httpBody, headers: headers).responseJSON { response in
  241. switch response.result {
  242. case .success(let value):
  243. let translationArray = value as! [NSDictionary]
  244. if translationArray != [] {
  245. print(translationArray)
  246. for item in translationArray {
  247. let id = item.value(forKey: "id") as! NSNumber;
  248. let districtName = item.value(forKey: "districtName") as? String ?? "" ;
  249. let state = item.value(forKey: "state") as? NSObject;
  250. let stateId = state?.value(forKey: "id") as! NSNumber;
  251. let districtNameHindi = item.value(forKey: "districtNameHindi") as? String ?? "" ;
  252. var districtModel = [DistrictModel]()
  253. let newDistrictModel = DistrictModel(id: Int(truncating: id), districtName: districtName, stateId: Int(truncating: stateId), districtNameHindi: districtNameHindi)
  254. districtModel.append(newDistrictModel)
  255. DBManager.sharedInstance.addData(objs: districtModel)
  256. }
  257. let realmEntries = DBManager.sharedInstance.database.objects(DistrictModel.self)
  258. print("district count")
  259. print(realmEntries.count)
  260. self.getDistrict(offset: offset + max)
  261. } else {
  262. //Next API call
  263. //self.getTopic(offset: 0)
  264. delegate.groupLeave()
  265. }
  266. case .failure(let error):
  267. print(error)
  268. }
  269. }
  270. }
  271. }
  272. func getTopic(offset:Int) {
  273. if let delegate = delegate {
  274. let max = 10
  275. let url = "\(ApiUrl.BASE_URL + ApiUrl.API_TOPIC).json?max=\(max)&offset=\(offset)"
  276. let cookieValue = UserDefaultsConstant.getValueFromUserDefults(for: "cookieValue") ?? ""
  277. let headers: HTTPHeaders = [
  278. "Content-Type": "application/json",
  279. "Accept": "application/json",
  280. "Cookie": "\(Constant.CookieName) = \(cookieValue)"
  281. ]
  282. print(headers)
  283. print(url)
  284. AF.request(url, method: .get, encoding: URLEncoding.httpBody, headers: headers).responseJSON { response in
  285. switch response.result {
  286. case .success(let value):
  287. let topicArray = value as! [NSDictionary]
  288. if topicArray != [] {
  289. print(topicArray)
  290. for item in topicArray {
  291. let id = item.value(forKey: "id") as! NSNumber;
  292. let level = item.value(forKey: "level") as? NSObject;
  293. let levelId = level?.value(forKey: "id") as! NSNumber;
  294. let noOfQuizQuestions = item.value(forKey: "noOfQuizQuestions") as! NSNumber;
  295. let video = item.value(forKey: "video") as? String ?? "" ;
  296. let dateCreated = item.value(forKey: "dateCreated") as? String ?? "" ;
  297. let lastUpdated = item.value(forKey: "lastUpdated") as? String ?? "" ;
  298. let name = item.value(forKey: "name") as? String ?? "" ;
  299. let questionBank = item.value(forKey: "questionBank") as? NSObject;
  300. let questionBankId = questionBank?.value(forKey: "id") as! NSNumber;
  301. let content = item.value(forKey: "content") as? String ?? "" ;
  302. let language = item.value(forKey: "language") as? NSObject;
  303. let languageId = language?.value(forKey: "id") as! NSNumber;
  304. var topicModel = [TopicModel]()
  305. let newtopicModel = TopicModel(id: Int(truncating: id), levelId: Int(truncating: levelId), noOfQuizQuestions: Int(truncating: noOfQuizQuestions), video: video, dateCreated: dateCreated, lastUpdated: lastUpdated, name: name, questionBankId: Int(truncating: questionBankId), content: content, languageId: Int(truncating: languageId))
  306. topicModel.append(newtopicModel)
  307. DBManager.sharedInstance.addData(objs: topicModel)
  308. //self.topicDownloader(id: Int(truncating: id))
  309. }
  310. let realmEntries = DBManager.sharedInstance.database.objects(TopicModel.self)
  311. print("topic count")
  312. print(realmEntries.count)
  313. self.getTopic(offset: offset + max)
  314. } else {
  315. //Next API call
  316. // delegate.stopActivityIndicator()
  317. // delegate.navigation()
  318. // print("All api call done!")
  319. delegate.groupLeave()
  320. }
  321. case .failure(let error):
  322. print(error)
  323. }
  324. }
  325. }
  326. }
  327. // func getUserDetails() {
  328. // switch UserDefaultsConstant.getValueFromUserDefults(for: Constant.signInMethod) as! String {
  329. // case "google":
  330. // let url = "\(ApiUrl.BASE_URL + ApiUrl.API_SEARCH_STUDENT)?email=\(emailId)"
  331. // print(url)
  332. // let cookieValue = UserDefaultsConstant.getValueFromUserDefults(for: "cookieValue") ?? ""
  333. // let headers: HTTPHeaders = [
  334. // "Content-Type": "application/json",
  335. // "Accept": "application/json",
  336. // "Cookie": "\(Constant.CookieName) = \(cookieValue)"
  337. // ]
  338. // print(headers)
  339. //
  340. // AF.request(url, method: .get, encoding: URLEncoding.httpBody, headers: headers).responseJSON { [self] response in
  341. // switch response.result {
  342. // case .success(let value):
  343. // let data = value as! NSDictionary
  344. // print(data)
  345. // let id = data.value(forKey: "id") as? Int ?? 0;
  346. // if id != 0 {
  347. // let userData = value as! NSDictionary
  348. //
  349. // let accountExpired = userData.value(forKey: "accountExpired") as! NSNumber;
  350. // let accountLocked = userData.value(forKey: "accountLocked") as! NSNumber;
  351. // let dateCreated = userData.value(forKey: "dateCreated") as? String ?? "";
  352. // let dateOfBirth = userData.value(forKey: "dateOfBirth") as? String ?? "";
  353. // let district = userData.value(forKey: "district") as? NSObject;
  354. // let districtId = district?.value(forKey: "id") as! NSNumber;
  355. // let email = userData.value(forKey: "email") as? String ?? "";
  356. // let enabled = userData.value(forKey: "enabled") as! NSNumber;
  357. // let gender = userData.value(forKey: "gender") as? String ?? "";
  358. // let id = userData.value(forKey: "id") as! NSNumber;
  359. // let level = userData.value(forKey: "level") as? NSObject;
  360. // let levelId = level?.value(forKey: "id") as! NSNumber;
  361. // let mobile = userData.value(forKey: "mobile") as? String ?? "";
  362. // let name = userData.value(forKey: "name") as? String ?? "";
  363. // let password = userData.value(forKey: "password") as? String ?? "";
  364. // let passwordExpired = userData.value(forKey: "passwordExpired") as! NSNumber;
  365. // let role = userData.value(forKey: "role") as? NSObject;
  366. // let roleId = role?.value(forKey: "id") as! NSNumber;
  367. // let state = userData.value(forKey: "state") as? NSObject;
  368. // let stateId = state?.value(forKey: "id") as! NSNumber;
  369. // let topic = userData.value(forKey: "topic") as? NSObject;
  370. // let topicId = topic?.value(forKey: "id") as! NSNumber;
  371. // let username = userData.value(forKey: "username") as? String ?? "";
  372. //
  373. // var userModel = [UserDetailsModel]()
  374. // let newUserModel = UserDetailsModel(accountExpired: Int(truncating: accountExpired), accountLocked: Int(truncating: accountLocked), dateCreated: dateCreated, dateOfBirth: dateOfBirth, districtId: Int(truncating: districtId), email: email, enabled: Int(truncating: enabled), gender: gender, id: Int(truncating: id), levelId: Int(truncating: levelId), mobile: mobile, name: name, password: password, passwordExpired: Int(truncating: passwordExpired), roleId: Int(truncating: roleId), stateId: Int(truncating: stateId), topicId: Int(truncating: topicId), username: username, appleIdentifier: UserDefaults.standard.string(forKey: Constant.userIdentifier) ?? "")
  375. // userModel.append(newUserModel)
  376. // DBManager.sharedInstance.addData(objs: userModel)
  377. // let userModelDB = DBManager.sharedInstance.database.objects(UserDetailsModel.self)
  378. // print("user count")
  379. // print(userModelDB.count)
  380. //
  381. // UserDefaultsConstant.setIntValueInUserDefaults(objValue: Int(truncating: id), for: Constant.studentId)
  382. // delegate?.groupLeave()
  383. // }
  384. // case .failure(let error):
  385. // print(error)
  386. // }
  387. // }
  388. // case "apple":
  389. // let userIdentifier = UserDefaultsConstant.getValueFromUserDefults(for: Constant.userIdentifier) ?? ""
  390. // let url = "\(ApiUrl.BASE_URL + ApiUrl.API_SEARCH_APPLE_USERIDENTIFIER)?appleUserIdentifier=\(userIdentifier)"
  391. // print(url)
  392. // let cookieValue = UserDefaultsConstant.getValueFromUserDefults(for: "cookieValue") ?? ""
  393. // let headers: HTTPHeaders = [
  394. // "Content-Type": "application/json",
  395. // "Accept": "application/json",
  396. // "Cookie": "\(Constant.CookieName) = \(cookieValue)"
  397. // ]
  398. // print(headers)
  399. //
  400. // AF.request(url, method: .get, encoding: URLEncoding.httpBody, headers: headers).responseJSON { [self] response in
  401. // switch response.result {
  402. // case .success(let value):
  403. // let data = value as! NSDictionary
  404. // print(data)
  405. // let id = data.value(forKey: "id") as? Int ?? 0;
  406. // if id != 0 {
  407. // let userData = value as! NSDictionary
  408. //
  409. // let accountExpired = userData.value(forKey: "accountExpired") as! NSNumber;
  410. // let accountLocked = userData.value(forKey: "accountLocked") as! NSNumber;
  411. // let dateCreated = userData.value(forKey: "dateCreated") as? String ?? "";
  412. // let dateOfBirth = userData.value(forKey: "dateOfBirth") as? String ?? "";
  413. // let district = userData.value(forKey: "district") as? NSObject;
  414. // let districtId = district?.value(forKey: "id") as! NSNumber;
  415. // let email = userData.value(forKey: "email") as? String ?? "";
  416. // let enabled = userData.value(forKey: "enabled") as! NSNumber;
  417. // let gender = userData.value(forKey: "gender") as? String ?? "";
  418. // let id = userData.value(forKey: "id") as! NSNumber;
  419. // let level = userData.value(forKey: "level") as? NSObject;
  420. // let levelId = level?.value(forKey: "id") as! NSNumber;
  421. // let mobile = userData.value(forKey: "mobile") as? String ?? "";
  422. // let name = userData.value(forKey: "name") as? String ?? "";
  423. // let password = userData.value(forKey: "password") as? String ?? "";
  424. // let passwordExpired = userData.value(forKey: "passwordExpired") as! NSNumber;
  425. // let role = userData.value(forKey: "role") as? NSObject;
  426. // let roleId = role?.value(forKey: "id") as! NSNumber;
  427. // let state = userData.value(forKey: "state") as? NSObject;
  428. // let stateId = state?.value(forKey: "id") as! NSNumber;
  429. // let topic = userData.value(forKey: "topic") as? NSObject;
  430. // let topicId = topic?.value(forKey: "id") as! NSNumber;
  431. // let username = userData.value(forKey: "username") as? String ?? "";
  432. // let appleUserIdentifier = userData.value(forKey: "appleUserIdentifier") as? String ?? "";
  433. //
  434. // var userModel = [UserDetailsModel]()
  435. // let newUserModel = UserDetailsModel(accountExpired: Int(truncating: accountExpired), accountLocked: Int(truncating: accountLocked), dateCreated: dateCreated, dateOfBirth: dateOfBirth, districtId: Int(truncating: districtId), email: email, enabled: Int(truncating: enabled), gender: gender, id: Int(truncating: id), levelId: Int(truncating: levelId), mobile: mobile, name: name, password: password, passwordExpired: Int(truncating: passwordExpired), roleId: Int(truncating: roleId), stateId: Int(truncating: stateId), topicId: Int(truncating: topicId), username: username, appleIdentifier: appleUserIdentifier)
  436. // userModel.append(newUserModel)
  437. // DBManager.sharedInstance.addData(objs: userModel)
  438. // let userModelDB = DBManager.sharedInstance.database.objects(UserDetailsModel.self)
  439. // print("user count")
  440. // print(userModelDB.count)
  441. //
  442. // UserDefaultsConstant.setIntValueInUserDefaults(objValue: Int(truncating: id), for: Constant.studentId)
  443. // //Navigate to Home
  444. // DispatchQueue.main.async {
  445. // let loaderVC = self.storyboard?.instantiateViewController(withIdentifier:"LoadingAnimationViewController" ) as! LoadingAnimationViewController
  446. // self.navigationController?.pushViewController(loaderVC, animated: true)
  447. // }
  448. // }
  449. // case .failure(let error):
  450. // print(error)
  451. // }
  452. // }
  453. // default:
  454. // <#code#>
  455. // }
  456. // }
  457. }
  458. protocol SplashScreenViewProtocol {
  459. func callDataAPI()
  460. func groupLeave()
  461. func stopActivityIndicator()
  462. func showToastMessage(message:String)
  463. func navigation()
  464. }