You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

298 lines
9.4 KiB

6 years ago
  1. //
  2. // SplashScreenViewController.swift
  3. // GMERemittance
  4. //
  5. // Created by gme_2 on 10/09/2018.
  6. //Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import UIKit
  9. import Localize_Swift
  10. import DTTJailbreakDetection
  11. class SplashScreenViewController: UIViewController {
  12. private struct StringConstants {
  13. let descriptionFirst = "hassle_free_transfer_text".localized()
  14. let descriptionSecond = "connect_local_community_text".localized()
  15. let descriptionThird = "earn_reward_point_text".localized()
  16. let loginText = "login_text".localized()
  17. let newUserText = "new_user_text".localized()
  18. }
  19. // MARK: IBOutlets
  20. @IBOutlet private weak var imageViewDotCenter: UIImageView!
  21. @IBOutlet private weak var imageViewDotLeft: UIImageView!
  22. @IBOutlet private weak var imageViewDotRight: UIImageView!
  23. @IBOutlet private weak var imageViewHomeLogo: UIImageView!
  24. @IBOutlet private weak var labelHomeDescription: UILabel!
  25. @IBOutlet private weak var buttonLogin: UIButton!
  26. @IBOutlet private weak var buttonSignUp: UIButton!
  27. @IBOutlet private weak var languageBackGroundView: UIView!
  28. @IBOutlet private weak var languageLabel: UILabel!
  29. private var languageTapGuesture: UITapGestureRecognizer?
  30. // MARK: Properties
  31. private var timer: Timer?
  32. private var counter: Int = 0
  33. private let imageDotSelected = UIImage(named: "dotSelected")
  34. private let imageDotUnselected = UIImage(named: "dotUnselected")
  35. private let imageFirst = UIImage(named: "ic_splashFirstScreen")
  36. private let imageSecond = UIImage(named: "ic_splashSecondScreen")
  37. private let imageThird = UIImage(named: "ic_splashThirdScreen")
  38. private var languageIndex = 0
  39. private var languages: [SendMoneyCountryViewModel] = []
  40. private var selectedLanguage: SendMoneyCountryViewModel? {
  41. didSet {
  42. let code = Language.init(rawValue: selectedLanguage?.code ?? "en")?.code ?? "en"
  43. Localize.setCurrentLanguage(code)
  44. UserDefaults.standard.set(code, forKey: AppConstants.firstTimeLanguageIsSet)
  45. setText()
  46. }
  47. }
  48. var presenter: SplashScreenModuleInterface?
  49. // MARK: VC's Life cycle
  50. override func viewDidLoad() {
  51. super.viewDidLoad()
  52. checkJailBreak()
  53. self.setup()
  54. }
  55. override func viewWillAppear(_ animated: Bool) {
  56. super.viewWillAppear(animated)
  57. self.navigationItem.title = ""
  58. self.hideNavBar()
  59. self.setupTimer()
  60. }
  61. override func viewWillDisappear(_ animated: Bool) {
  62. super.viewWillDisappear(animated)
  63. self.navigationItem.title = ""
  64. self.timer?.invalidate()
  65. }
  66. // MARK: IBActions
  67. private func checkJailBreak() {
  68. if (DTTJailbreakDetection.isJailbroken()) {
  69. self.alert(message: "Your device is jail broken.", title: "Alert!") {
  70. exit(0)
  71. }
  72. }
  73. }
  74. @IBAction private func login(_ sender: UIButton) {
  75. self.presenter?.login()
  76. }
  77. @IBAction private func register(_ sender: UIButton) {
  78. self.presenter?.register()
  79. }
  80. // MARK: Other Functions
  81. private func hideNavBar() {
  82. self.navigationController?.isNavigationBarHidden = true
  83. self.navigationController?.navigationBar.barTintColor = AppConstants.themeRedColor
  84. self.navigationController?.navigationBar.isTranslucent = false
  85. self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
  86. self.navigationController?.navigationBar.shadowImage = UIImage()
  87. }
  88. private func setup() {
  89. // all setup should be done here
  90. hideNavBar()
  91. setUpButtons()
  92. imageViewDotLeft.image = UIImage(named: "dotSelected")
  93. let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(_SplashScreenViewController.handleSwipes(_:)))
  94. let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(_SplashScreenViewController.handleSwipes(_:)))
  95. leftSwipe.direction = .left
  96. rightSwipe.direction = .right
  97. view.addGestureRecognizer(leftSwipe)
  98. view.addGestureRecognizer(rightSwipe)
  99. setLanguageBackgroundView()
  100. }
  101. private func setLanguageBackgroundView() {
  102. let languageSelectionGuesture = UITapGestureRecognizer(target: self, action: #selector(self.showCountryPickerview))
  103. self.languageBackGroundView.addGestureRecognizer(languageSelectionGuesture)
  104. self.languageBackGroundView.layer.cornerRadius = 10
  105. self.languageBackGroundView.clipsToBounds = true
  106. self.languageBackGroundView.layer.addShadow(offset: CGSize.init(width: 2, height: 2))
  107. NotificationCenter.default.addObserver(self, selector: #selector(setText), name: NSNotification.Name(LCLLanguageChangeNotification), object: nil)
  108. setupLanguages()
  109. configureLanguage()
  110. // if exist a selected language then don't animate, and put in the selected language into languageLabel
  111. if let language = UserDefaults.standard.string(forKey: AppConstants.firstTimeLanguageIsSet){
  112. self.selectedLanguage = self.languages.filter{$0.code?.languageCode == language.languageCode}.first
  113. } else {
  114. configureTimer()
  115. }
  116. }
  117. @objc private func setText() {
  118. timer?.invalidate()
  119. self.buttonLogin.setTitle("login_text".localized(), for: .normal)
  120. self.buttonSignUp.setTitle("new_user_text".localized(), for: .normal)
  121. self.languageLabel.text = self.selectedLanguage?.title
  122. setImageandLabels()
  123. }
  124. private func setupLanguages() {
  125. self.languages = Utility.getLanguages()
  126. }
  127. private func configureTimer() {
  128. let timer = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(self.updateLanguage), userInfo: nil, repeats: true)
  129. self.timer = timer
  130. }
  131. private func configureLanguage() {
  132. self.buttonLogin.setTitle(StringConstants().loginText, for: .normal)
  133. self.buttonSignUp.setTitle(StringConstants().newUserText, for: .normal)
  134. }
  135. @objc private func handleSwipes(_ sender: UISwipeGestureRecognizer) {
  136. if (sender.direction == .right) {
  137. counter = counter - 1
  138. if counter < 0 {
  139. counter = 2
  140. }
  141. }
  142. if (sender.direction == .left) {
  143. counter = counter + 1
  144. if counter > 2 {
  145. counter = 0
  146. }
  147. }
  148. setImageandLabels()
  149. }
  150. @objc private func updateImage() {
  151. counter += 1
  152. if counter > 2 {
  153. counter = 0
  154. }
  155. setImageandLabels()
  156. }
  157. private func setImageandLabels() {
  158. imageViewDotLeft.image = imageDotUnselected
  159. imageViewDotCenter.image = imageDotUnselected
  160. imageViewDotRight.image = imageDotUnselected
  161. switch counter {
  162. case 0:
  163. imageViewDotLeft.image = imageDotSelected
  164. imageViewHomeLogo.image = imageFirst
  165. labelHomeDescription.text = StringConstants().descriptionFirst
  166. case 1:
  167. imageViewDotCenter.image = imageDotSelected
  168. imageViewHomeLogo.image = imageSecond
  169. labelHomeDescription.text = StringConstants().descriptionSecond
  170. case 2:
  171. imageViewDotRight.image = imageDotSelected
  172. imageViewHomeLogo.image = imageThird
  173. labelHomeDescription.text = StringConstants().descriptionThird
  174. default:
  175. return
  176. }
  177. }
  178. private func setUpButtons() {
  179. buttonSignUp.layer.cornerRadius = 25
  180. buttonLogin.backgroundColor = .clear
  181. buttonLogin.layer.cornerRadius = 25
  182. buttonLogin.layer.borderWidth = 1
  183. buttonLogin.layer.borderColor = UIColor.white.cgColor
  184. buttonSignUp.layer.borderWidth = 1
  185. buttonSignUp.layer.borderColor = UIColor.white.cgColor
  186. self.view.backgroundColor = AppConstants.themeRedColor
  187. self.buttonSignUp.backgroundColor = AppConstants.themeDarkRedColor
  188. self.buttonLogin.backgroundColor = AppConstants.themeBlueColor
  189. // self.buttonSignUp.backgroundColor = AppConstants.themeBlueColor
  190. }
  191. private func setupTimer() {
  192. self.timer?.fire()
  193. }
  194. @objc private func updateLanguage() {
  195. self.changeText(text: self.languages.elementAt(index: self.languageIndex)?.name ?? "")
  196. if (self.languageIndex + 1) >= (self.languages.count) {
  197. self.languageIndex = 0
  198. }else {
  199. self.languageIndex += 1
  200. }
  201. }
  202. private func changeText(text: String) {
  203. let animation: CATransition = CATransition()
  204. animation.timingFunction = CAMediaTimingFunction(name:
  205. kCAMediaTimingFunctionEaseInEaseOut)
  206. animation.type = kCATransitionPush
  207. animation.subtype = kCATransitionFromTop
  208. animation.duration = 0.8
  209. let animation1: CATransition = CATransition()
  210. animation1.timingFunction = CAMediaTimingFunction(name:
  211. kCAMediaTimingFunctionEaseInEaseOut)
  212. animation1.type = kCATransitionFade
  213. animation1.subtype = kCATransitionReveal
  214. animation1.duration = 0.8
  215. languageLabel.layer.add(animation, forKey: kCATransitionPush)
  216. self.languageLabel.text = text
  217. }
  218. @objc private func showCountryPickerview() {
  219. setupLanguages()
  220. let viewcontroller = UIStoryboard.init(name: "CountryPicker", bundle: nil).instantiateViewController(withIdentifier: "CountryPickerViewController") as! CountryPickerViewController
  221. let data: [SendMoneyCountryViewModel] = self.languages
  222. viewcontroller.isLanguageSelection = true
  223. viewcontroller.data = data
  224. viewcontroller.doneAction = self.countrySelected
  225. viewcontroller.type = PickerTitle.country
  226. self.present(viewcontroller, animated: true, completion: nil)
  227. }
  228. private func countrySelected(models: [SendMoneyCountryViewModel]) {
  229. self.selectedLanguage = models.first
  230. }
  231. override func didReceiveMemoryWarning() {
  232. super.didReceiveMemoryWarning()
  233. }
  234. }
  235. // MARK: SplashScreenViewInterface
  236. extension SplashScreenViewController: SplashScreenViewInterface {
  237. }