From aafe1da0f20d75a14dcb807b2f1155396057929a Mon Sep 17 00:00:00 2001 From: mulili Date: Tue, 2 Mar 2021 21:30:25 +0300 Subject: [PATCH] Fixed wrong token displayed on search - 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 --- FreeOTP/TokensViewController.swift | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/FreeOTP/TokensViewController.swift b/FreeOTP/TokensViewController.swift index d353dd4..c6a7105 100644 --- a/FreeOTP/TokensViewController.swift +++ b/FreeOTP/TokensViewController.swift @@ -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 = "" @@ -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 } } @@ -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) @@ -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 @@ -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()