From acfee7a357965e3224ee57dbc6cbd87b2e2a1100 Mon Sep 17 00:00:00 2001 From: eevee Date: Fri, 26 Apr 2024 00:51:32 +0300 Subject: [PATCH] native popups --- .../EeveeSpotify/Helpers/PopUpHelper.swift | 39 +++++++++++++++++++ .../EeveeSpotify/Helpers/WindowHelper.swift | 29 -------------- .../Models/SPTEncorePopUpDialog.swift | 7 ++++ .../Models/SPTEncorePopUpDialogModel.swift | 11 ++++++ .../Models/SPTEncorePopUpPresenter.swift | 7 ++++ Sources/EeveeSpotify/Tweak.x.swift | 4 +- 6 files changed, 66 insertions(+), 31 deletions(-) create mode 100644 Sources/EeveeSpotify/Helpers/PopUpHelper.swift delete mode 100644 Sources/EeveeSpotify/Helpers/WindowHelper.swift create mode 100644 Sources/EeveeSpotify/Models/SPTEncorePopUpDialog.swift create mode 100644 Sources/EeveeSpotify/Models/SPTEncorePopUpDialogModel.swift create mode 100644 Sources/EeveeSpotify/Models/SPTEncorePopUpPresenter.swift diff --git a/Sources/EeveeSpotify/Helpers/PopUpHelper.swift b/Sources/EeveeSpotify/Helpers/PopUpHelper.swift new file mode 100644 index 0000000..f94175a --- /dev/null +++ b/Sources/EeveeSpotify/Helpers/PopUpHelper.swift @@ -0,0 +1,39 @@ +import UIKit +import Orion +import Foundation + +class PopUpHelper { + + static let sharedPresenter = type( + of: Dynamic.SPTEncorePopUpPresenter + .alloc(interface: SPTEncorePopUpPresenter.self) + ) + .shared() + + static func showPopUp( + message: String, + buttonText: String + ) { + + let model = Dynamic.SPTEncorePopUpDialogModel + .alloc(interface: SPTEncorePopUpDialogModel.self) + .initWithTitle( + "EeveeSpotify", + description: message, + image: nil, + primaryButtonTitle: buttonText, + secondaryButtonTitle: nil + ) + + let dialog = Dynamic.SPTEncorePopUpDialog + .alloc(interface: SPTEncorePopUpDialog.self) + .`init`() + + dialog.update(model) + dialog.setEventHandler({ + sharedPresenter.dismissPopupWithAnimate(true, clearQueue: false, completion: nil) + }) + + sharedPresenter.presentPopUp(dialog) + } +} diff --git a/Sources/EeveeSpotify/Helpers/WindowHelper.swift b/Sources/EeveeSpotify/Helpers/WindowHelper.swift deleted file mode 100644 index 60f8163..0000000 --- a/Sources/EeveeSpotify/Helpers/WindowHelper.swift +++ /dev/null @@ -1,29 +0,0 @@ -import UIKit -import Foundation - -class WindowHelper { - - static let shared = WindowHelper() - - let window: UIWindow - let rootViewController: UIViewController - - private init() { - self.window = UIApplication.shared.windows.first! - self.rootViewController = window.rootViewController! - } - - func presentViewController(_ viewController: UIViewController) { - rootViewController.present(viewController, animated: true) - } - - func showPopup(message: String, buttonText: String) { - - let alert = UIAlertController(title: "EeveeSpotify", message: message, preferredStyle: .alert) - - alert.overrideUserInterfaceStyle = .dark - alert.addAction(UIAlertAction(title: buttonText, style: .default)) - - presentViewController(alert) - } -} diff --git a/Sources/EeveeSpotify/Models/SPTEncorePopUpDialog.swift b/Sources/EeveeSpotify/Models/SPTEncorePopUpDialog.swift new file mode 100644 index 0000000..24a1f85 --- /dev/null +++ b/Sources/EeveeSpotify/Models/SPTEncorePopUpDialog.swift @@ -0,0 +1,7 @@ +import Foundation + +@objc protocol SPTEncorePopUpDialog { + func `init`() -> SPTEncorePopUpDialog + func update(_ popUpModel: SPTEncorePopUpDialogModel) + func setEventHandler(_ handler: @escaping () -> Void) +} \ No newline at end of file diff --git a/Sources/EeveeSpotify/Models/SPTEncorePopUpDialogModel.swift b/Sources/EeveeSpotify/Models/SPTEncorePopUpDialogModel.swift new file mode 100644 index 0000000..ea0436b --- /dev/null +++ b/Sources/EeveeSpotify/Models/SPTEncorePopUpDialogModel.swift @@ -0,0 +1,11 @@ +import Foundation + +@objc protocol SPTEncorePopUpDialogModel { + func initWithTitle( + _ title: String, + description: String, + image: Any?, + primaryButtonTitle: String, + secondaryButtonTitle: String? + ) -> SPTEncorePopUpDialogModel +} \ No newline at end of file diff --git a/Sources/EeveeSpotify/Models/SPTEncorePopUpPresenter.swift b/Sources/EeveeSpotify/Models/SPTEncorePopUpPresenter.swift new file mode 100644 index 0000000..d513b64 --- /dev/null +++ b/Sources/EeveeSpotify/Models/SPTEncorePopUpPresenter.swift @@ -0,0 +1,7 @@ +import Foundation + +@objc protocol SPTEncorePopUpPresenter { + static func shared() -> SPTEncorePopUpPresenter + func presentPopUp(_ popUp: SPTEncorePopUpDialog) + func dismissPopupWithAnimate(_ animate: Bool, clearQueue: Bool, completion: Any?) +} \ No newline at end of file diff --git a/Sources/EeveeSpotify/Tweak.x.swift b/Sources/EeveeSpotify/Tweak.x.swift index 878cd36..f2d63e5 100644 --- a/Sources/EeveeSpotify/Tweak.x.swift +++ b/Sources/EeveeSpotify/Tweak.x.swift @@ -38,7 +38,7 @@ struct EeveeSpotify: Tweak { DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) { - WindowHelper.shared.showPopup( + PopUpHelper.showPopUp( message: "Please log in and restart the app to get Premium.", buttonText: "Okay!" ) @@ -66,7 +66,7 @@ struct EeveeSpotify: Tweak { DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) { - WindowHelper.shared.showPopup( + PopUpHelper.showPopUp( message: "Unable to apply tweak: \(error)", buttonText: "OK" )