Skip to content

Commit

Permalink
scrolling up works sometimes
Browse files Browse the repository at this point in the history
  • Loading branch information
kugel3 committed Mar 15, 2024
1 parent 101d084 commit 6095ef8
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ public struct TransactionHistory: Sendable, FeatureReducer {
/// Workaround, TCA sends the sectionDisappeared after we dismiss, causing a run-time warning
var didDismiss: Bool = false

var transactionToScrollTo: TXID? = nil

struct Loading: Hashable, Sendable {
let parameters: TransactionHistoryParameters
var isLoading: Bool = false
Expand Down
103 changes: 92 additions & 11 deletions RadixWallet/Features/AccountHistory/TransactionHistory+View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -587,8 +587,32 @@ extension TransactionHistory {
}

public func updateUIView(_ uiView: UITableView, context: Context) {
context.coordinator.sections = sections
uiView.reloadData()
guard sections != context.coordinator.sections else { return }
let oldTransactions = context.coordinator.sections.allTransactions
let newTransactions = sections.allTransactions

if !oldTransactions.isEmpty, newTransactions.hasSuffix(oldTransactions) {
print(" •• updateUIView: inserted \(newTransactions.count - oldTransactions.count) above")
let oldContentHeight = uiView.contentSize.height
let oldContentOffset = uiView.contentOffset.y
context.coordinator.sections = sections
uiView.reloadData()
let newContentHeight = uiView.contentSize.height

let new = oldContentOffset + newContentHeight - oldContentHeight
let inserted = newContentHeight - oldContentHeight
print(" •• updateUIView: (height : offset): \(oldContentHeight) : \(oldContentOffset) -> \(newContentHeight) : \(new) [\(inserted)]")

uiView.contentOffset.y = oldContentOffset + newContentHeight - oldContentHeight
} else if !oldTransactions.isEmpty, newTransactions.hasPrefix(oldTransactions) {
print(" •• updateUIView: inserted \(newTransactions.count - oldTransactions.count) below")
context.coordinator.sections = sections
uiView.reloadData()
} else {
print(" •• updateUIView: everything changed")
context.coordinator.sections = sections
uiView.reloadData()
}
}

public func makeCoordinator() -> Coordinator {
Expand All @@ -607,8 +631,6 @@ extension TransactionHistory {

private var scrolling: (direction: ScrollDirection, count: Int) = (.down, 0)

var scrolledToTransaction: TXID? = nil

public init(
sections: IdentifiedArrayOf<TransactionSection>,
action: @escaping (Action) -> Void
Expand Down Expand Up @@ -668,11 +690,13 @@ extension TransactionHistory {
}
previousCell = indexPath

// We only want to pre-emptively load if they have been scrolling for a while in the same direction
if scrolling.count > 8 {
if scrolling.direction == .down, bottomTransactions.contains(txID) {
let transactions = sections.allTransactions
if scrolling.direction == .down, transactions.suffix(15).contains(txID) {
action(.nearingBottom)
scrolling.count = 0
} else if scrollDirection == .up, topTransactions.contains(txID) {
} else if scrollDirection == .up, transactions.prefix(7).contains(txID) {
action(.nearingTop)
scrolling.count = 0
}
Expand Down Expand Up @@ -707,14 +731,71 @@ extension TransactionHistory {
action(.monthChanged(newMonth))
month = newMonth
}
}
}
}

private var topTransactions: some Collection<TXID> {
sections.prefix(7).flatMap(\.transactions.ids).prefix(7)
}
extension Collection where Element: Equatable {
func hasPrefix(_ elements: some Collection<Element>) -> Bool {
prefix(elements.count).elementsEqual(elements)
}

private var bottomTransactions: [TXID] {
sections.suffix(15).flatMap(\.transactions.ids).suffix(15)
func hasSuffix(_ elements: some Collection<Element>) -> Bool {
suffix(elements.count).elementsEqual(elements)
}

func prefix(sharedWith other: some Collection<Element>) -> [Element] {
zip(self, other).prefix(while: ==).map(\.0)
}

func suffix(sharedWith other: some Collection<Element>) -> some Collection<Element> {
zip(self, other).map(Pair.init).suffix(while: \.equal).map(\.left)
}
}

extension IdentifiedArrayOf<TransactionHistory.TransactionSection> {
var allTransactions: [TXID] {
flatMap(\.transactions.ids)
}

var firstTransaction: TXID? {
first?.transactions.first?.id
}

func index(of transaction: TXID) -> IndexPath? {
for (index, section) in enumerated() {
if let row = section.transactions.ids.firstIndex(of: transaction) {
return .init(row: row, section: index)
}
}

return nil
}
}

// MARK: - Pair
public struct Pair<L, R> {
public let left: L
public let right: R

public init(_ left: L, _ right: R) {
self.left = left
self.right = right
}
}

// MARK: Sendable
extension Pair: Sendable where L: Sendable, R: Sendable {}

// MARK: Equatable
extension Pair: Equatable where L: Equatable, R: Equatable {}

// MARK: Hashable
extension Pair: Hashable where L: Hashable, R: Hashable {}

extension Pair where L == R, L: Equatable, R: Equatable {
/// The two components are equal
public var equal: Bool {
left == right
}
}

0 comments on commit 6095ef8

Please sign in to comment.