Skip to content

Commit

Permalink
- Bug fix: fix favourite re-ordering
Browse files Browse the repository at this point in the history
- Bug fix: prevent re-ordering above XTZ
  • Loading branch information
simonmcl committed Oct 4, 2024
1 parent 758068c commit a797d71
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 67 deletions.
36 changes: 31 additions & 5 deletions Kukai Mobile/Modules/Account/FavouriteBalancesViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import UIKit
import KukaiCoreSwift
import Combine

class FavouriteBalancesViewModel: ViewModel, UITableViewDiffableDataSourceHandler {
protocol MoveableDiffableDataSourceDelegate: AnyObject {
func didMove()
}

class FavouriteBalancesViewModel: ViewModel, UITableViewDiffableDataSourceHandler, MoveableDiffableDataSourceDelegate {

private var accountDataRefreshedCancellable: AnyCancellable?

Expand Down Expand Up @@ -43,6 +47,8 @@ class FavouriteBalancesViewModel: ViewModel, UITableViewDiffableDataSourceHandle

class MoveableDiffableDataSource: UITableViewDiffableDataSource<SectionEnum, CellDataType> {

weak var delegate: MoveableDiffableDataSourceDelegate? = nil

override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
if indexPath.section == 0 {
return false
Expand All @@ -64,10 +70,19 @@ class FavouriteBalancesViewModel: ViewModel, UITableViewDiffableDataSourceHandle
return
}

// Prevent anything from going above XTZ
guard destinationIndexPath.section != 0 else {
var currentSnapshot = self.snapshot()
currentSnapshot.moveSection(sourceIndexPath.section, afterSection: sourceIndexPath.section-1)
self.apply(currentSnapshot)
return
}


// Record the new state on disk
if let address = DependencyManager.shared.selectedWalletAddress,
let token = (itemIdentifier(for: sourceIndexPath) as? Token),
TokenStateService.shared.moveFavouriteBalance(forAddress: address, forToken: token, toIndex: destinationIndexPath.section) {
TokenStateService.shared.moveFavouriteBalance(forAddress: address, forToken: token, toIndex: destinationIndexPath.section-1) {

// We have 1 row per section (to acheive a certain UI). By default, move tries to take a row and move it to another section. We need to modify this to move sections around instead
// Get the section that its trying to be added too. If its at index 0, then user wants to move source, above destination, so instead assign it to the previous section
Expand All @@ -83,19 +98,22 @@ class FavouriteBalancesViewModel: ViewModel, UITableViewDiffableDataSourceHandle
}

self.apply(currentSnapshot)
DependencyManager.shared.balanceService.updateTokenStates(forAddress: address, selectedAccount: true)
self.delegate?.didMove()

} else {
//self.state = .failure(KukaiError.internalApplicationError(error: "Unable to rearrange favorite"), "Unable to rearrange favorite")
}
}
}


func didMove() {
self.updateChanges()
self.refresh(animate: false)
}

// MARK: - Functions
func makeDataSource(withTableView tableView: UITableView) {
dataSource = MoveableDiffableDataSource(tableView: tableView, cellProvider: { [weak self] tableView, indexPath, item in
let moveableDiff = MoveableDiffableDataSource(tableView: tableView, cellProvider: { [weak self] tableView, indexPath, item in

if let amount = item as? XTZAmount {
if let cell = tableView.dequeueReusableCell(withIdentifier: "FavouriteTokenCell", for: indexPath) as? FavouriteTokenCell {
Expand Down Expand Up @@ -128,6 +146,9 @@ class FavouriteBalancesViewModel: ViewModel, UITableViewDiffableDataSourceHandle
return UITableViewCell()
})

moveableDiff.delegate = self

dataSource = moveableDiff
dataSource?.defaultRowAnimation = .fade
}

Expand Down Expand Up @@ -212,4 +233,9 @@ class FavouriteBalancesViewModel: ViewModel, UITableViewDiffableDataSourceHandle
func showReorderButton() -> Bool {
return favouriteCount > 1
}

func updateChanges() {
guard let address = DependencyManager.shared.selectedWalletAddress else { return }
DependencyManager.shared.balanceService.updateTokenStates(forAddress: address, selectedAccount: true)
}
}
17 changes: 8 additions & 9 deletions Kukai Mobile/Services/TokenStateService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public class TokenStateService {
if isFavourite(forAddress: address, token: token) == nil {
let count = favouriteBalances[address]?.count ?? 0
addBlankFavouriteBalanceIfNeeded(forAddress: address)
favouriteBalances[address]?[balanceId(from: token)] = count + 1
favouriteBalances[address]?[balanceId(from: token)] = count
return writeFavouriteBalances()
}

Expand All @@ -87,7 +87,7 @@ public class TokenStateService {
if isFavourite(forAddress: address, nft: nft) == nil {
let count = favouriteCollectibles[address]?.count ?? 0
addBlankFavouriteCollectibleIfNeeded(forAddress: address)
favouriteCollectibles[address]?[nftId(from: nft)] = count + 1
favouriteCollectibles[address]?[nftId(from: nft)] = count
return writeFavouriteCollectibles()
}

Expand Down Expand Up @@ -138,8 +138,6 @@ public class TokenStateService {

public func removeFavourite(forAddress address: String, token: Token) -> Bool {
let balanceId = balanceId(from: token)
let currentSortIndex = favouriteBalances[address]?[balanceId] ?? -1

favouriteBalances[address]?.removeValue(forKey: balanceId)
recomputeSortOrder(inDict: &favouriteBalances, withAddress: address, andMove: nil)

Expand All @@ -148,8 +146,6 @@ public class TokenStateService {

public func removeFavourite(forAddress address: String, nft: NFT) -> Bool {
let balanceId = nftId(from: nft)
let currentSortIndex = favouriteCollectibles[address]?[balanceId] ?? -1

favouriteCollectibles[address]?.removeValue(forKey: nftId(from: nft))
recomputeSortOrder(inDict: &favouriteCollectibles, withAddress: address, andMove: nil)

Expand Down Expand Up @@ -210,13 +206,16 @@ public class TokenStateService {
}

if let moveItem = optionalMove {
guard moveItem.to > 0, moveItem.to <= sortedArray.count+1, moveItem.from >= 0, moveItem.from <= sortedArray.count else { return }
guard moveItem.to >= 0, moveItem.to < sortedArray.count, moveItem.from >= 0, moveItem.from < sortedArray.count else {
return
}

sortedArray.move(fromOffsets: IndexSet([moveItem.from-1]), toOffset: moveItem.to-1)
let oldOtem = sortedArray.remove(at: moveItem.from)
sortedArray.insert(oldOtem, at: moveItem.to)
}

for (index, item) in sortedArray.enumerated() {
tempDict[item.key] = index + 1
tempDict[item.key] = index
}

dict[address] = tempDict
Expand Down
Loading

0 comments on commit a797d71

Please sign in to comment.