From e4578686e3517e00f7fd1f37ff0c8f27888971e0 Mon Sep 17 00:00:00 2001 From: Nathan Mattes Date: Mon, 22 May 2023 11:41:42 +0200 Subject: [PATCH] Follow all suggested user accounts (IOS-157) --- Mastodon.xcodeproj/project.pbxproj | 16 +------ .../RecommendAccountItem.swift | 0 .../RecommendAccountSection.swift | 0 .../SuggestionAccountViewController.swift | 44 +++++-------------- .../SuggestionAccountViewModel.swift | 16 +++++++ ...estionAccountTableViewCell+ViewModel.swift | 37 ---------------- .../SuggestionAccountTableViewCell.swift | 23 +++++++--- 7 files changed, 47 insertions(+), 89 deletions(-) rename Mastodon/{Diffable/RecommandAccount => Scene/SuggestionAccount}/RecommendAccountItem.swift (100%) rename Mastodon/{Diffable/RecommandAccount => Scene/SuggestionAccount}/RecommendAccountSection.swift (100%) delete mode 100644 Mastodon/Scene/SuggestionAccount/TableView-Components/SuggestionAccountTableViewCell+ViewModel.swift diff --git a/Mastodon.xcodeproj/project.pbxproj b/Mastodon.xcodeproj/project.pbxproj index 16e743e08..3e480ec54 100644 --- a/Mastodon.xcodeproj/project.pbxproj +++ b/Mastodon.xcodeproj/project.pbxproj @@ -439,7 +439,6 @@ DBCC3B30261440A50045B23D /* UITabBarController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBCC3B2F261440A50045B23D /* UITabBarController.swift */; }; DBCC3B8F26148F7B0045B23D /* CachedProfileViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBCC3B8E26148F7B0045B23D /* CachedProfileViewModel.swift */; }; DBD376B2269302A4007FEC24 /* UITableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBD376B1269302A4007FEC24 /* UITableViewCell.swift */; }; - DBD5B1F627BCD3D200BD6B38 /* SuggestionAccountTableViewCell+ViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBD5B1F527BCD3D200BD6B38 /* SuggestionAccountTableViewCell+ViewModel.swift */; }; DBD5B1F827BCFD9D00BD6B38 /* DataSourceProvider+TableViewControllerNavigateable.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBD5B1F727BCFD9D00BD6B38 /* DataSourceProvider+TableViewControllerNavigateable.swift */; }; DBD5B1FA27BD013700BD6B38 /* DataSourceProvider+StatusTableViewControllerNavigateable.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBD5B1F927BD013700BD6B38 /* DataSourceProvider+StatusTableViewControllerNavigateable.swift */; }; DBDFF1902805543100557A48 /* DiscoveryPostsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBDFF18F2805543100557A48 /* DiscoveryPostsViewController.swift */; }; @@ -1164,7 +1163,6 @@ DBCC3B2F261440A50045B23D /* UITabBarController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UITabBarController.swift; sourceTree = ""; }; DBCC3B8E26148F7B0045B23D /* CachedProfileViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CachedProfileViewModel.swift; sourceTree = ""; }; DBD376B1269302A4007FEC24 /* UITableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UITableViewCell.swift; sourceTree = ""; }; - DBD5B1F527BCD3D200BD6B38 /* SuggestionAccountTableViewCell+ViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "SuggestionAccountTableViewCell+ViewModel.swift"; sourceTree = ""; }; DBD5B1F727BCFD9D00BD6B38 /* DataSourceProvider+TableViewControllerNavigateable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "DataSourceProvider+TableViewControllerNavigateable.swift"; sourceTree = ""; }; DBD5B1F927BD013700BD6B38 /* DataSourceProvider+StatusTableViewControllerNavigateable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "DataSourceProvider+StatusTableViewControllerNavigateable.swift"; sourceTree = ""; }; DBDFF18F2805543100557A48 /* DiscoveryPostsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DiscoveryPostsViewController.swift; sourceTree = ""; }; @@ -1615,7 +1613,6 @@ DB4F097826A039B400D62E92 /* Onboarding */, DB0617F827855B170030EE79 /* User */, DB0617F927855B460030EE79 /* Profile */, - DB0FCB892796BE1E006C02E2 /* RecommandAccount */, DB4F097926A039C400D62E92 /* Status */, DB65C63527A2AF52008BAC2E /* Report */, DB0617F727855B010030EE79 /* Notification */, @@ -1670,6 +1667,8 @@ 2DAC9E36262FC20B0062E1A6 /* SuggestionAccount */ = { isa = PBXGroup; children = ( + 2DE0FACD2615F7AD00CDF649 /* RecommendAccountSection.swift */, + DBB45B5F27B50A4F002DC5A7 /* RecommendAccountItem.swift */, 2DAC9E37262FC2320062E1A6 /* SuggestionAccountViewController.swift */, 2DAC9E3D262FC2400062E1A6 /* SuggestionAccountViewModel.swift */, DBB45B6127B51112002DC5A7 /* SuggestionAccountViewModel+Diffable.swift */, @@ -1682,7 +1681,6 @@ isa = PBXGroup; children = ( 2DAC9E45262FC9FD0062E1A6 /* SuggestionAccountTableViewCell.swift */, - DBD5B1F527BCD3D200BD6B38 /* SuggestionAccountTableViewCell+ViewModel.swift */, D8BE30B22A179E26006B8270 /* SuggestionAccountTableViewFooter.swift */, ); path = "TableView-Components"; @@ -1921,15 +1919,6 @@ path = View; sourceTree = ""; }; - DB0FCB892796BE1E006C02E2 /* RecommandAccount */ = { - isa = PBXGroup; - children = ( - 2DE0FACD2615F7AD00CDF649 /* RecommendAccountSection.swift */, - DBB45B5F27B50A4F002DC5A7 /* RecommendAccountItem.swift */, - ); - path = RecommandAccount; - sourceTree = ""; - }; DB1D187125EF5BBD003F1F23 /* TableView */ = { isa = PBXGroup; children = ( @@ -3597,7 +3586,6 @@ DB938F0F2624119800E5B6C1 /* ThreadViewModel+LoadThreadState.swift in Sources */, DB6180F226391CF40018D199 /* MediaPreviewImageViewModel.swift in Sources */, 62FD27D12893707600B205C5 /* BookmarkViewController.swift in Sources */, - DBD5B1F627BCD3D200BD6B38 /* SuggestionAccountTableViewCell+ViewModel.swift in Sources */, DB63F767279A5EB300455B82 /* NotificationTimelineViewModel.swift in Sources */, 2D607AD826242FC500B70763 /* NotificationViewModel.swift in Sources */, DB5B54AB2833C12A00DEF8B2 /* RebloggedByViewController.swift in Sources */, diff --git a/Mastodon/Diffable/RecommandAccount/RecommendAccountItem.swift b/Mastodon/Scene/SuggestionAccount/RecommendAccountItem.swift similarity index 100% rename from Mastodon/Diffable/RecommandAccount/RecommendAccountItem.swift rename to Mastodon/Scene/SuggestionAccount/RecommendAccountItem.swift diff --git a/Mastodon/Diffable/RecommandAccount/RecommendAccountSection.swift b/Mastodon/Scene/SuggestionAccount/RecommendAccountSection.swift similarity index 100% rename from Mastodon/Diffable/RecommandAccount/RecommendAccountSection.swift rename to Mastodon/Scene/SuggestionAccount/RecommendAccountSection.swift diff --git a/Mastodon/Scene/SuggestionAccount/SuggestionAccountViewController.swift b/Mastodon/Scene/SuggestionAccount/SuggestionAccountViewController.swift index 43a22f21d..bd3df4fac 100644 --- a/Mastodon/Scene/SuggestionAccount/SuggestionAccountViewController.swift +++ b/Mastodon/Scene/SuggestionAccount/SuggestionAccountViewController.swift @@ -72,6 +72,12 @@ class SuggestionAccountViewController: UIViewController, NeedsDependency { tableView.deselectRow(with: transitionCoordinator, animated: animated) } + + //MARK: - Actions + + @objc func doneButtonDidClick(_ sender: UIButton) { + dismiss(animated: true, completion: nil) + } } // MARK: - UITableViewDelegate @@ -106,42 +112,16 @@ extension SuggestionAccountViewController: AuthContextProvider { var authContext: AuthContext { viewModel.authContext } } -// MARK: - SuggestionAccountTableViewCellDelegate -extension SuggestionAccountViewController: SuggestionAccountTableViewCellDelegate { - func suggestionAccountTableViewCell( - _ cell: SuggestionAccountTableViewCell, - friendshipDidPressed button: UIButton - ) { - guard let tableViewDiffableDataSource = viewModel.tableViewDiffableDataSource else { return } - guard let indexPath = tableView.indexPath(for: cell) else { return } - guard let item = tableViewDiffableDataSource.itemIdentifier(for: indexPath) else { return } - - switch item { - case .account(let user): - Task { @MainActor in - do { - try await DataSourceFacade.responseToUserFollowAction( - dependency: self, - user: user - ) - } catch { - // do noting - } - } - } - } -} +// MARK: - UserTableViewCellDelegate +extension SuggestionAccountViewController: UserTableViewCellDelegate {} + +// MARK: - SuggestionAccountTableViewCellDelegate +extension SuggestionAccountViewController: SuggestionAccountTableViewCellDelegate { } -extension SuggestionAccountViewController { - @objc func doneButtonDidClick(_ sender: UIButton) { - dismiss(animated: true, completion: nil) - } -} extension SuggestionAccountViewController: SuggestionAccountTableViewFooterDelegate { func followAll(_ footerView: SuggestionAccountTableViewFooter) { - // get all five suggested accounts aka user - // follow all of them + viewModel.followAllSuggestedAccounts(self) } } diff --git a/Mastodon/Scene/SuggestionAccount/SuggestionAccountViewModel.swift b/Mastodon/Scene/SuggestionAccount/SuggestionAccountViewModel.swift index 42f18ab4c..59bf7f55c 100644 --- a/Mastodon/Scene/SuggestionAccount/SuggestionAccountViewModel.swift +++ b/Mastodon/Scene/SuggestionAccount/SuggestionAccountViewModel.swift @@ -85,4 +85,20 @@ final class SuggestionAccountViewModel: NSObject { .store(in: &disposeBag) } + func followAllSuggestedAccounts(_ dependency: NeedsDependency & AuthContextProvider ) { + + let userRecords = userFetchedResultsController.records.compactMap { + $0.object(in: dependency.context.managedObjectContext)?.asRecord + } + + userRecords.forEach { user in + Task { + try? await DataSourceFacade.responseToUserViewButtonAction( + dependency: dependency, + user: user, + buttonState: .follow + ) + } + } + } } diff --git a/Mastodon/Scene/SuggestionAccount/TableView-Components/SuggestionAccountTableViewCell+ViewModel.swift b/Mastodon/Scene/SuggestionAccount/TableView-Components/SuggestionAccountTableViewCell+ViewModel.swift deleted file mode 100644 index 05ed2c13d..000000000 --- a/Mastodon/Scene/SuggestionAccount/TableView-Components/SuggestionAccountTableViewCell+ViewModel.swift +++ /dev/null @@ -1,37 +0,0 @@ -// -// SuggestionAccountTableViewCell+ViewModel.swift -// Mastodon -// -// Created by MainasuK on 2022-2-16. -// - -import UIKit -import Combine -import CoreDataStack -import MastodonAsset -import MastodonCore -import MastodonUI -import MastodonMeta -import Meta - -extension SuggestionAccountTableViewCell { - - func configure(user: MastodonUser) { - //TODO: Set Delegate - userView.configure(user: user, delegate: nil) - //TODO: Fix Button State - userView.setButtonState(.follow) - - let metaContent: MetaContent = { - do { - let mastodonContent = MastodonContent(content: user.note ?? "", emojis: [:]) - return try MastodonMetaContent.convert(document: mastodonContent) - } catch { - assertionFailure() - return PlaintextMetaContent(string: user.note ?? "") - } - } () - - bioMetaLabel.configure(content: metaContent) - } -} diff --git a/Mastodon/Scene/SuggestionAccount/TableView-Components/SuggestionAccountTableViewCell.swift b/Mastodon/Scene/SuggestionAccount/TableView-Components/SuggestionAccountTableViewCell.swift index 3e07bff6c..f05f87a10 100644 --- a/Mastodon/Scene/SuggestionAccount/TableView-Components/SuggestionAccountTableViewCell.swift +++ b/Mastodon/Scene/SuggestionAccount/TableView-Components/SuggestionAccountTableViewCell.swift @@ -16,10 +16,9 @@ import MastodonMeta import MastodonAsset import MastodonLocalization import MastodonUI +import MastodonCore -protocol SuggestionAccountTableViewCellDelegate: AnyObject { - func suggestionAccountTableViewCell(_ cell: SuggestionAccountTableViewCell, friendshipDidPressed button: UIButton) -} +protocol SuggestionAccountTableViewCellDelegate: AnyObject, UserViewDelegate {} final class SuggestionAccountTableViewCell: UITableViewCell { @@ -80,9 +79,21 @@ final class SuggestionAccountTableViewCell: UITableViewCell { disposeBag.removeAll() } - //MARK: - Action + func configure(user: MastodonUser) { + userView.configure(user: user, delegate: delegate) + //TODO: Fix Button State + userView.setButtonState(.follow) - @objc private func buttonDidPressed(_ sender: UIButton) { - delegate?.suggestionAccountTableViewCell(self, friendshipDidPressed: sender) + let metaContent: MetaContent = { + do { + let mastodonContent = MastodonContent(content: user.note ?? "", emojis: [:]) + return try MastodonMetaContent.convert(document: mastodonContent) + } catch { + assertionFailure() + return PlaintextMetaContent(string: user.note ?? "") + } + } () + + bioMetaLabel.configure(content: metaContent) } }