From 3d07069ee5bdf0c8fe71f2504409964eadf4008c Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Tue, 11 Jan 2022 11:42:41 -0500 Subject: [PATCH] Mark items read on selection --- .../Items/ItemCollectionViewCell.swift | 27 ++++++++++++------- .../Screens/Items/ItemsViewController.swift | 3 ++- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/Reader/Screens/Items/ItemCollectionViewCell.swift b/Reader/Screens/Items/ItemCollectionViewCell.swift index e6100d3..7203524 100644 --- a/Reader/Screens/Items/ItemCollectionViewCell.swift +++ b/Reader/Screens/Items/ItemCollectionViewCell.swift @@ -9,7 +9,7 @@ import UIKit import SwiftSoup protocol ItemCollectionViewCellDelegate: AnyObject { - func itemCellSelected(item: Item) + func itemCellSelected(cell: ItemCollectionViewCell, item: Item) } class ItemCollectionViewCell: UICollectionViewListCell { @@ -80,10 +80,23 @@ class ItemCollectionViewCell: UICollectionViewListCell { } contentLabel.isHidden = contentLabel.text?.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty ?? true - setColors() + updateColors() } - private func setColors() { + func setRead(_ read: Bool, animated: Bool) { + guard self.item.read != read else { return } + self.item.read = read + if animated { + // i don't know why .transition works but .animate doesn't + UIView.transition(with: self, duration: 0.2, options: .transitionCrossDissolve) { + self.updateColors() + } + } else { + updateColors() + } + } + + private func updateColors() { if item.read { titleLabel.textColor = .secondaryLabel feedTitleLabel.textColor = .secondaryLabel @@ -113,16 +126,12 @@ class ItemCollectionViewCell: UICollectionViewListCell { } @objc private func cellDoubleTapped() { - self.item.read = !self.item.read - // i don't know why .transition works but .animate doesn't - UIView.transition(with: self, duration: 0.2, options: .transitionCrossDissolve) { - self.setColors() - } + setRead(!item.read, animated: true) shouldHighlight = true } @objc private func cellSingleTapped() { - delegate?.itemCellSelected(item: item) + delegate?.itemCellSelected(cell: self, item: item) shouldHighlight = true } diff --git a/Reader/Screens/Items/ItemsViewController.swift b/Reader/Screens/Items/ItemsViewController.swift index 80301a7..22d24a3 100644 --- a/Reader/Screens/Items/ItemsViewController.swift +++ b/Reader/Screens/Items/ItemsViewController.swift @@ -103,7 +103,8 @@ extension ItemsViewController: UICollectionViewDelegate { } extension ItemsViewController: ItemCollectionViewCellDelegate { - func itemCellSelected(item: Item) { + func itemCellSelected(cell: ItemCollectionViewCell, item: Item) { + cell.setRead(true, animated: true) show(ReadViewController(item: item, fervorController: fervorController), sender: nil) } }