Primera build enviada

This commit is contained in:
2026-01-19 14:40:43 +01:00
parent c6be398e5a
commit b03d35194f
36 changed files with 1641 additions and 561 deletions
@@ -36,6 +36,10 @@ struct ImportDataView: View {
private let accountRepository = AccountRepository()
private var availableAccounts: [Account] {
accountStore.accounts.filter { $0.safeId != nil }
}
init(importContext: ImportContext = .settings) {
self.importContext = importContext
}
@@ -146,8 +150,17 @@ struct ImportDataView: View {
Text(errorMessage ?? "")
}
.onAppear {
if selectedAccountId == nil {
selectedAccountId = accountStore.selectedAccount?.id ?? accountStore.accounts.first?.id
// Ensure selectedAccountId is valid and exists in availableAccounts
let validIds = Set(availableAccounts.compactMap { $0.safeId })
if selectedAccountId == nil || !validIds.contains(selectedAccountId!) {
selectedAccountId = accountStore.selectedAccount?.safeId ?? availableAccounts.first?.safeId
}
}
.onChange(of: availableAccounts) { _, newAccounts in
// Re-validate selectedAccountId when accounts change
let validIds = Set(newAccounts.compactMap { $0.safeId })
if let currentId = selectedAccountId, !validIds.contains(currentId) {
selectedAccountId = newAccounts.first?.safeId
}
}
.onChange(of: accountSelection) { _, _ in
@@ -219,18 +232,17 @@ Personal,Stocks,Index Fund,2024-01-01,15000,12000,Long-term
}
private var shouldShowAccountSelection: Bool {
importContext == .onboarding
// Show account selection when there are multiple accounts (Premium) or during onboarding
iapService.isPremium && availableAccounts.count > 1 || importContext == .onboarding
}
private var importFooterText: String {
if importContext == .onboarding {
if shouldShowAccountSelection {
return iapService.isPremium
? "Import will be added to the selected account."
: "Free users import into the existing account."
: "Free users import into the Default account."
}
return iapService.isPremium
? "Accounts are imported as provided."
: "Free users import into the selected account."
return "Data will be imported into your Default account."
}
private var accountSection: some View {
@@ -245,8 +257,8 @@ Personal,Stocks,Index Fund,2024-01-01,15000,12000,Long-term
if accountSelection == .existing {
Picker("Import into", selection: $selectedAccountId) {
ForEach(accountStore.accounts) { account in
Text(account.name).tag(Optional(account.id))
ForEach(availableAccounts, id: \.objectID) { account in
Text(account.name).tag(Optional(account.safeId))
}
}
.disabled(isImporting)
@@ -273,9 +285,9 @@ Personal,Stocks,Index Fund,2024-01-01,15000,12000,Long-term
}
private var selectedAccountName: String? {
accountStore.accounts.first { $0.id == selectedAccountId }?.name
availableAccounts.first { $0.safeId == selectedAccountId }?.name
?? accountStore.selectedAccount?.name
?? accountStore.accounts.first?.name
?? availableAccounts.first?.name
}
private func validateNewAccountName() -> String? {
@@ -355,12 +367,17 @@ Personal,Stocks,Index Fund,2024-01-01,15000,12000,Long-term
}
private func handleImportContent(_ content: String) {
let shouldForceSingleAccount = importContext == .onboarding
let allowMultipleAccounts = iapService.isPremium && !shouldForceSingleAccount
var defaultAccountName = accountStore.selectedAccount?.name
?? accountStore.accounts.first?.name
// For non-Premium users or when only one account exists, use the Default account
// For Premium users with multiple accounts, respect their selection
let useAccountSelection = shouldShowAccountSelection && iapService.isPremium
let allowMultipleAccounts = false // Always import into a single account
if shouldForceSingleAccount && iapService.isPremium {
var defaultAccountName = accountStore.accounts.first(where: { $0.isDefaultAccount })?.name
?? accountStore.selectedAccount?.name
?? accountStore.accounts.first?.name
?? Account.defaultAccountName
if useAccountSelection {
if accountSelection == .new {
accountErrorMessage = validateNewAccountName()
guard accountErrorMessage == nil else { return }
@@ -375,7 +392,7 @@ Personal,Stocks,Index Fund,2024-01-01,15000,12000,Long-term
)
defaultAccountName = account.name
} else {
defaultAccountName = selectedAccountName
defaultAccountName = selectedAccountName ?? defaultAccountName
}
}
@@ -397,7 +414,11 @@ Personal,Stocks,Index Fund,2024-01-01,15000,12000,Long-term
isImporting = false
if importResult.errors.isEmpty {
resultMessage = "Imported \(importResult.sourcesCreated) sources and \(importResult.snapshotsCreated) snapshots."
var message = "Imported \(importResult.sourcesCreated) sources and \(importResult.snapshotsCreated) snapshots."
if importResult.snapshotsUpdated > 0 {
message += " Updated \(importResult.snapshotsUpdated) existing snapshots."
}
resultMessage = message
tabSelection.selectedTab = 0
dismiss()
} else {