Refactor UIAlertController Logic

This commit is contained in:
Leon Böttger committed 2025-11-10 19:44:15 +01:00
1 parent 21f0f865b6
commit 4e73fced74
8 files changed
+63 -42

No files matched your search

+25 -11
View File
@@ -41,9 +41,14 @@ struct DeviceListView: View {
@State private var qrCode: Image? = nil
@StateObject var sendModel = SendModel()
#if !EXTENSION
@StateObject var receiveModel = ReceiveModel()
#endif
#if !EXTENSION
@StateObject var receiveModel = ReceiveModel(controlPlusScreen: { show in
log("[DeviceListView] Setting showPlusScreen to \(show)")
DeviceListViewModel.sharedInstance.showPlusScreen = show
})
#endif
@ObservedObject var model = DeviceListViewModel.sharedInstance
@Environment(\.scenePhase) var scenePhase
@Environment(\.sheetActive) var sheetActive
@@ -54,7 +59,7 @@ struct DeviceListView: View {
let showsLoadingIndicator = nearbyConnectionManager.hasLocalNetworkPermission && nearbyConnectionManager.isConnectedToLocalNetwork
BottomBarView(header: "QuickDrop ", navigationBarLayout: isShareExtension() ? .SmallOnlyAlways : .Default, bottomViewHeight: 30) {
BottomBarView(header: "QuickDrop", navigationBarLayout: isShareExtension() ? .SmallOnlyAlways : .Default, bottomViewHeight: 30) {
VStack {
VStack(alignment: .leading, spacing: 8) {
@@ -185,7 +190,7 @@ struct DeviceListView: View {
log("[ScenePhase] App became active")
NearbyConnectionManager.shared.startDeviceDiscovery()
#if !EXTENSION
#if !EXTENSION
NearbyConnectionManager.shared.becomeVisible()
if Settings.shared.incomingTransmissionCount > 0 {
@@ -193,18 +198,21 @@ struct DeviceListView: View {
requestReviewOnce()
}
}
#endif
#endif
}
if newValue == .background {
log("[ScenePhase] App went to background")
#if !EXTENSION
#if !EXTENSION
NearbyConnectionManager.shared.becomeInvisible()
#endif
#endif
NearbyConnectionManager.shared.stopDeviceDiscovery()
}
}
#if !EXTENSION
.modifier(PlusVersionSheetModifier(sheetActive: $model.showPlusScreen, isPlusVersion: $settings.gotPlus))
#endif
.animation(.smooth, value: nearbyConnectionManager.hasLocalNetworkPermission)
.animation(.smooth, value: nearbyConnectionManager.isConnectedToLocalNetwork)
.navigationBarItems(trailing: ZStack {
@@ -218,6 +226,12 @@ struct DeviceListView: View {
}
}
final class DeviceListViewModel: SharedInstance {
static var sharedInstance = DeviceListViewModel()
@Published var showPlusScreen = false
}
struct DeviceFilePickerButton: View {
@@ -297,11 +311,11 @@ struct DeviceButtonLabel: View {
func isShareExtension() -> Bool {
#if EXTENSION
#if EXTENSION
return true
#else
#else
return false
#endif
#endif
}
+20 -11
View File
@@ -18,7 +18,7 @@ final class ProgressAlert {
private weak var progressView: UIProgressView?
// MARK: - Initial Accept / Decline
func askForUserPermission(title: String, message: String, acceptLabel: String, acceptAlwaysLabel: String?, rejectLabel: String, onAccept: @escaping (AcceptAction) -> Void, onCancel: @escaping () -> Void) {
func askForUserPermission(title: String, message: String, acceptLabel: String, acceptAlwaysLabel: String?, rejectLabel: String, onAccept: @escaping (AcceptAction) -> Void) {
guard let vc = topMostViewController() else { return }
let alert = UIAlertController(title: title,
@@ -31,28 +31,23 @@ final class ProgressAlert {
alert.addAction(UIAlertAction(title: acceptLabel, style: .default, handler: { _ in
onAccept(.Accept)
self.showProgressAlert(on: vc, onCancel: onCancel)
}))
if let acceptAlways = acceptAlwaysLabel {
alert.addAction(UIAlertAction(title: acceptAlways, style: .default, handler: { _ in
onAccept(.AcceptAlways)
self.showProgressAlert(on: vc, onCancel: onCancel)
}))
}
vc.present(alert, animated: true)
}
func showProgressAlert(onCancel: @escaping () -> Void) {
guard let vc = topMostViewController() else { return }
self.showProgressAlert(on: vc, onCancel: onCancel)
}
// MARK: - Progress Alert
private func showProgressAlert(on vc: UIViewController, onCancel: @escaping () -> Void) {
private func showProgressAlert(onCancel: @escaping () -> Void) {
guard let vc = topMostViewController() else { return }
let alert = UIAlertController(title: "Receiving".localized(),
message: "\n\n",
preferredStyle: .alert)
@@ -77,11 +72,25 @@ final class ProgressAlert {
vc.present(alert, animated: true)
}
// MARK: - Update Progress
// progress: 0…1 → update progress, nil → finished
func updateProgress(_ progress: Double?, completion: @escaping () -> Void = {}) {
func updateProgress(_ progress: Double?, onCancel: @escaping () -> Void, completion: @escaping () -> Void = {}) {
// No progress to show, nothing shown currently → just complete
if progressAlert == nil && progress == nil {
completion()
return
}
DispatchQueue.main.async {
// Show progress if not shown yet
if self.progressAlert == nil {
self.showProgressAlert(onCancel: onCancel)
}
if let p = progress {
self.progressView?.setProgress(Float(p), animated: true)
completion()