Reflect edit / post / delete status better

This commit is contained in:
Thomas Ricouard 2024-01-01 16:46:34 +01:00
parent 8c72b627df
commit 8ee5da319c
6 changed files with 34 additions and 4 deletions

View file

@ -23,7 +23,7 @@ struct IceCubesApp: App {
@State var currentAccount = CurrentAccount.shared
@State var userPreferences = UserPreferences.shared
@State var pushNotificationsService = PushNotificationsService.shared
@State var watcher = StreamWatcher()
@State var watcher = StreamWatcher.shared
@State var quickLook = QuickLook.shared
@State var theme = Theme.shared

View file

@ -26,7 +26,9 @@ import Observation
public var unreadNotificationsCount: Int = 0
public var latestEvent: (any StreamEvent)?
public init() {
public static let shared = StreamWatcher()
private init() {
decoder.keyDecodingStrategy = .convertFromSnakeCase
}
@ -149,4 +151,22 @@ import Observation
return nil
}
}
public func emmitDeleteEvent(for status: String) {
let event = StreamEventDelete(status: status)
self.events.append(event)
self.latestEvent = event
}
public func emmitEditEvent(for status: Status) {
let event = StreamEventStatusUpdate(status: status)
self.events.append(event)
self.latestEvent = event
}
public func emmitPostEvent(for status: Status) {
let event = StreamEventUpdate(status: status)
self.events.append(event)
self.latestEvent = event
}
}

View file

@ -22,7 +22,7 @@ public struct StreamEventUpdate: StreamEvent {
public struct StreamEventStatusUpdate: StreamEvent {
public let date = Date()
public var id: String { status.id }
public var id: String { status.id + (status.editedAt?.asDate.description ?? "")}
public let status: Status
public init(status: Status) {
self.status = status

View file

@ -201,8 +201,14 @@ import SwiftUI
switch mode {
case .new, .replyTo, .quote, .mention, .shareExtension:
postStatus = try await client.post(endpoint: Statuses.postStatus(json: data))
if let postStatus {
StreamWatcher.shared.emmitPostEvent(for: postStatus)
}
case let .edit(status):
postStatus = try await client.put(endpoint: Statuses.editStatus(id: status.id, json: data))
if let postStatus {
StreamWatcher.shared.emmitEditEvent(for: postStatus)
}
}
HapticManager.shared.fireHaptic(.notification(.success))
if hasExplicitlySelectedLanguage, let selectedLanguage {

View file

@ -295,6 +295,7 @@ import SwiftUI
func delete() async {
do {
_ = try await client.delete(endpoint: Statuses.status(id: status.id))
StreamWatcher.shared.emmitDeleteEvent(for: status.id)
} catch {}
}

View file

@ -110,6 +110,7 @@ import SwiftUI
func handleEvent(event: any StreamEvent) async {
if let event = event as? StreamEventUpdate,
let client,
timeline == .home,
canStreamEvents,
isTimelineVisible,
@ -119,6 +120,7 @@ import SwiftUI
let newStatus = event.status
await datasource.insert(newStatus, at: 0)
await cacheHome()
StatusDataControllerProvider.shared.updateDataControllers(for: [event.status], client: client)
let statuses = await datasource.get()
withAnimation {
statusesState = .display(statuses: statuses, nextPageState: .hasNextPage)
@ -130,8 +132,9 @@ import SwiftUI
withAnimation {
statusesState = .display(statuses: statuses, nextPageState: .hasNextPage)
}
} else if let event = event as? StreamEventStatusUpdate {
} else if let event = event as? StreamEventStatusUpdate, let client {
if let originalIndex = await datasource.indexOf(statusId: event.status.id) {
StatusDataControllerProvider.shared.updateDataControllers(for: [event.status], client: client)
await datasource.replace(event.status, at: originalIndex)
let statuses = await datasource.get()
await cacheHome()