NCLoginPoll.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //
  2. // SwiftUIView.swift
  3. // Nextcloud
  4. //
  5. // Created by Milen on 21.05.24.
  6. // Copyright © 2024 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 NextcloudKit
  24. import SwiftUI
  25. struct NCLoginPoll: View {
  26. let loginFlowV2Token: String
  27. let loginFlowV2Endpoint: String
  28. let loginFlowV2Login: String
  29. var cancelButtonDisabled = false
  30. @ObservedObject private var loginManager = LoginManager()
  31. @Environment(\.dismiss) private var dismiss
  32. var body: some View {
  33. VStack {
  34. Text(NSLocalizedString("_poll_desc_", comment: ""))
  35. .multilineTextAlignment(.center)
  36. .foregroundStyle(.white)
  37. .padding()
  38. ProgressView()
  39. .scaleEffect(1.5)
  40. .tint(.white)
  41. .padding()
  42. HStack {
  43. Button(NSLocalizedString("_cancel_", comment: "")) {
  44. dismiss()
  45. }
  46. .disabled(loginManager.isLoading || cancelButtonDisabled)
  47. .buttonStyle(.bordered)
  48. .tint(.white)
  49. Button(NSLocalizedString("_retry_", comment: "")) {
  50. loginManager.openLoginInBrowser()
  51. }
  52. .buttonStyle(.borderedProminent)
  53. .foregroundStyle(Color(NCBrandColor.shared.customer))
  54. .tint(.white)
  55. }
  56. .padding()
  57. }
  58. .frame(maxWidth: .infinity, maxHeight: .infinity)
  59. .onChange(of: loginManager.pollFinished) { value in
  60. if value {
  61. let window = UIApplication.shared.firstWindow
  62. if window?.rootViewController is NCMainTabBarController {
  63. window?.rootViewController?.dismiss(animated: true, completion: nil)
  64. } else {
  65. if let mainTabBarController = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController() as? NCMainTabBarController {
  66. mainTabBarController.modalPresentationStyle = .fullScreen
  67. mainTabBarController.view.alpha = 0
  68. window?.rootViewController = mainTabBarController
  69. window?.makeKeyAndVisible()
  70. UIView.animate(withDuration: 0.5) {
  71. mainTabBarController.view.alpha = 1
  72. }
  73. }
  74. }
  75. }
  76. }
  77. .background(Color(NCBrandColor.shared.customer))
  78. .onAppear {
  79. loginManager.configure(loginFlowV2Token: loginFlowV2Token, loginFlowV2Endpoint: loginFlowV2Endpoint, loginFlowV2Login: loginFlowV2Login)
  80. if !isRunningForPreviews {
  81. loginManager.openLoginInBrowser()
  82. }
  83. }
  84. .interactiveDismissDisabled()
  85. }
  86. }
  87. #Preview {
  88. NCLoginPoll(loginFlowV2Token: "", loginFlowV2Endpoint: "", loginFlowV2Login: "")
  89. }
  90. private class LoginManager: ObservableObject {
  91. private let appDelegate = (UIApplication.shared.delegate as? AppDelegate)!
  92. var loginFlowV2Token = ""
  93. var loginFlowV2Endpoint = ""
  94. var loginFlowV2Login = ""
  95. @Published var pollFinished = false
  96. @Published var isLoading = false
  97. init() {
  98. NotificationCenter.default.addObserver(self, selector: #selector(applicationDidBecomeActive(_:)), name: UIApplication.didBecomeActiveNotification, object: nil)
  99. }
  100. @objc func applicationDidBecomeActive(_ notification: NSNotification) {
  101. poll()
  102. }
  103. func configure(loginFlowV2Token: String, loginFlowV2Endpoint: String, loginFlowV2Login: String) {
  104. self.loginFlowV2Token = loginFlowV2Token
  105. self.loginFlowV2Endpoint = loginFlowV2Endpoint
  106. self.loginFlowV2Login = loginFlowV2Login
  107. }
  108. func poll() {
  109. NextcloudKit.shared.getLoginFlowV2Poll(token: self.loginFlowV2Token, endpoint: self.loginFlowV2Endpoint) { server, loginName, appPassword, _, error in
  110. if error == .success, let urlBase = server, let user = loginName, let appPassword {
  111. self.isLoading = true
  112. self.appDelegate.createAccount(urlBase: urlBase, user: user, password: appPassword) { error in
  113. if error == .success {
  114. self.pollFinished = true
  115. }
  116. }
  117. }
  118. }
  119. }
  120. func openLoginInBrowser() {
  121. UIApplication.shared.open(URL(string: loginFlowV2Login)!)
  122. }
  123. }