generated from GSM-MSG/MSG-Repository-Generator
-
Notifications
You must be signed in to change notification settings - Fork 0
Book Detail 화면 UI #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a670056
Book Detail 화면 UI
baekteun 928ff8f
ReadingLog.date 추가
baekteun 824bb59
ReadingLog date 표시
baekteun 5e39221
책을 찾지 못할 경우 예외 처리
baekteun 76d987e
계산된 총 시간 viewModel로부터 주입
baekteun 8ad0414
performAndWait<T>(_:) -> T 로 All cell들 정보 표시
baekteun 273c148
trait collection change handling
baekteun c8d923d
managedObjectContext 활용
baekteun 7f5eb52
Update ONMIR/Feature/BookDetail/BookDetailViewModel.swift
baekteun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| import CoreData | ||
|
|
||
| #warning("TODO: Sendable") | ||
| extension BookEntity: @unchecked Sendable {} | ||
| extension QuoteEntity: @unchecked Sendable {} | ||
| extension ReadingLogEntity: @unchecked Sendable {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| import UIKit | ||
| import CoreData | ||
| import SnapKit | ||
|
|
||
| #warning("TODO: UI") | ||
| final class AllQuotesViewController: UIViewController { | ||
| private let tableView = UITableView() | ||
| private let bookObjectID: NSManagedObjectID | ||
| private lazy var fetchedResultsController = makeFetchedResultsController() | ||
| private lazy var dataSource = makeDataSource() | ||
|
|
||
| init(bookObjectID: NSManagedObjectID) { | ||
| self.bookObjectID = bookObjectID | ||
| super.init(nibName: nil, bundle: nil) | ||
| } | ||
|
|
||
| required init?(coder: NSCoder) { | ||
| fatalError("init(coder:) has not been implemented") | ||
| } | ||
|
|
||
| override func viewDidLoad() { | ||
| super.viewDidLoad() | ||
| setupUI() | ||
|
|
||
| do { | ||
| try fetchedResultsController.performFetch() | ||
| updateSnapshot() | ||
| } catch { | ||
| print("Failed to fetch quotes: \(error)") | ||
baekteun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| private func setupUI() { | ||
| title = "All Quotes" | ||
| view.backgroundColor = .systemBackground | ||
|
|
||
| tableView.delegate = self | ||
| tableView.register(UITableViewCell.self, forCellReuseIdentifier: "QuoteCell") | ||
baekteun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| tableView.rowHeight = UITableView.automaticDimension | ||
| tableView.estimatedRowHeight = 80 | ||
|
|
||
| view.addSubview(tableView) | ||
| tableView.snp.makeConstraints { make in | ||
| make.edges.equalToSuperview() | ||
| } | ||
| } | ||
|
|
||
| private func makeDataSource() -> UITableViewDiffableDataSource<Int, NSManagedObjectID> { | ||
| let dataSource = UITableViewDiffableDataSource<Int, NSManagedObjectID>(tableView: tableView) { [weak self] tableView, indexPath, objectID in | ||
| let cell = tableView.dequeueReusableCell(withIdentifier: "QuoteCell", for: indexPath) | ||
|
|
||
| guard let self = self | ||
| else { return cell } | ||
|
|
||
| let context = self.fetchedResultsController.managedObjectContext | ||
|
|
||
| guard | ||
| let quote = context.object(with: objectID) as? QuoteEntity | ||
| else { | ||
| return cell | ||
| } | ||
|
|
||
| cell.textLabel?.text = quote.managedObjectContext?.performAndWait { quote.content } ?? "" | ||
| cell.detailTextLabel?.text = quote.managedObjectContext?.performAndWait { "\(quote.page) P" } ?? "" | ||
| cell.textLabel?.numberOfLines = 0 | ||
| cell.accessoryType = .disclosureIndicator | ||
|
|
||
| return cell | ||
| } | ||
|
|
||
| tableView.dataSource = dataSource | ||
| return dataSource | ||
| } | ||
|
|
||
| private func makeFetchedResultsController() -> NSFetchedResultsController<QuoteEntity> { | ||
| let context = ContextManager.shared.mainContext | ||
|
|
||
| guard let book = context.object(with: bookObjectID) as? BookEntity else { | ||
| assertionFailure("Book not found") | ||
| self.dismiss(animated: true) | ||
| return .init() | ||
| } | ||
|
|
||
| let request: NSFetchRequest<QuoteEntity> = QuoteEntity.fetchRequest() | ||
| request.predicate = NSPredicate(format: "book == %@", book) | ||
| request.sortDescriptors = [NSSortDescriptor(keyPath: \QuoteEntity.page, ascending: false)] | ||
|
|
||
| let controller = NSFetchedResultsController( | ||
| fetchRequest: request, | ||
| managedObjectContext: context, | ||
| sectionNameKeyPath: nil, | ||
| cacheName: nil | ||
| ) | ||
|
|
||
| controller.delegate = self | ||
| return controller | ||
| } | ||
|
|
||
| private func updateSnapshot() { | ||
| var snapshot = NSDiffableDataSourceSnapshot<Int, NSManagedObjectID>() | ||
| snapshot.appendSections([0]) | ||
|
|
||
| let objectIDs = fetchedResultsController.fetchedObjects?.map { $0.objectID } ?? [] | ||
| snapshot.appendItems(objectIDs, toSection: 0) | ||
|
|
||
| dataSource.apply(snapshot, animatingDifferences: true) | ||
| } | ||
| } | ||
|
|
||
|
|
||
| extension AllQuotesViewController: UITableViewDelegate { | ||
| func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { | ||
| tableView.deselectRow(at: indexPath, animated: true) | ||
| } | ||
| } | ||
|
|
||
| extension AllQuotesViewController: @preconcurrency NSFetchedResultsControllerDelegate { | ||
| func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChangeContentWith snapshot: NSDiffableDataSourceSnapshotReference) { | ||
| let typedSnapshot = snapshot as NSDiffableDataSourceSnapshot<Int, NSManagedObjectID> | ||
| dataSource.apply(typedSnapshot, animatingDifferences: true) | ||
| } | ||
| } | ||
125 changes: 125 additions & 0 deletions
125
ONMIR/Feature/BookDetail/AllReadingLogsViewController.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| import UIKit | ||
| import CoreData | ||
| import SnapKit | ||
|
|
||
| #warning("TODO: UI") | ||
| final class AllReadingLogsViewController: UIViewController { | ||
| private let tableView = UITableView() | ||
| private let bookObjectID: NSManagedObjectID | ||
| private lazy var fetchedResultsController = makeFetchedResultsController() | ||
| private lazy var dataSource = makeDataSource() | ||
|
|
||
| private static let timeFormatter = { | ||
| let formatter = DateComponentsFormatter() | ||
| formatter.allowedUnits = [.hour, .minute] | ||
| formatter.unitsStyle = .abbreviated | ||
| return formatter | ||
| }() | ||
|
|
||
|
|
||
| init(bookObjectID: NSManagedObjectID) { | ||
| self.bookObjectID = bookObjectID | ||
| super.init(nibName: nil, bundle: nil) | ||
| } | ||
|
|
||
| required init?(coder: NSCoder) { | ||
| fatalError("init(coder:) has not been implemented") | ||
| } | ||
|
|
||
| override func viewDidLoad() { | ||
| super.viewDidLoad() | ||
| setupUI() | ||
|
|
||
| do { | ||
| try fetchedResultsController.performFetch() | ||
| updateSnapshot() | ||
| } catch { | ||
| print("Failed to fetch reading logs: \(error)") | ||
baekteun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| private func setupUI() { | ||
| title = "All Book Logs" | ||
| view.backgroundColor = .systemBackground | ||
|
|
||
| tableView.delegate = self | ||
| tableView.register(UITableViewCell.self, forCellReuseIdentifier: "ReadingLogCell") | ||
|
|
||
| view.addSubview(tableView) | ||
| tableView.snp.makeConstraints { make in | ||
| make.edges.equalToSuperview() | ||
| } | ||
| } | ||
|
|
||
| private func makeDataSource() -> UITableViewDiffableDataSource<Int, NSManagedObjectID> { | ||
| let dataSource = UITableViewDiffableDataSource<Int, NSManagedObjectID>(tableView: tableView) { [weak self] tableView, indexPath, objectID in | ||
| let cell = tableView.dequeueReusableCell(withIdentifier: "ReadingLogCell", for: indexPath) | ||
|
|
||
| guard let self else { return cell } | ||
|
|
||
| let context = self.fetchedResultsController.managedObjectContext | ||
| guard | ||
| let log = context.object(with: objectID) as? ReadingLogEntity | ||
| else { | ||
| return cell | ||
| } | ||
|
|
||
| cell.textLabel?.text = log.managedObjectContext?.performAndWait { "\(log.startPage) - \(log.endPage)" } ?? "" | ||
| cell.detailTextLabel?.text = log.managedObjectContext?.performAndWait { Self.timeFormatter.string(from: log.readingSeconds) } ?? "" | ||
| cell.accessoryType = .disclosureIndicator | ||
|
|
||
| return cell | ||
| } | ||
|
|
||
| tableView.dataSource = dataSource | ||
| return dataSource | ||
| } | ||
|
|
||
| private func makeFetchedResultsController() -> NSFetchedResultsController<ReadingLogEntity> { | ||
| let context = ContextManager.shared.mainContext | ||
|
|
||
| guard let book = context.object(with: bookObjectID) as? BookEntity else { | ||
| assertionFailure("Book not found") | ||
| self.dismiss(animated: true) | ||
| return .init() | ||
| } | ||
|
|
||
| let request: NSFetchRequest<ReadingLogEntity> = ReadingLogEntity.fetchRequest() | ||
| request.predicate = NSPredicate(format: "book == %@", book) | ||
| request.sortDescriptors = [NSSortDescriptor(keyPath: \ReadingLogEntity.startPage, ascending: false)] | ||
|
|
||
| let controller = NSFetchedResultsController( | ||
| fetchRequest: request, | ||
| managedObjectContext: context, | ||
| sectionNameKeyPath: nil, | ||
| cacheName: nil | ||
| ) | ||
|
|
||
| controller.delegate = self | ||
| return controller | ||
| } | ||
|
|
||
| private func updateSnapshot() { | ||
| var snapshot = NSDiffableDataSourceSnapshot<Int, NSManagedObjectID>() | ||
| snapshot.appendSections([0]) | ||
|
|
||
| let objectIDs = fetchedResultsController.fetchedObjects?.map { $0.objectID } ?? [] | ||
| snapshot.appendItems(objectIDs, toSection: 0) | ||
|
|
||
| dataSource.apply(snapshot, animatingDifferences: true) | ||
| } | ||
| } | ||
|
|
||
|
|
||
| extension AllReadingLogsViewController: UITableViewDelegate { | ||
| func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { | ||
| tableView.deselectRow(at: indexPath, animated: true) | ||
| } | ||
| } | ||
|
|
||
| extension AllReadingLogsViewController: @preconcurrency NSFetchedResultsControllerDelegate { | ||
| func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChangeContentWith snapshot: NSDiffableDataSourceSnapshotReference) { | ||
| let typedSnapshot = snapshot as NSDiffableDataSourceSnapshot<Int, NSManagedObjectID> | ||
| dataSource.apply(typedSnapshot, animatingDifferences: true) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.