Skip to content

Commit

Permalink
scrolling issue
Browse files Browse the repository at this point in the history
  • Loading branch information
kugel3 committed Mar 22, 2024
1 parent bffcb39 commit 0dfa306
Showing 1 changed file with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ import ComposableArchitecture
struct Triggering<T: Hashable & Sendable>: Hashable, Sendable {
let created: Date = .now
let value: T

private init(value: T) {
self.value = value
}

static func updated(_ value: T) -> Self {
.init(value: value)
}
}

// MARK: - TransactionHistory
Expand Down Expand Up @@ -36,7 +44,7 @@ public struct TransactionHistory: Sendable, FeatureReducer {

var sections: IdentifiedArrayOf<TransactionSection> = []

var scrollTarget: Triggering<TXID?> = .init(value: nil)
var scrollTarget: Triggering<TXID?> = .updated(nil)

/// The currently selected month
var currentMonth: DateRangeItem.ID
Expand Down Expand Up @@ -237,7 +245,8 @@ public struct TransactionHistory: Sendable, FeatureReducer {
// Helper methods

func loadTransactionsFirstTime(state: inout State) -> Effect<Action> {
loadHistory(.down, scrollTarget: .latestTransaction, state: &state)
print("• loadTransactionsFirstTime: .latestTransaction"); return
loadHistory(.down, scrollTarget: .latestTransaction, state: &state)
}

/// Load history for the previously selected period, using the provided filters
Expand Down Expand Up @@ -285,6 +294,8 @@ public struct TransactionHistory: Sendable, FeatureReducer {

state.loading.isLoading = true

print("• loadHistory: \(direction), scroll: \(scrollTarget)")

let request = TransactionHistoryRequest(
account: state.account.accountAddress,
parameters: parameters,
Expand Down Expand Up @@ -340,21 +351,24 @@ extension TransactionHistory.State {
}

mutating func setScrollTarget(_ scrollTarget: TransactionHistory.ScrollTarget?) {
guard let scrollTarget else { return }
guard let scrollTarget else {
self.scrollTarget = .updated(nil)
return
}

func scrollToLatest() {
guard let lastSection = sections.first, let lastTransaction = lastSection.transactions.first else { return }
self.currentMonth = lastSection.month
self.scrollTarget = .init(value: lastTransaction.id)
self.scrollTarget = .updated(lastTransaction.id)
}

switch scrollTarget {
case let .transaction(txID):
self.scrollTarget = .init(value: txID)
self.scrollTarget = .updated(txID)
case let .beforeDate(date):
if let lastSectionInMonth = sections.first(where: { $0.day < date }), let lastTransaction = lastSectionInMonth.transactions.first {
self.currentMonth = lastSectionInMonth.month
self.scrollTarget = .init(value: lastTransaction.id)
self.scrollTarget = .updated(lastTransaction.id)
} else {
scrollToLatest()
}
Expand Down

0 comments on commit 0dfa306

Please sign in to comment.