Skip to content

Commit

Permalink
Fixed wrong token displayed on search
Browse files Browse the repository at this point in the history
- The correct token should now be displayed on search and also drag & drop regardless of the whether the token is locked or not.

Signed off by Mulili Nzuki<[email protected]>
  • Loading branch information
mul1sh authored and justin-stephenson committed Mar 3, 2021
1 parent 64ff165 commit aafe1da
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions FreeOTP/TokensViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class TokensViewController : UICollectionViewController, UICollectionViewDelegat
let size = self.collectionView(collectionView, layout: collectionView.collectionViewLayout, sizeForItemAt: indexPath)
let imageSize = CGSize(width: size.height, height: size.height)

if let token = searchingTokens ? searchedTokensArray[indexPath.row] : store.load(indexPath.row) {
if let token = getTokenAtIndex(tokenIndex: indexPath.row) {

cell.state = nil
var iconName = ""
Expand Down Expand Up @@ -151,7 +151,7 @@ class TokensViewController : UICollectionViewController, UICollectionViewDelegat
collectionView.deselectItem(at: indexPath, animated: true)

if let cell = collectionView.cellForItem(at: indexPath) as! TokenCell? {
if let token = store.load(indexPath.row) {
if let token = getTokenAtIndex(tokenIndex: indexPath.row) {
cell.state = token.codes
}
}
Expand Down Expand Up @@ -180,7 +180,7 @@ class TokensViewController : UICollectionViewController, UICollectionViewDelegat

// Drag and drop delegate methods
func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
if let token = store.load(indexPath.row) {
if let token = getTokenAtIndex(tokenIndex: indexPath.row) {
let itemProvider = NSItemProvider(object: token)

let dragItem = UIDragItem(itemProvider: itemProvider)
Expand Down Expand Up @@ -215,7 +215,7 @@ class TokensViewController : UICollectionViewController, UICollectionViewDelegat
let p = gestureRecognizer.location(in: collectionView)
if let currPath = collectionView?.indexPathForItem(at: p) {
if let cell = collectionView?.cellForItem(at: currPath) {
if let token = searchingTokens ? searchedTokensArray[currPath.row] : store.load(currPath.row) {
if let token = getTokenAtIndex(tokenIndex: currPath.row) {
UIView.animate(withDuration: 0.5, animations: {
cell.transform = CGAffineTransform(translationX: 1200, y: 0)
}, completion: { (Bool) -> Void in
Expand Down Expand Up @@ -362,6 +362,11 @@ class TokensViewController : UICollectionViewController, UICollectionViewDelegat
navigationItem.leftBarButtonItem = barButtonItem
}

// helper func to return token at a certain position depending on the state of the UICollectionView
private func getTokenAtIndex(tokenIndex: Int) -> Token? {
return searchingTokens ? searchedTokensArray[tokenIndex] : store.load(tokenIndex)
}

@objc func handleShowSearchBar() {
search(shouldShow: true)
searchBar.becomeFirstResponder()
Expand Down

0 comments on commit aafe1da

Please sign in to comment.