diff --git a/Mastodon/Scene/Notification/NotificationViewModel.swift b/Mastodon/Scene/Notification/NotificationViewModel.swift index 2f2be1805..039653ee5 100644 --- a/Mastodon/Scene/Notification/NotificationViewModel.swift +++ b/Mastodon/Scene/Notification/NotificationViewModel.swift @@ -25,9 +25,26 @@ final class NotificationViewModel { // output let scopes = NotificationTimelineViewModel.Scope.allCases @Published var viewControllers: [UIViewController] = [] - @Published var currentPageIndex = 0 - + @Published var currentPageIndex = 0 { + didSet { + lastPageIndex = currentPageIndex + } + } + private var lastPageIndex: Int { + get { + UserDefaults.shared.getLastSelectedNotificationsTab( + accessToken: authContext.mastodonAuthenticationBox.userAuthorization.accessToken + ) + } + set { + UserDefaults.shared.setLastSelectedNotificationsTab( + accessToken: authContext.mastodonAuthenticationBox.userAuthorization.accessToken, + value: newValue + ) + } + } + init(context: AppContext, authContext: AuthContext) { self.context = context self.authContext = authContext @@ -58,7 +75,14 @@ extension NotificationViewModel: PageboyViewControllerDataSource { } func defaultPage(for pageboyViewController: PageboyViewController) -> PageboyViewController.Page? { - return .first + guard + let pageCount = pageboyViewController.pageCount, + pageCount > 1, + (0...(pageCount - 1)).contains(lastPageIndex) + else { + return .first /// this should never happen, but in case we somehow manage to acquire invalid data in `lastPageIndex` let's make sure not to crash the app. + } + return .at(index: lastPageIndex) } } diff --git a/MastodonSDK/Sources/MastodonCommon/Preference/Preference+Notification.swift b/MastodonSDK/Sources/MastodonCommon/Preference/Preference+Notification.swift index 38ed3aa5e..6e2871802 100644 --- a/MastodonSDK/Sources/MastodonCommon/Preference/Preference+Notification.swift +++ b/MastodonSDK/Sources/MastodonCommon/Preference/Preference+Notification.swift @@ -20,7 +20,8 @@ extension UserDefaults { } private static let notificationCountKeyPrefix = "notification_count" - + private static let notificationsLastTabIndexKeyPrefix = "last_notification_tab_index" + public func getNotificationCountWithAccessToken(accessToken: String) -> Int { let prefix = UserDefaults.notificationCountKeyPrefix let key = UserDefaults.deriveKey(from: accessToken, prefix: prefix) @@ -38,6 +39,17 @@ extension UserDefaults { setNotificationCountWithAccessToken(accessToken: accessToken, value: count + 1) } + @objc public func getLastSelectedNotificationsTab(accessToken: String) -> Int { + let prefix = UserDefaults.notificationsLastTabIndexKeyPrefix + let key = UserDefaults.deriveKey(from: accessToken, prefix: prefix) + return integer(forKey: key) + } + + @objc public func setLastSelectedNotificationsTab(accessToken: String, value: Int) { + let prefix = UserDefaults.notificationsLastTabIndexKeyPrefix + let key = UserDefaults.deriveKey(from: accessToken, prefix: prefix) + setValue(value, forKey: key) + } } extension UserDefaults {