Skip to content

Commit

Permalink
Show dev environment indicator in Sync Settings (#2265)
Browse files Browse the repository at this point in the history
Task/Issue URL: https://app.asana.com/0/1201493110486074/1206158416073061/f

Description:
When dev environment is active, a small pill button indicating this is shown on top of Sync Settings view.
  • Loading branch information
ayoy authored Dec 13, 2023
1 parent fbf755c commit 354eb36
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
8 changes: 7 additions & 1 deletion DuckDuckGo/SyncSettingsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,13 @@ class SyncSettingsViewController: UIHostingController<SyncSettingsView> {
self.syncService = syncService
self.syncBookmarksAdapter = syncBookmarksAdapter

let viewModel = SyncSettingsViewModel()
let viewModel = SyncSettingsViewModel(
isOnDevEnvironment: { syncService.serverEnvironment == .development },
switchToProdEnvironment: {
syncService.updateServerEnvironment(.production)
UserDefaults.standard.set(ServerEnvironment.production.description, forKey: UserDefaultsWrapper<String>.Key.syncEnvironment.rawValue)
}
)

super.init(rootView: SyncSettingsView(model: viewModel))

Expand Down
2 changes: 1 addition & 1 deletion DuckDuckGoTests/SyncManagementViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class SyncManagementViewModelTests: XCTestCase, SyncManagementViewModelDelegate
fileprivate var monitor = Monitor<SyncManagementViewModelDelegate>()

lazy var model: SyncSettingsViewModel = {
let model = SyncSettingsViewModel()
let model = SyncSettingsViewModel(isOnDevEnvironment: { false }, switchToProdEnvironment: {})
model.delegate = self
return model
}()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,16 @@ public class SyncSettingsViewModel: ObservableObject {
@Published var recoveryCode = ""

public weak var delegate: SyncManagementViewModelDelegate?

public init() { }
private(set) var isOnDevEnvironment: Bool
private(set) var switchToProdEnvironment: () -> Void = {}

public init(isOnDevEnvironment: @escaping () -> Bool, switchToProdEnvironment: @escaping () -> Void) {
self.isOnDevEnvironment = isOnDevEnvironment()
self.switchToProdEnvironment = { [weak self] in
switchToProdEnvironment()
self?.isOnDevEnvironment = isOnDevEnvironment()
}
}

func disableSync() {
isBusy = true
Expand Down
34 changes: 34 additions & 0 deletions LocalPackages/SyncUI/Sources/SyncUI/Views/SyncSettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public struct SyncSettingsView: View {
let timer = Timer.publish(every: 3, on: .main, in: .common).autoconnect()
@State var isSyncWithSetUpSheetVisible = false
@State var isRecoverSyncedDataSheetVisible = false
@State var isEnvironmentSwitcherInstructionsVisible = false

public init(model: SyncSettingsViewModel) {
self.model = model
Expand Down Expand Up @@ -121,6 +122,8 @@ extension SyncSettingsView {
}
Spacer()
}
} header: {
devEnvironmentIndicator()
} footer: {
HStack {
Spacer()
Expand Down Expand Up @@ -265,6 +268,7 @@ extension SyncSettingsView {
.fill(.green)
.frame(width: 8)
.padding(.bottom, 1)
devEnvironmentIndicator()
}
} footer: {
Text(UserText.turnSyncOffSectionFooter)
Expand Down Expand Up @@ -344,6 +348,36 @@ extension SyncSettingsView {
}
}

@ViewBuilder
func devEnvironmentIndicator() -> some View {
if model.isOnDevEnvironment {
Button(action: {
isEnvironmentSwitcherInstructionsVisible.toggle()
}, label: {
if #available(iOS 15.0, *) {
Text("Dev environment")
.daxFootnoteRegular()
.padding(.horizontal, 10)
.padding(.vertical, 2)
.foregroundColor(.white)
.background(Color.red40)
.clipShape(RoundedRectangle(cornerRadius: 10))
} else {
Text("Dev environment")
}
})
.alert(isPresented: $isEnvironmentSwitcherInstructionsVisible) {
Alert(
title: Text("You're using Sync Development environment"),
primaryButton: .default(Text("Keep Development")),
secondaryButton: .destructive(Text("Switch to Production"), action: model.switchToProdEnvironment)
)
}
} else {
EmptyView()
}
}

enum LimitedItemType {
case bookmarks
case credentials
Expand Down

0 comments on commit 354eb36

Please sign in to comment.