~ knotless Search/
↑↓ move↵ open⇧↵ all results

geesawra.industries/old-fella

Old people's social media app

0
1
main
190 lines · 6.6 KB Raw History
import Foundation
import SwiftUI

extension AppModel {
    func sceneBecameActive() {
        refreshExpiredModeration()
        if !isRestoringSession, account != nil {
            Task { await reloadContentPreferences() }
        }
    }

    func restoreSession() async {
        let session = moderationSession
        defer {
            isRestoringSession = false
            signedInAvatar?.finishPreload()
        }
        do {
            let restored = try await service.restoreAccount { [weak self] accountID in
                guard let self, moderationSession == session, !isSigningIn, !isSigningOut else { return }
                signedInAvatar?.preload(for: accountID)
            }
            guard let restored, session == moderationSession, !isSigningIn, !isSigningOut
            else { return }
            await signedInAvatar?.restore(for: restored)
            guard session == moderationSession, !isSigningIn, !isSigningOut else { return }
            account = restored
            if !composerOnly {
                await restoreFeedSelection()
                guard session == moderationSession else { return }
            }
            await reloadContentPreferences()
            guard session == moderationSession, contentPreferences != nil else { return }
            if !composerOnly { await loadFeed() }
        } catch {
            guard session == moderationSession, !Task.isCancelled, !NetworkFailure.isCancellation(error) else { return }
            errorMessage = NetworkFailure.message(for: error)
        }
    }

    func signIn(handle: String, appPassword: String, authFactorToken: String? = nil) async {
        guard !isSigningIn, !isSigningOut else { return }
        cancelContentPreferencesReload()
        moderationSession = UUID()
        let session = moderationSession
        followResults = [:]
        followingActorIDs = []
        activeFollowIDs = []
        uncertainFollowIDs = []
        contentPreferences = nil
        invalidateContent()
        moderationStates = [:]
        moderationBusy = []
        uncertainModerationIDs = [:]
        uncertainReports = []
        moderationError = nil
        isSigningIn = true
        errorMessage = nil
        authFactorErrorMessage = nil
        defer { isSigningIn = false }
        do {
            let signedIn = try await service.signIn(
                handle: handle,
                appPassword: appPassword,
                authFactorToken: authFactorToken
            )
            guard session == moderationSession, !isSigningOut else { return }
            signedInAvatar?.update(for: signedIn)
            account = signedIn
            await restoreFeedSelection()
            guard session == moderationSession else { return }
            requiresAuthFactorToken = false
            await reloadContentPreferences()
            guard session == moderationSession, contentPreferences != nil else { return }
            await loadFeed()
        } catch ServiceError.authFactorTokenRequired {
            guard session == moderationSession else { return }
            requiresAuthFactorToken = true
        } catch ServiceError.authFactorTokenRejected {
            guard session == moderationSession else { return }
            requiresAuthFactorToken = true
            authFactorErrorMessage = ServiceError.authFactorTokenRejected.localizedDescription
        } catch {
            guard session == moderationSession, !Task.isCancelled, !NetworkFailure.isCancellation(error) else { return }
            errorMessage = NetworkFailure.message(for: error)
        }
    }

    func cancelAuthFactorSignIn() {
        requiresAuthFactorToken = false
        authFactorErrorMessage = nil
        errorMessage = nil
    }

    func signInWithDemo() async {
        await signIn(handle: "demo.bsky.social", appPassword: "ui-test")
    }

    func signOut() async {
        guard !isSigningOut else { return }
        isSigningOut = true
        cancelContentPreferencesReload()
        defer { isSigningOut = false }
        contentPreferences = nil
        invalidateContent()
        do {
            try await service.signOut()
            clearSignedOutAccount()
        } catch {
            if !NetworkFailure.isCancellation(error) { errorMessage = NetworkFailure.message(for: error) }
            contentPreferencesError = "Could not load content preferences. Try again."
        }
    }
    private func clearSignedOutAccount() {
        contentGeneration = UUID()
        sessionGeneration = UUID()
        moderationSession = UUID()
        signedInAvatar?.clear()
        account = nil
        selectedFeed = nil
        padColumns = [.following, .notifications]
        padStates = [:]
        subscribedFeeds = []
        isLoadingFeeds = false
        feedsErrorMessage = nil
        contentPreferences = nil
        contentPreferencesError = nil
        contentPreferencesRefreshError = nil
        activePostActions = []
        deletedPostIDs = []
        activeFollowIDs = []
        uncertainPostIDs = []
        uncertainFollowIDs = []
        uncertainDraftKeys = []
        isPublishing = false
        publishErrorMessage = nil
        publishResultUnknown = false
        posts = []
        notifications = []
        notificationPageStarts = []
        notificationsCursor = nil
        hasMoreNotifications = false
        notificationsErrorMessage = nil
        replyContexts = [:]
        repostedBy = [:]
        threadContexts = [:]
        loadingPostIDs = []
        failedPostIDs = []
        postErrors = [:]
        feedErrorMessage = nil
        retryFeedPage = false
        isLoadingPost = false
        timelinePostIDs = []
        cursor = nil
        hasMorePosts = false
        clearSignedOutInteractions()
    }

    private func clearSignedOutInteractions() {
        followingActorIDs = []
        followResults = [:]
        moderationStates = [:]
        moderationBusy = []
        uncertainModerationIDs = [:]
        uncertainReports = []
        moderationError = nil
        profiles = [:]
        authorPosts = [:]
        profileCursors = [:]
        profileErrors = [:]
        profileRepostedBy = [:]
        loadingMoreProfileIDs = []
        loadingProfileIDs = []
        feedRequest = nil
        moreFeedRequest = nil
        postRequests = [:]
        profileRequests = [:]
        moreProfileRequests = [:]
        notificationsRequest = nil
        moreNotificationsRequest = nil
        isLoadingNotifications = false
        isLoadingMoreNotifications = false
        resumeNotificationLoads()
        failedNotificationsPage = false
        isLoading = false
        isLoadingMore = false
        errorMessage = nil
    }

}