AppSetupState.swift 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2019-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. import Foundation
  21. @objc public class AppSetupState: NSObject {
  22. private let appSetupNotCompleted = "APP_SETUP_NOT_COMPLETED"
  23. private let myIdentityStore: MyIdentityStore?
  24. @objc init(myIdentityStore: MyIdentityStore?) {
  25. self.myIdentityStore = myIdentityStore
  26. super.init()
  27. self.checkDatabaseFile()
  28. }
  29. @objc override convenience init() {
  30. self.init(myIdentityStore: nil)
  31. }
  32. /**
  33. Check is database not created at first time instanced and identity is provisioned.
  34. - Returns: True means setup process is finished and database and identity is ready
  35. */
  36. @objc public func isAppSetupCompleted() -> Bool {
  37. guard self.myIdentityStore != nil else {
  38. return false;
  39. }
  40. return self.existsDatabaseFile() && self.myIdentityStore?.isProvisioned() ?? false;
  41. }
  42. /**
  43. Call this if setup completed, otherwise setup screen will show again.
  44. */
  45. @objc public func appSetupCompleted() {
  46. FileUtility.delete(fileUrl: FileUtility.appDataDirectory?.appendingPathComponent(appSetupNotCompleted))
  47. }
  48. /**
  49. Check is database not created at first time instanced.
  50. - Returns: True means database already exitsts on first instanced
  51. */
  52. @objc public func existsDatabaseFile() -> Bool {
  53. if UserDefaults.standard.bool(forKey: "FASTLANE_SNAPSHOT") {
  54. // We create DB for screenshots. Return true to skip wizard
  55. return true
  56. }
  57. return !FileUtility.isExists(fileUrl: FileUtility.appDataDirectory?.appendingPathComponent(appSetupNotCompleted))
  58. }
  59. private func checkDatabaseFile() {
  60. if !FileUtility.isExists(fileUrl: DatabaseManager.storeUrl()) && !FileUtility.isExists(fileUrl: FileUtility.appDataDirectory?.appendingPathComponent(appSetupNotCompleted)) {
  61. FileUtility.write(fileUrl: FileUtility.appDataDirectory?.appendingPathComponent(appSetupNotCompleted), contents: nil)
  62. }
  63. }
  64. }