Skip to content

Commit

Permalink
Some defense against ProfileViewController.authContext.getter Forced …
Browse files Browse the repository at this point in the history
…Unwrapping Crash

Make the viewModel of the ProfileViewController optional rather than force unwrapping it. If the necessary information is not available, Profile page should show blank rather than crashing, and hopefully will have the expected info soon or the next time it is opened.

There is still a crash danger inherent in the use of TabBarPager, which requires a non-optional for several protocol methods were we can’t guarantee to have one to return. This dependency should be removed in the future.
  • Loading branch information
shannon committed Oct 23, 2024
1 parent bec6a3b commit 583c796
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 90 deletions.
3 changes: 2 additions & 1 deletion Mastodon/Protocol/PagerTabStripNavigateable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import MastodonLocalization
typealias PagerTabStripNavigateable = PagerTabStripNavigateableCore & PagerTabStripNavigateableRelay

protocol PagerTabStripNavigateableCore: AnyObject {
var navigateablePageViewController: PagerTabStripViewController { get }
var navigateablePageViewController: PagerTabStripViewController? { get }
var pagerTabStripNavigateKeyCommands: [UIKeyCommand] { get }

func pagerTabStripNavigateKeyCommandHandler(_ sender: UIKeyCommand)
Expand Down Expand Up @@ -82,6 +82,7 @@ extension PagerTabStripNavigateableCore where Self: PagerTabStripNavigateableRel

extension PagerTabStripNavigateableCore {
func navigate(direction: PagerTabStripNavigationDirection) {
guard let navigateablePageViewController = navigateablePageViewController else { return }
let index = navigateablePageViewController.currentIndex
let targetIndex: Int

Expand Down
13 changes: 7 additions & 6 deletions Mastodon/Scene/Profile/Paging/ProfilePagingViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ final class ProfilePagingViewController: ButtonBarPagerTabStripViewController, T
weak var pagingDelegate: ProfilePagingViewControllerDelegate?

var disposeBag = Set<AnyCancellable>()
var viewModel: ProfilePagingViewModel!
var viewModel: ProfilePagingViewModel?

let buttonBarShadowView = UIView()
private var buttonBarShadowAlpha: CGFloat = 0.0

// MARK: - TabBarPageViewController

var currentPage: TabBarPage? {
return viewModel.viewControllers[currentIndex]
return viewModel?.viewControllers[currentIndex]
}

var currentPageIndex: Int? {
Expand All @@ -41,13 +41,13 @@ final class ProfilePagingViewController: ButtonBarPagerTabStripViewController, T
// MARK: - ButtonBarPagerTabStripViewController

override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] {
return viewModel.viewControllers
return viewModel?.viewControllers ?? []
}

override func updateIndicator(for viewController: PagerTabStripViewController, fromIndex: Int, toIndex: Int, withProgressPercentage progressPercentage: CGFloat, indexWasChanged: Bool) {
super.updateIndicator(for: viewController, fromIndex: fromIndex, toIndex: toIndex, withProgressPercentage: progressPercentage, indexWasChanged: indexWasChanged)

guard indexWasChanged else { return }
guard indexWasChanged, let viewModel = viewModel else { return }
let page = viewModel.viewControllers[toIndex]
tabBarPageViewDelegate?.pageViewController(self, didPresentingTabBarPage: page, at: toIndex)
}
Expand Down Expand Up @@ -88,7 +88,7 @@ extension ProfilePagingViewController {
buttonBarView.backgroundColor = .systemBackground
buttonBarShadowView.pinTo(to: buttonBarView)

viewModel.$needsSetupBottomShadow
viewModel?.$needsSetupBottomShadow
.receive(on: DispatchQueue.main)
.sink { [weak self] needsSetupBottomShadow in
guard let self = self else { return }
Expand Down Expand Up @@ -145,7 +145,7 @@ extension ProfilePagingViewController {
}

func setupBottomShadow() {
guard viewModel.needsSetupBottomShadow else {
guard let viewModel = viewModel, viewModel.needsSetupBottomShadow else {
buttonBarShadowView.layer.shadowColor = nil
buttonBarShadowView.layer.shadowRadius = 0
return
Expand Down Expand Up @@ -176,6 +176,7 @@ extension ProfilePagingViewController {
extension ProfilePagingViewController {

var currentViewController: (UIViewController & TabBarPage)? {
guard let viewModel = viewModel else { return nil }
guard !viewModel.viewControllers.isEmpty,
currentIndex < viewModel.viewControllers.count
else { return nil }
Expand Down
Loading

0 comments on commit 583c796

Please sign in to comment.