Skip to content

Commit

Permalink
Refactor: Remove NeedsDependency
Browse files Browse the repository at this point in the history
The AppContext is already a singleton. SceneCoordinators are unique to UIWindowScenes, so fetch them that way.

Fixes iOS-324
  • Loading branch information
whattherestimefor committed Dec 3, 2024
1 parent fc7ebd6 commit 65bf555
Show file tree
Hide file tree
Showing 142 changed files with 401 additions and 757 deletions.
4 changes: 0 additions & 4 deletions Mastodon.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,6 @@
DB87D4452609BE0500D12C0D /* ComposeStatusPollOptionCollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB87D4442609BE0500D12C0D /* ComposeStatusPollOptionCollectionViewCell.swift */; };
DB87D4512609CF1E00D12C0D /* ComposeStatusPollOptionAppendEntryCollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB87D4502609CF1E00D12C0D /* ComposeStatusPollOptionAppendEntryCollectionViewCell.swift */; };
DB8AF54425C13647002E6C99 /* SceneCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB8AF54225C13647002E6C99 /* SceneCoordinator.swift */; };
DB8AF54525C13647002E6C99 /* NeedsDependency.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB8AF54325C13647002E6C99 /* NeedsDependency.swift */; };
DB8AF55025C13703002E6C99 /* MainTabBarController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB8AF54F25C13703002E6C99 /* MainTabBarController.swift */; };
DB8AF55D25C138B7002E6C99 /* UIViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB8AF55C25C138B7002E6C99 /* UIViewController.swift */; };
DB8F7076279E954700E1225B /* DataSourceFacade+Follow.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB8F7075279E954700E1225B /* DataSourceFacade+Follow.swift */; };
Expand Down Expand Up @@ -1100,7 +1099,6 @@
DB87D4502609CF1E00D12C0D /* ComposeStatusPollOptionAppendEntryCollectionViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ComposeStatusPollOptionAppendEntryCollectionViewCell.swift; sourceTree = "<group>"; };
DB89BA1025C10FF5008580ED /* Mastodon.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = Mastodon.entitlements; sourceTree = "<group>"; };
DB8AF54225C13647002E6C99 /* SceneCoordinator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SceneCoordinator.swift; sourceTree = "<group>"; };
DB8AF54325C13647002E6C99 /* NeedsDependency.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NeedsDependency.swift; sourceTree = "<group>"; };
DB8AF54F25C13703002E6C99 /* MainTabBarController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MainTabBarController.swift; sourceTree = "<group>"; };
DB8AF55C25C138B7002E6C99 /* UIViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIViewController.swift; sourceTree = "<group>"; };
DB8D8E3128196FA0009FD90F /* sv */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = sv; path = sv.lproj/Intents.strings; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2588,7 +2586,6 @@
isa = PBXGroup;
children = (
DB8AF54225C13647002E6C99 /* SceneCoordinator.swift */,
DB8AF54325C13647002E6C99 /* NeedsDependency.swift */,
D8F9170E2A4B47EF008A5370 /* Coordinator.swift */,
);
path = Coordinator;
Expand Down Expand Up @@ -3812,7 +3809,6 @@
2D364F7825E66D8300204FDC /* MastodonResendEmailViewModel.swift in Sources */,
DBEFCD7B282A162400C0ABEA /* ReportReasonView.swift in Sources */,
D8E5C346296DAB84007E76A7 /* DataSourceFacade+Status+History.swift in Sources */,
DB8AF54525C13647002E6C99 /* NeedsDependency.swift in Sources */,
DB63F77B279ACAE500455B82 /* DataSourceFacade+Favorite.swift in Sources */,
DB9D6BF825E4F5690051B173 /* NotificationViewController.swift in Sources */,
2DAC9E46262FC9FD0062E1A6 /* SuggestionAccountTableViewCell.swift in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion Mastodon/Activity/SafariActivity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class SafariActivity: UIActivity {
weak var sceneCoordinator: SceneCoordinator?
var url: NSURL?

init(sceneCoordinator: SceneCoordinator) {
init(sceneCoordinator: SceneCoordinator?) {
self.sceneCoordinator = sceneCoordinator
}

Expand Down
19 changes: 0 additions & 19 deletions Mastodon/Coordinator/NeedsDependency.swift

This file was deleted.

44 changes: 22 additions & 22 deletions Mastodon/Coordinator/SceneCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ import MBProgressHUD
@MainActor
final public class SceneCoordinator {

fileprivate static func coordinator(for view: UIView) -> SceneCoordinator? {
return SceneDelegate.delegate(for: view)?.coordinator
}

private var disposeBag = Set<AnyCancellable>()

private weak var scene: UIScene!
private weak var sceneDelegate: SceneDelegate!
private(set) weak var appContext: AppContext!

var authenticationBox: MastodonAuthenticationBox? {
AuthenticationServiceProvider.shared.currentActiveUser.value
Expand All @@ -45,7 +48,6 @@ final public class SceneCoordinator {
) {
self.scene = scene
self.sceneDelegate = sceneDelegate
self.appContext = appContext

NotificationService.shared.requestRevealNotificationPublisher
.receive(on: DispatchQueue.main)
Expand Down Expand Up @@ -118,7 +120,6 @@ final public class SceneCoordinator {
break
case .mention, .reblog, .favourite, .poll, .status:
let threadViewModel = RemoteThreadViewModel(
context: appContext,
authenticationBox: authenticationBox,
notificationID: notificationID
)
Expand Down Expand Up @@ -250,25 +251,26 @@ extension SceneCoordinator {

switch UIDevice.current.userInterfaceIdiom {
case .phone:
let viewController = MainTabBarController(context: appContext, coordinator: self, authenticationBox: authenticationBox)
let viewController = MainTabBarController(authenticationBox: authenticationBox)
self.splitViewController = nil
self.tabBarController = viewController
rootViewController = viewController
default:
let splitViewController = RootSplitViewController(context: appContext, coordinator: self, authenticationBox: authenticationBox)
let splitViewController = RootSplitViewController(authenticationBox: authenticationBox)
self.splitViewController = splitViewController
self.tabBarController = splitViewController.contentSplitViewController.mainTabBarController
rootViewController = splitViewController
}

// this feels wrong
sceneDelegate.window?.rootViewController = rootViewController // base: main
self.rootViewController = rootViewController

if authenticationBox == nil { // entry #1: welcome
DispatchQueue.main.async {
_ = self.present(
scene: .welcome,
from: self.sceneDelegate.window?.rootViewController,
from: rootViewController, // self.sceneDelegate.window?.rootViewController,
transition: .modal(animated: true, completion: nil)
)
}
Expand Down Expand Up @@ -422,13 +424,11 @@ private extension SceneCoordinator {
let _viewController = WebViewController(viewModel)
viewController = _viewController
case .searchDetail(let viewModel):
let _viewController = SearchDetailViewController(appContext: appContext, sceneCoordinator: self, authenticationBox: viewModel.authenticationBox)
let _viewController = SearchDetailViewController(authenticationBox: viewModel.authenticationBox)
_viewController.viewModel = viewModel
viewController = _viewController
case .searchResult(let viewModel):
let searchResultViewController = SearchResultViewController()
searchResultViewController.context = appContext
searchResultViewController.coordinator = self
searchResultViewController.viewModel = viewModel
viewController = searchResultViewController
case .compose(let viewModel):
Expand Down Expand Up @@ -460,19 +460,19 @@ private extension SceneCoordinator {
case .followedTags(let viewModel):
guard let authenticationBox else { return nil }

viewController = FollowedTagsViewController(appContext: appContext, sceneCoordinator: self, authenticationBox: authenticationBox, viewModel: viewModel)
viewController = FollowedTagsViewController(authenticationBox: authenticationBox, viewModel: viewModel)
case .favorite(let viewModel):
let _viewController = FavoriteViewController()
_viewController.viewModel = viewModel
viewController = _viewController
case .follower(let viewModel):
let followerListViewController = FollowerListViewController(viewModel: viewModel, coordinator: self, context: appContext)
let followerListViewController = FollowerListViewController(viewModel: viewModel)
viewController = followerListViewController
case .following(let viewModel):
let followingListViewController = FollowingListViewController(viewModel: viewModel, coordinator: self, context: appContext)
let followingListViewController = FollowingListViewController(viewModel: viewModel)
viewController = followingListViewController
case .familiarFollowers(let viewModel):
viewController = FamiliarFollowersViewController(viewModel: viewModel, context: appContext, coordinator: self)
viewController = FamiliarFollowersViewController(viewModel: viewModel)
case .rebloggedBy(let viewModel):
let _viewController = RebloggedByViewController()
_viewController.viewModel = viewModel
Expand Down Expand Up @@ -539,7 +539,7 @@ private extension SceneCoordinator {
let settingsCoordinator = SettingsCoordinator(presentedOn: presentedOn,
accountName: accountName,
setting: setting,
appContext: appContext,
appContext: AppContext.shared,
authenticationBox: authenticationBox,
sceneCoordinator: self
)
Expand All @@ -557,18 +557,11 @@ private extension SceneCoordinator {
case .notificationPolicy(let viewModel):
viewController = NotificationPolicyViewController(viewModel: viewModel)
case .accountNotificationTimeline(let viewModel, let request):
viewController = AccountNotificationTimelineViewController(viewModel: viewModel, context: appContext, coordinator: self, notificationRequest: request)
viewController = AccountNotificationTimelineViewController(viewModel: viewModel, notificationRequest: request)
}

setupDependency(for: viewController as? NeedsDependency)

return viewController
}

private func setupDependency(for needs: NeedsDependency?) {
needs?.context = appContext
needs?.coordinator = self
}
}

//MARK: - Loading
Expand Down Expand Up @@ -692,3 +685,10 @@ extension SceneCoordinator: SettingsCoordinatorDelegate {
self.mastodonAuthenticationController = authenticationController
}
}

public extension UIViewController {
var sceneCoordinator: SceneCoordinator? {
guard let view = viewIfLoaded else { assert(false); return nil }
return SceneCoordinator.coordinator(for: view)
}
}
4 changes: 0 additions & 4 deletions Mastodon/Diffable/Report/ReportSection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ extension ReportSection {
case .status(let status):
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: ReportStatusTableViewCell.self), for: indexPath) as! ReportStatusTableViewCell
configure(
context: context,
tableView: tableView,
cell: cell,
viewModel: .init(value: status),
Expand Down Expand Up @@ -83,19 +82,16 @@ extension ReportSection {
extension ReportSection {

static func configure(
context: AppContext,
tableView: UITableView,
cell: ReportStatusTableViewCell,
viewModel: ReportStatusTableViewCell.ViewModel,
configuration: Configuration
) {
StatusSection.setupStatusPollDataSource(
context: context,
authenticationBox: configuration.authenticationBox,
statusView: cell.statusView
)

cell.statusView.viewModel.context = context
cell.statusView.viewModel.authenticationBox = configuration.authenticationBox

cell.configure(
Expand Down
18 changes: 1 addition & 17 deletions Mastodon/Diffable/Status/StatusSection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ enum StatusSection: Equatable, Hashable {
extension StatusSection {

struct Configuration {
let context: AppContext
let authenticationBox: MastodonAuthenticationBox
weak var statusTableViewCellDelegate: StatusTableViewCellDelegate?
weak var timelineMiddleLoaderTableViewCellDelegate: TimelineMiddleLoaderTableViewCellDelegate?
Expand All @@ -33,7 +32,6 @@ extension StatusSection {

static func diffableDataSource(
tableView: UITableView,
context: AppContext,
configuration: Configuration
) -> UITableViewDiffableDataSource<StatusSection, StatusItem> {
tableView.register(StatusTableViewCell.self, forCellReuseIdentifier: String(describing: StatusTableViewCell.self))
Expand All @@ -48,7 +46,6 @@ extension StatusSection {
let displayItem = StatusTableViewCell.StatusTableViewCellViewModel.DisplayItem.feed(feed)
let contentConcealModel = StatusView.ContentConcealViewModel(status: feed.status, filterBox: StatusFilterService.shared.activeFilterBox, filterContext: configuration.filterContext)
configure(
context: context,
tableView: tableView,
cell: cell,
viewModel: StatusTableViewCell.StatusTableViewCellViewModel(displayItem: displayItem, contentConcealModel: contentConcealModel),
Expand All @@ -68,7 +65,6 @@ extension StatusSection {
let displayItem = StatusTableViewCell.StatusTableViewCellViewModel.DisplayItem.status(status)
let contentConcealModel = StatusView.ContentConcealViewModel(status: status, filterBox: StatusFilterService.shared.activeFilterBox, filterContext: configuration.filterContext)
configure(
context: context,
tableView: tableView,
cell: cell,
viewModel: StatusTableViewCell.StatusTableViewCellViewModel(displayItem: displayItem, contentConcealModel: contentConcealModel),
Expand All @@ -77,7 +73,6 @@ extension StatusSection {
return cell
case .thread(let thread):
let cell = dequeueConfiguredReusableCell(
context: context,
tableView: tableView,
indexPath: indexPath,
configuration: ThreadCellRegistrationConfiguration(
Expand Down Expand Up @@ -108,7 +103,6 @@ extension StatusSection {
}

static func dequeueConfiguredReusableCell(
context: AppContext,
tableView: UITableView,
indexPath: IndexPath,
configuration: ThreadCellRegistrationConfiguration
Expand All @@ -118,7 +112,6 @@ extension StatusSection {
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: StatusThreadRootTableViewCell.self), for: indexPath) as! StatusThreadRootTableViewCell
let contentConcealModel = StatusView.ContentConcealViewModel(status: threadContext.status, filterBox: StatusFilterService.shared.activeFilterBox, filterContext: .thread)
StatusSection.configure(
context: context,
tableView: tableView,
cell: cell,
viewModel: StatusTableViewCell.StatusTableViewCellViewModel(displayItem: .status(threadContext.status), contentConcealModel: contentConcealModel),
Expand All @@ -132,9 +125,7 @@ extension StatusSection {
let contentConcealModel = StatusView.ContentConcealViewModel(status: threadContext.status, filterBox: StatusFilterService.shared.activeFilterBox, filterContext: configuration.configuration.filterContext)
assert(configuration.configuration.filterContext == .thread)
StatusSection.configure(
context: context,
tableView: tableView,
cell: cell,
tableView: tableView, cell: cell,
viewModel: StatusTableViewCell.StatusTableViewCellViewModel(displayItem: displayItem, contentConcealModel: contentConcealModel),
configuration: configuration.configuration
)
Expand All @@ -147,7 +138,6 @@ extension StatusSection {
extension StatusSection {

public static func setupStatusPollDataSource(
context: AppContext,
authenticationBox: MastodonAuthenticationBox,
statusView: StatusView
) {
Expand Down Expand Up @@ -206,19 +196,16 @@ extension StatusSection {
extension StatusSection {

static func configure(
context: AppContext,
tableView: UITableView,
cell: StatusTableViewCell,
viewModel: StatusTableViewCell.StatusTableViewCellViewModel,
configuration: Configuration
) {
setupStatusPollDataSource(
context: context,
authenticationBox: configuration.authenticationBox,
statusView: cell.statusView
)

cell.statusView.viewModel.context = configuration.context
cell.statusView.viewModel.authenticationBox = configuration.authenticationBox

cell.configure(
Expand All @@ -229,19 +216,16 @@ extension StatusSection {
}

static func configure(
context: AppContext,
tableView: UITableView,
cell: StatusThreadRootTableViewCell,
viewModel: StatusTableViewCell.StatusTableViewCellViewModel,
configuration: Configuration
) {
setupStatusPollDataSource(
context: context,
authenticationBox: configuration.authenticationBox,
statusView: cell.statusView
)

cell.statusView.viewModel.context = configuration.context
cell.statusView.viewModel.authenticationBox = configuration.authenticationBox

cell.configure(
Expand Down
1 change: 0 additions & 1 deletion Mastodon/Diffable/User/UserSection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ enum UserSection: Hashable {
extension UserSection {
static func diffableDataSource(
tableView: UITableView,
context: AppContext,
authenticationBox: MastodonAuthenticationBox,
userTableViewCellDelegate: UserTableViewCellDelegate?
) -> UITableViewDiffableDataSource<UserSection, UserItem> {
Expand Down
4 changes: 2 additions & 2 deletions Mastodon/Protocol/Provider/DataSourceFacade+Block.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import MastodonSDK

extension DataSourceFacade {
static func responseToUserBlockAction(
dependency: NeedsDependency & AuthContextProvider,
dependency: AuthContextProvider,
account: Mastodon.Entity.Account
) async throws -> Mastodon.Entity.Relationship {
FeedbackGenerator.shared.generate(.selectionChanged)
Expand All @@ -35,7 +35,7 @@ extension DataSourceFacade {
}

static func responseToDomainBlockAction(
dependency: NeedsDependency & AuthContextProvider,
dependency: AuthContextProvider,
account: Mastodon.Entity.Account
) async throws -> Mastodon.Entity.Empty {
FeedbackGenerator.shared.generate(.selectionChanged)
Expand Down
2 changes: 1 addition & 1 deletion Mastodon/Protocol/Provider/DataSourceFacade+Bookmark.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import MastodonSDK
extension DataSourceFacade {
@MainActor
public static func responseToStatusBookmarkAction(
provider: NeedsDependency & AuthContextProvider & DataSourceProvider,
provider: AuthContextProvider & DataSourceProvider,
status: MastodonStatus
) async throws {
FeedbackGenerator.shared.generate(.selectionChanged)
Expand Down
Loading

0 comments on commit 65bf555

Please sign in to comment.