mirror of
https://github.com/Dimillian/IceCubesApp.git
synced 2025-09-01 23:53:48 +00:00
Reflect edit / post / delete status better
This commit is contained in:
parent
8c72b627df
commit
8ee5da319c
6 changed files with 34 additions and 4 deletions
|
@ -23,7 +23,7 @@ struct IceCubesApp: App {
|
||||||
@State var currentAccount = CurrentAccount.shared
|
@State var currentAccount = CurrentAccount.shared
|
||||||
@State var userPreferences = UserPreferences.shared
|
@State var userPreferences = UserPreferences.shared
|
||||||
@State var pushNotificationsService = PushNotificationsService.shared
|
@State var pushNotificationsService = PushNotificationsService.shared
|
||||||
@State var watcher = StreamWatcher()
|
@State var watcher = StreamWatcher.shared
|
||||||
@State var quickLook = QuickLook.shared
|
@State var quickLook = QuickLook.shared
|
||||||
@State var theme = Theme.shared
|
@State var theme = Theme.shared
|
||||||
|
|
||||||
|
|
|
@ -26,7 +26,9 @@ import Observation
|
||||||
public var unreadNotificationsCount: Int = 0
|
public var unreadNotificationsCount: Int = 0
|
||||||
public var latestEvent: (any StreamEvent)?
|
public var latestEvent: (any StreamEvent)?
|
||||||
|
|
||||||
public init() {
|
public static let shared = StreamWatcher()
|
||||||
|
|
||||||
|
private init() {
|
||||||
decoder.keyDecodingStrategy = .convertFromSnakeCase
|
decoder.keyDecodingStrategy = .convertFromSnakeCase
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -149,4 +151,22 @@ import Observation
|
||||||
return nil
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,7 +22,7 @@ public struct StreamEventUpdate: StreamEvent {
|
||||||
|
|
||||||
public struct StreamEventStatusUpdate: StreamEvent {
|
public struct StreamEventStatusUpdate: StreamEvent {
|
||||||
public let date = Date()
|
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 let status: Status
|
||||||
public init(status: Status) {
|
public init(status: Status) {
|
||||||
self.status = status
|
self.status = status
|
||||||
|
|
|
@ -201,8 +201,14 @@ import SwiftUI
|
||||||
switch mode {
|
switch mode {
|
||||||
case .new, .replyTo, .quote, .mention, .shareExtension:
|
case .new, .replyTo, .quote, .mention, .shareExtension:
|
||||||
postStatus = try await client.post(endpoint: Statuses.postStatus(json: data))
|
postStatus = try await client.post(endpoint: Statuses.postStatus(json: data))
|
||||||
|
if let postStatus {
|
||||||
|
StreamWatcher.shared.emmitPostEvent(for: postStatus)
|
||||||
|
}
|
||||||
case let .edit(status):
|
case let .edit(status):
|
||||||
postStatus = try await client.put(endpoint: Statuses.editStatus(id: status.id, json: data))
|
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))
|
HapticManager.shared.fireHaptic(.notification(.success))
|
||||||
if hasExplicitlySelectedLanguage, let selectedLanguage {
|
if hasExplicitlySelectedLanguage, let selectedLanguage {
|
||||||
|
|
|
@ -295,6 +295,7 @@ import SwiftUI
|
||||||
func delete() async {
|
func delete() async {
|
||||||
do {
|
do {
|
||||||
_ = try await client.delete(endpoint: Statuses.status(id: status.id))
|
_ = try await client.delete(endpoint: Statuses.status(id: status.id))
|
||||||
|
StreamWatcher.shared.emmitDeleteEvent(for: status.id)
|
||||||
} catch {}
|
} catch {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -110,6 +110,7 @@ import SwiftUI
|
||||||
|
|
||||||
func handleEvent(event: any StreamEvent) async {
|
func handleEvent(event: any StreamEvent) async {
|
||||||
if let event = event as? StreamEventUpdate,
|
if let event = event as? StreamEventUpdate,
|
||||||
|
let client,
|
||||||
timeline == .home,
|
timeline == .home,
|
||||||
canStreamEvents,
|
canStreamEvents,
|
||||||
isTimelineVisible,
|
isTimelineVisible,
|
||||||
|
@ -119,6 +120,7 @@ import SwiftUI
|
||||||
let newStatus = event.status
|
let newStatus = event.status
|
||||||
await datasource.insert(newStatus, at: 0)
|
await datasource.insert(newStatus, at: 0)
|
||||||
await cacheHome()
|
await cacheHome()
|
||||||
|
StatusDataControllerProvider.shared.updateDataControllers(for: [event.status], client: client)
|
||||||
let statuses = await datasource.get()
|
let statuses = await datasource.get()
|
||||||
withAnimation {
|
withAnimation {
|
||||||
statusesState = .display(statuses: statuses, nextPageState: .hasNextPage)
|
statusesState = .display(statuses: statuses, nextPageState: .hasNextPage)
|
||||||
|
@ -130,8 +132,9 @@ import SwiftUI
|
||||||
withAnimation {
|
withAnimation {
|
||||||
statusesState = .display(statuses: statuses, nextPageState: .hasNextPage)
|
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) {
|
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)
|
await datasource.replace(event.status, at: originalIndex)
|
||||||
let statuses = await datasource.get()
|
let statuses = await datasource.get()
|
||||||
await cacheHome()
|
await cacheHome()
|
||||||
|
|
Loading…
Reference in a new issue