← All repositories

crossmate

A collaborative crossword app for iPhone and iPad

git clone https://code.inqk.net/crossmate.git


Crossmate/Services/AppActions.swift (3053B)

1import Foundation
2import SwiftUI
3
4@MainActor
5final class AppActions {
6    private unowned let services: AppServices
7
8    init(services: AppServices) {
9        self.services = services
10    }
11
12    func resetDatabase() async {
13        do {
14            try await services.cloudService.resetAllData()
15        } catch {
16            services.announcements.post(Announcement(
17                id: "reset-database-error",
18                scope: .global,
19                severity: .error,
20                title: "Resetting Failed",
21                body: error.localizedDescription,
22                dismissal: .manual
23            ))
24        }
25    }
26
27    func inviteFriend(gameID: UUID, friendAuthorID: String) async throws {
28        do {
29            try await services.invites.inviteFriend(
30                gameID: gameID,
31                friendAuthorID: friendAuthorID
32            )
33            services.friendInviteRecency.recordInvite(to: friendAuthorID)
34        } catch let error as SyncEngine.PingOutboxError {
35            if error == .deliveryPending {
36                services.friendInviteRecency.recordInvite(to: friendAuthorID)
37            }
38            throw error
39        }
40    }
41
42    var recentFriendInviteRanks: [String: Int] {
43        services.friendInviteRecency.ranksByAuthorID
44    }
45
46    func inviteDelivery(gameID: UUID, friendAuthorID: String) -> InviteDelivery {
47        services.inviteDeliveries.delivery(
48            gameID: gameID,
49            friendAuthorID: friendAuthorID
50        )
51    }
52
53    func markInviteFailed(gameID: UUID, friendAuthorID: String, error: Error) {
54        services.inviteDeliveries.markFailed(
55            recordName: nil,
56            gameID: gameID,
57            friendAuthorID: friendAuthorID,
58            failure: InviteDeliveryFailure(error: error)
59        )
60    }
61
62    func acceptInvite(shareURL: String, pingRecordName: String) async throws {
63        _ = try await services.invites.acceptInvite(
64            shareURL: shareURL,
65            pingRecordName: pingRecordName
66        )
67    }
68
69    func declineInvite(gameID: UUID) async throws {
70        try await services.invites.declineInvite(gameID: gameID)
71    }
72
73    func blockFriend(authorID: String) async {
74        await services.invites.blockFriend(authorID: authorID)
75    }
76
77    func unblockFriend(authorID: String) async {
78        await services.invites.unblockFriend(authorID: authorID)
79    }
80
81    func renameFriend(authorID: String, nickname: String) async {
82        do {
83            try await services.friendController.setNickname(
84                friendAuthorID: authorID,
85                nickname: nickname
86            )
87        } catch {
88            services.announcements.post(Announcement(
89                id: "rename-friend-error-\(authorID)",
90                scope: .global,
91                severity: .error,
92                title: "Renaming Failed",
93                body: error.localizedDescription,
94                dismissal: .manual
95            ))
96        }
97    }
98
99}
100
101extension EnvironmentValues {
102    @Entry var appActions: AppActions? = nil
103}