Skip to content

Commit

Permalink
Make Profile-screen use Mastodon.Entity.Account instead of MastodonUs…
Browse files Browse the repository at this point in the history
…er (#1192)

- Make Profile-screen use Mastodon.Entity.Account instead of
MastodonUser
- Fix things left and right
- Just like with the Status-refactoring I'm expecting more bugs to show
up over time.
  • Loading branch information
zeitschlag authored Feb 21, 2024
2 parents a5f74b8 + 3cf397d commit db12ea4
Show file tree
Hide file tree
Showing 173 changed files with 2,378 additions and 6,338 deletions.
62 changes: 17 additions & 45 deletions Mastodon.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

121 changes: 71 additions & 50 deletions Mastodon/Coordinator/SceneCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ final public class SceneCoordinator {
self.appContext = appContext

scene.session.sceneCoordinator = self

appContext.notificationService.requestRevealNotificationPublisher
.receive(on: DispatchQueue.main)
.sink(receiveValue: { [weak self] pushNotification in
guard let self = self else { return }
Task {
.sink(receiveValue: {
[weak self] pushNotification in
guard let self else { return }
Task { @MainActor in
guard let currentActiveAuthenticationBox = self.authContext?.mastodonAuthenticationBox else { return }
let accessToken = pushNotification.accessToken // use raw accessToken value without normalize
if currentActiveAuthenticationBox.userAuthorization.accessToken == accessToken {
Expand All @@ -67,54 +68,76 @@ final public class SceneCoordinator {
let userID = authentication.userID
let isSuccess = try await appContext.authenticationService.activeMastodonUser(domain: domain, userID: userID)
guard isSuccess else { return }

self.setup()
try await Task.sleep(nanoseconds: .second * 1)

// redirect to notifications tab
self.switchToTabBar(tab: .notifications)

// Delay in next run loop
DispatchQueue.main.async { [weak self] in
guard let self = self else { return }

// Note:
// show (push) on phone and pad
let from: UIViewController? = {
if let splitViewController = self.splitViewController {
if splitViewController.compactMainTabBarViewController.topMost?.view.window != nil {
// compact
return splitViewController.compactMainTabBarViewController.topMost
} else {
// expand
return splitViewController.contentSplitViewController.mainTabBarController.topMost
}

// Note:
// show (push) on phone and pad
let from: UIViewController? = {
if let splitViewController = self.splitViewController {
if splitViewController.compactMainTabBarViewController.topMost?.view.window != nil {
// compact
return splitViewController.compactMainTabBarViewController.topMost
} else {
return self.tabBarController.topMost
// expand
return splitViewController.contentSplitViewController.mainTabBarController.topMost
}
}()

// show notification related content
guard let type = Mastodon.Entity.Notification.NotificationType(rawValue: pushNotification.notificationType) else { return }
guard let authContext = self.authContext else { return }
let notificationID = String(pushNotification.notificationID)

switch type {
case .follow:
let profileViewModel = RemoteProfileViewModel(context: appContext, authContext: authContext, notificationID: notificationID)
_ = self.present(scene: .profile(viewModel: profileViewModel), from: from, transition: .show)
case .followRequest:
// do nothing
break
case .mention, .reblog, .favourite, .poll, .status:
let threadViewModel = RemoteThreadViewModel(context: appContext, authContext: authContext, notificationID: notificationID)
_ = self.present(scene: .thread(viewModel: threadViewModel), from: from, transition: .show)
case ._other:
assertionFailure()
break
} else {
return self.tabBarController.topMost
}
} // end DispatchQueue.main.async

}()

// show notification related content
guard let type = Mastodon.Entity.Notification.NotificationType(rawValue: pushNotification.notificationType) else { return }
guard let authContext = self.authContext else { return }
guard let me = authContext.mastodonAuthenticationBox.authentication.account() else { return }
let notificationID = String(pushNotification.notificationID)

switch type {
case .follow:
let account = try await appContext.apiService.notification(
notificationID: notificationID,
authenticationBox: authContext.mastodonAuthenticationBox
).value.account

let relationship = try await appContext.apiService.relationship(forAccounts: [account], authenticationBox: authContext.mastodonAuthenticationBox).value.first

let profileViewModel = ProfileViewModel(
context: appContext,
authContext: authContext,
account: account,
relationship: relationship,
me: me
)
_ = self.present(
scene: .profile(viewModel: profileViewModel),
from: from,
transition: .show
)
case .followRequest:
// do nothing
break
case .mention, .reblog, .favourite, .poll, .status:
let threadViewModel = RemoteThreadViewModel(
context: appContext,
authContext: authContext,
notificationID: notificationID
)
_ = self.present(
scene: .thread(viewModel: threadViewModel),
from: from,
transition: .show
)

case ._other:
assertionFailure()
break
}

} catch {
assertionFailure(error.localizedDescription)
return
Expand All @@ -140,7 +163,7 @@ extension SceneCoordinator {
case activityViewControllerPresent(animated: Bool, completion: (() -> Void)? = nil)
case none
}

enum Scene {
// onboarding
case welcome
Expand Down Expand Up @@ -357,7 +380,7 @@ extension SceneCoordinator {
return viewController
}

func switchToTabBar(tab: MainTabBarController.Tab) {
func switchToTabBar(tab: Tab) {
splitViewController?.contentSplitViewController.currentSupplementaryTab = tab

splitViewController?.compactMainTabBarViewController.selectedIndex = tab.rawValue
Expand Down Expand Up @@ -472,9 +495,7 @@ private extension SceneCoordinator {
_viewController.viewModel = viewModel
viewController = _viewController
case .report(let viewModel):
let _viewController = ReportViewController()
_viewController.viewModel = viewModel
viewController = _viewController
viewController = ReportViewController(viewModel: viewModel)
case .reportServerRules(let viewModel):
let _viewController = ReportServerRulesViewController()
_viewController.viewModel = viewModel
Expand Down
6 changes: 2 additions & 4 deletions Mastodon/Diffable/Notification/NotificationSection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,6 @@ extension NotificationSection {
viewModel: NotificationTableViewCell.ViewModel,
configuration: Configuration
) {
cell.notificationView.viewModel.context = context
cell.notificationView.viewModel.authContext = configuration.authContext

StatusSection.setupStatusPollDataSource(
context: context,
authContext: configuration.authContext,
Expand All @@ -91,7 +88,8 @@ extension NotificationSection {
cell.configure(
tableView: tableView,
viewModel: viewModel,
delegate: configuration.notificationTableViewCellDelegate
delegate: configuration.notificationTableViewCellDelegate,
authenticationBox: configuration.authContext.mastodonAuthenticationBox
)

cell.notificationView.statusView.viewModel.filterContext = configuration.filterContext
Expand Down
1 change: 0 additions & 1 deletion Mastodon/Diffable/Report/ReportItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ enum ReportItem: Hashable {
case header(context: HeaderContext)
case status(record: MastodonStatus)
case comment(context: CommentContext)
case result(record: ManagedObjectRecord<MastodonUser>)
case bottomLoader
}

Expand Down
8 changes: 0 additions & 8 deletions Mastodon/Diffable/Report/ReportSection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ extension ReportSection {
tableView.register(ReportHeadlineTableViewCell.self, forCellReuseIdentifier: String(describing: ReportHeadlineTableViewCell.self))
tableView.register(ReportStatusTableViewCell.self, forCellReuseIdentifier: String(describing: ReportStatusTableViewCell.self))
tableView.register(ReportCommentTableViewCell.self, forCellReuseIdentifier: String(describing: ReportCommentTableViewCell.self))
tableView.register(ReportResultActionTableViewCell.self, forCellReuseIdentifier: String(describing: ReportResultActionTableViewCell.self))
tableView.register(TimelineBottomLoaderTableViewCell.self, forCellReuseIdentifier: String(describing: TimelineBottomLoaderTableViewCell.self))

return UITableViewDiffableDataSource(tableView: tableView) { tableView, indexPath, item -> UITableViewCell? in
Expand Down Expand Up @@ -72,13 +71,6 @@ extension ReportSection {
}
.store(in: &cell.disposeBag)
return cell
case .result(let record):
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: ReportResultActionTableViewCell.self), for: indexPath) as! ReportResultActionTableViewCell
context.managedObjectContext.performAndWait {
guard let user = record.object(in: context.managedObjectContext) else { return }
cell.avatarImageView.configure(configuration: .init(url: user.avatarImageURL()))
}
return cell
case .bottomLoader:
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: TimelineBottomLoaderTableViewCell.self), for: indexPath) as! TimelineBottomLoaderTableViewCell
cell.activityIndicatorView.startAnimating()
Expand Down
2 changes: 1 addition & 1 deletion Mastodon/Diffable/User/UserSection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ extension UserSection {
case .account(let account, let relationship):
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: UserTableViewCell.self), for: indexPath) as! UserTableViewCell

guard let me = authContext.mastodonAuthenticationBox.authentication.user(in: context.managedObjectContext) else { return cell }
guard let me = authContext.mastodonAuthenticationBox.authentication.account() else { return cell }

cell.userView.setButtonState(.loading)
cell.configure(
Expand Down
2 changes: 0 additions & 2 deletions Mastodon/Extension/AppContext+NextAccount.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
// Created by Marcus Kida on 17.11.22.
//

import CoreData
import CoreDataStack
import MastodonCore
import MastodonSDK

Expand Down
25 changes: 0 additions & 25 deletions Mastodon/Persistence/Model/SearchHistory.swift

This file was deleted.

46 changes: 15 additions & 31 deletions Mastodon/Protocol/Provider/DataSourceFacade+Block.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,56 +13,40 @@ import MastodonSDK
extension DataSourceFacade {
static func responseToUserBlockAction(
dependency: NeedsDependency & AuthContextProvider,
user: ManagedObjectRecord<MastodonUser>
) async throws {
account: Mastodon.Entity.Account
) async throws -> Mastodon.Entity.Relationship {
let selectionFeedbackGenerator = await UISelectionFeedbackGenerator()
await selectionFeedbackGenerator.selectionChanged()

let apiService = dependency.context.apiService
let authBox = dependency.authContext.mastodonAuthenticationBox

_ = try await apiService.toggleBlock(
user: user,
authenticationBox: authBox
)

try await dependency.context.apiService.getBlocked(

let response = try await apiService.toggleBlock(
account: account,
authenticationBox: authBox
)
dependency.context.authenticationService.fetchFollowingAndBlockedAsync()
}

static func responseToUserBlockAction(
dependency: NeedsDependency & AuthContextProvider,
user: Mastodon.Entity.Account
) async throws {
let selectionFeedbackGenerator = await UISelectionFeedbackGenerator()
await selectionFeedbackGenerator.selectionChanged()

let apiService = dependency.context.apiService
let authBox = dependency.authContext.mastodonAuthenticationBox
let userInfo = [
UserInfoKey.relationship: response.value,
]

_ = try await apiService.toggleBlock(
user: user,
authenticationBox: authBox
)
NotificationCenter.default.post(name: .relationshipChanged, object: self, userInfo: userInfo)

try await dependency.context.apiService.getBlocked(
authenticationBox: authBox
)
dependency.context.authenticationService.fetchFollowingAndBlockedAsync()
return response.value
}

static func responseToDomainBlockAction(
dependency: NeedsDependency & AuthContextProvider,
user: ManagedObjectRecord<MastodonUser>
) async throws {
account: Mastodon.Entity.Account
) async throws -> Mastodon.Entity.Empty {
let selectionFeedbackGenerator = await UISelectionFeedbackGenerator()
await selectionFeedbackGenerator.selectionChanged()

let apiService = dependency.context.apiService
let authBox = dependency.authContext.mastodonAuthenticationBox

_ = try await apiService.toggleDomainBlock(user: user, authenticationBox: authBox)
let response = try await apiService.toggleDomainBlock(account: account, authenticationBox: authBox)

return response.value
}
}
Loading

0 comments on commit db12ea4

Please sign in to comment.