Skip to content

Commit c57de5e

Browse files
fix(previews): enable local currency SwiftUI previews
1 parent c85a865 commit c57de5e

4 files changed

Lines changed: 41 additions & 28 deletions

File tree

DashWallet.xcodeproj/xcshareddata/xcschemes/dashwallet.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
</Testables>
7474
</TestAction>
7575
<LaunchAction
76-
buildConfiguration = "Testflight"
76+
buildConfiguration = "Debug"
7777
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
7878
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
7979
language = "en"

DashWallet/AppDelegate.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ - (void)registerForPushNotifications {
8181
#pragma mark - UIApplicationDelegate
8282

8383
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(nullable NSDictionary *)launchOptions {
84+
#if DEBUG
85+
if ([NSProcessInfo.processInfo.environment[@"XCODE_RUNNING_FOR_PREVIEWS"] isEqualToString:@"1"]) {
86+
return YES;
87+
}
88+
#endif /* DEBUG */
89+
8490
#if FRESH_INSTALL
8591
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:[[NSBundle mainBundle] bundleIdentifier]];
8692
NSArray *secItemClasses = @[(__bridge id)kSecClassGenericPassword,

DashWallet/Sources/UI/Menu/Settings/LocalCurrency/LocalCurrencyView.swift

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,9 @@ struct LocalCurrencyView: View {
3838
self.onBack = onBack
3939
}
4040

41-
/// Preview / testing init — accepts a pre-built ViewModel.
42-
fileprivate init(
43-
viewModel: LocalCurrencyViewModel,
44-
onSelect: @escaping (String) -> Void,
45-
onBack: (() -> Void)? = nil
46-
) {
47-
self._viewModel = StateObject(wrappedValue: viewModel)
48-
self.onSelect = onSelect
49-
self.onBack = onBack
50-
}
51-
5241
var body: some View {
5342
ZStack(alignment: .top) {
54-
// Background
43+
background
5544

5645
LocalCurrencyScrollContentView(
5746
headerHeight: fullHeaderSize.height,
@@ -77,7 +66,10 @@ struct LocalCurrencyView: View {
7766
}
7867
}
7968
}
80-
.background(Color.primaryBackground)
69+
}
70+
71+
private var background: some View {
72+
Color.primaryBackground
8173
}
8274
}
8375

@@ -179,6 +171,24 @@ private struct LocalCurrencyTopOverlayView: View {
179171
// MARK: - Preview
180172

181173
#if DEBUG
174+
extension LocalCurrencyView {
175+
fileprivate init(
176+
viewModel: LocalCurrencyViewModel,
177+
onSelect: @escaping (String) -> Void,
178+
onBack: (() -> Void)? = nil
179+
) {
180+
self._viewModel = StateObject(wrappedValue: viewModel)
181+
self.onSelect = onSelect
182+
self.onBack = onBack
183+
}
184+
}
185+
186+
extension LocalCurrencyViewModel {
187+
convenience init(items: [CurrencyItem], selectedCode: String) {
188+
self.init(allItems: items, selectedCurrencyCode: selectedCode)
189+
}
190+
}
191+
182192
#Preview {
183193
LocalCurrencyView(
184194
viewModel: LocalCurrencyViewModel(

DashWallet/Sources/UI/Menu/Settings/LocalCurrency/LocalCurrencyViewModel.swift

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,14 @@ class LocalCurrencyViewModel: ObservableObject {
2727
@Published private(set) var filteredItems: [CurrencyItem] = []
2828
@Published var selectedCurrencyCode: String
2929

30-
/// Production init — loads currencies from CurrencyExchangerObjcWrapper.
31-
init(currencyCode: String? = nil) {
30+
init(allItems: [CurrencyItem], selectedCurrencyCode: String) {
31+
self.allItems = allItems
32+
self.selectedCurrencyCode = selectedCurrencyCode
33+
self.filteredItems = allItems
34+
setupSearch()
35+
}
36+
37+
convenience init(currencyCode: String? = nil) {
3238
let formatter = NumberFormatter()
3339
formatter.maximumFractionDigits = 2
3440
formatter.minimumFractionDigits = 2
@@ -41,18 +47,7 @@ class LocalCurrencyViewModel: ObservableObject {
4147
priceString: formatter.string(from: price.price)
4248
)
4349
}
44-
self.allItems = items
45-
self.selectedCurrencyCode = currencyCode ?? App.fiatCurrency
46-
self.filteredItems = items
47-
setupSearch()
48-
}
49-
50-
/// Preview / testing init — accepts pre-built items without DashSync.
51-
init(items: [CurrencyItem], selectedCode: String) {
52-
self.allItems = items
53-
self.selectedCurrencyCode = selectedCode
54-
self.filteredItems = items
55-
setupSearch()
50+
self.init(allItems: items, selectedCurrencyCode: currencyCode ?? App.fiatCurrency)
5651
}
5752

5853
func select(currencyCode: String) {
@@ -82,6 +77,8 @@ class LocalCurrencyViewModel: ObservableObject {
8277
}
8378
}
8479

80+
81+
8582
// MARK: - Flag mapping
8683

8784
extension LocalCurrencyViewModel {

0 commit comments

Comments
 (0)