Fix pull to refresh on top-level navigation views

This commit is contained in:
William Brawner 2022-06-15 20:24:21 -06:00
parent a6e0da2f96
commit 70c33c5ab0
4 changed files with 28 additions and 14 deletions

View file

@ -17,7 +17,7 @@ struct BudgetDetailsView: View {
var body: some View { var body: some View {
InlineLoadingView( InlineLoadingView(
data: self.$dataStore.overview, data: self.$dataStore.overview,
action: { await self.dataStore.loadOverview(self.budget) }, action: { await self.dataStore.loadOverview(showLoader: false) },
errorTextLocalizedStringKey: "budgets_load_failure" errorTextLocalizedStringKey: "budgets_load_failure"
) { overview in ) { overview in
List { List {
@ -33,7 +33,7 @@ struct BudgetDetailsView: View {
} }
.listStyle(.insetGrouped) .listStyle(.insetGrouped)
.refreshable { .refreshable {
await dataStore.loadOverview(self.budget) await dataStore.loadOverview(showLoader: false)
} }
.navigationBarItems(trailing: Button(action: { .navigationBarItems(trailing: Button(action: {
dataStore.editBudget() dataStore.editBudget()

View file

@ -154,8 +154,13 @@ class DataStore : ObservableObject {
await self.selectBudget(budget) await self.selectBudget(budget)
} }
func loadOverview(_ budget: Budget) async { func loadOverview(showLoader: Bool = true) async {
self.overview = .loading guard case let .success(budget) = self.budget else {
return
}
if showLoader {
self.overview = .loading
}
do { do {
let budgetBalance = try await self.apiService.sumTransactions(budgetId: budget.id, categoryId: nil, from: nil, to: nil) let budgetBalance = try await self.apiService.sumTransactions(budgetId: budget.id, categoryId: nil, from: nil, to: nil)
let categories = try await self.apiService.getCategories(budgetId: budget.id, expense: nil, archived: false, count: nil, page: nil) let categories = try await self.apiService.getCategories(budgetId: budget.id, expense: nil, archived: false, count: nil, page: nil)
@ -190,7 +195,7 @@ class DataStore : ObservableObject {
func selectBudget(_ budget: Budget?) async { func selectBudget(_ budget: Budget?) async {
if let budget = budget { if let budget = budget {
self.budget = .success(budget) self.budget = .success(budget)
await loadOverview(budget) await loadOverview()
await getTransactions() await getTransactions()
await getCategories(budgetId: budget.id, expense: nil, archived: nil, count: nil, page: nil) await getCategories(budgetId: budget.id, expense: nil, archived: nil, count: nil, page: nil)
await getRecurringTransactions() await getRecurringTransactions()
@ -221,15 +226,17 @@ class DataStore : ObservableObject {
} }
@Published var selectedCategory: TwigsCore.Category? = nil @Published var selectedCategory: TwigsCore.Category? = nil
func getCategories() async { func getCategories(showLoader: Bool = true) async {
guard case let .success(budget) = self.budget else { guard case let .success(budget) = self.budget else {
return return
} }
await self.getCategories(budgetId: budget.id) await self.getCategories(budgetId: budget.id, showLoader: showLoader)
} }
func getCategories(budgetId: String, expense: Bool? = nil, archived: Bool? = false, count: Int? = nil, page: Int? = nil) async { func getCategories(budgetId: String, expense: Bool? = nil, archived: Bool? = false, count: Int? = nil, page: Int? = nil, showLoader: Bool = true) async {
self.categories = .loading if showLoader {
self.categories = .loading
}
do { do {
let categories = try await apiService.getCategories(budgetId: budgetId, expense: expense, archived: archived, count: count, page: page) let categories = try await apiService.getCategories(budgetId: budgetId, expense: expense, archived: archived, count: count, page: page)
self.categories = .success(categories) self.categories = .success(categories)
@ -307,11 +314,13 @@ class DataStore : ObservableObject {
} }
@Published var selectedRecurringTransaction: RecurringTransaction? = nil @Published var selectedRecurringTransaction: RecurringTransaction? = nil
func getRecurringTransactions() async { func getRecurringTransactions(showLoader: Bool = true) async {
guard case let .success(budget) = self.budget else { guard case let .success(budget) = self.budget else {
return return
} }
self.recurringTransactions = .loading if showLoader {
self.recurringTransactions = .loading
}
do { do {
let transactions = try await self.apiService.getRecurringTransactions(budget.id) let transactions = try await self.apiService.getRecurringTransactions(budget.id)
self.recurringTransactions = .success(transactions.sorted(by: { $0.title < $1.title })) self.recurringTransactions = .success(transactions.sorted(by: { $0.title < $1.title }))
@ -398,12 +407,14 @@ class DataStore : ObservableObject {
} }
@Published var selectedTransaction: Transaction? = nil @Published var selectedTransaction: Transaction? = nil
func getTransactions() async { func getTransactions(showLoader: Bool = true) async {
guard let budgetId = self.budgetId else { guard let budgetId = self.budgetId else {
self.transactions = .error(NetworkError.unknown) self.transactions = .error(NetworkError.unknown)
return return
} }
self.transactions = .loading if showLoader {
self.transactions = .loading
}
do { do {
var categoryIds: [String] = [] var categoryIds: [String] = []
if let categoryId = categoryId { if let categoryId = categoryId {

View file

@ -23,6 +23,9 @@ struct RecurringTransactionsListView: View {
RecurringTransactionsListItemView(transaction) RecurringTransactionsListItemView(transaction)
} }
} }
.refreshable {
await dataStore.getRecurringTransactions(showLoader: false)
}
} }
.toolbar { .toolbar {
ToolbarItem(placement: .navigationBarTrailing) { ToolbarItem(placement: .navigationBarTrailing) {

View file

@ -95,7 +95,7 @@ struct TransactionListView<Content>: View where Content: View {
} }
.searchable(text: $search) .searchable(text: $search)
.refreshable { .refreshable {
await dataStore.getTransactions() await dataStore.getTransactions(showLoader: false)
} }
.sheet( .sheet(
isPresented: .constant(addingTransaction), isPresented: .constant(addingTransaction),