StatusDataController: Handle remote status

This commit is contained in:
Thomas Ricouard 2023-03-01 07:28:46 +01:00
parent 963cef02a1
commit d2c58482f0
4 changed files with 21 additions and 18 deletions

View file

@ -13,9 +13,9 @@ public protocol StatusDataControlling: ObservableObject {
var reblogsCount: Int { get set }
var repliesCount: Int { get set }
func toggleBookmark() async
func toggleReblog() async
func toggleFavorite() async
func toggleBookmark(remoteStatus: String?) async
func toggleReblog(remoteStatus: String?) async
func toggleFavorite(remoteStatus: String?) async
}
@MainActor
@ -80,10 +80,11 @@ public final class StatusDataController: StatusDataControlling {
}
}
public func toggleFavorite() async {
public func toggleFavorite(remoteStatus: String?) async {
guard client.isAuth else { return }
isFavorited.toggle()
let endpoint = isFavorited ? Statuses.favorite(id: status.id) : Statuses.unfavorite(id: status.id)
let id = remoteStatus ?? status.id
let endpoint = isFavorited ? Statuses.favorite(id: id) : Statuses.unfavorite(id: id)
favoritesCount += isFavorited ? 1 : -1
objectWillChange.send()
do {
@ -97,10 +98,11 @@ public final class StatusDataController: StatusDataControlling {
}
public func toggleReblog() async {
public func toggleReblog(remoteStatus: String?) async {
guard client.isAuth else { return }
isReblogged.toggle()
let endpoint = isReblogged ? Statuses.reblog(id: status.id) : Statuses.unreblog(id: status.id)
let id = remoteStatus ?? status.id
let endpoint = isReblogged ? Statuses.reblog(id: id) : Statuses.unreblog(id: id)
reblogsCount += isReblogged ? 1 : -1
objectWillChange.send()
do {
@ -113,10 +115,11 @@ public final class StatusDataController: StatusDataControlling {
}
}
public func toggleBookmark() async {
public func toggleBookmark(remoteStatus: String?) async {
guard client.isAuth else { return }
isBookmarked.toggle()
let endpoint = isBookmarked ? Statuses.bookmark(id: status.id) : Statuses.unbookmark(id: status.id)
let id = remoteStatus ?? status.id
let endpoint = isBookmarked ? Statuses.bookmark(id: id) : Statuses.unbookmark(id: id)
do {
let status: Status = try await client.post(endpoint: endpoint)
updateFrom(status: status, publishUpdate: true)

View file

@ -156,13 +156,13 @@ struct StatusRowActionsView: View {
viewModel.routerPath.presentedSheet = .replyToStatusEditor(status: viewModel.localStatus ?? viewModel.status)
SoundEffectManager.shared.playSound(of: .share)
case .favorite:
await statusDataController.toggleFavorite()
await statusDataController.toggleFavorite(remoteStatus: viewModel.localStatusId)
SoundEffectManager.shared.playSound(of: .favorite)
case .bookmark:
await statusDataController.toggleBookmark()
await statusDataController.toggleBookmark(remoteStatus: viewModel.localStatusId)
SoundEffectManager.shared.playSound(of: .bookmark)
case .boost:
await statusDataController.toggleReblog()
await statusDataController.toggleReblog(remoteStatus: viewModel.localStatusId)
SoundEffectManager.shared.playSound(of: .boost)
default:
break

View file

@ -32,18 +32,18 @@ struct StatusRowContextMenu: View {
var body: some View {
if !viewModel.isRemote {
Button { Task {
await statusDataController.toggleFavorite()
await statusDataController.toggleFavorite(remoteStatus: nil)
} } label: {
Label(statusDataController.isFavorited ? "status.action.unfavorite" : "status.action.favorite", systemImage: "star")
}
Button { Task {
await statusDataController.toggleReblog()
await statusDataController.toggleReblog(remoteStatus: nil)
} } label: {
boostLabel
}
.disabled(viewModel.status.visibility == .direct || viewModel.status.visibility == .priv && viewModel.status.account.id != account.account?.id)
Button { Task {
await statusDataController.toggleBookmark()
await statusDataController.toggleBookmark(remoteStatus: nil)
} } label: {
Label(statusDataController.isBookmarked ? "status.action.unbookmark" : "status.action.bookmark",
systemImage: "bookmark")

View file

@ -62,16 +62,16 @@ struct StatusRowSwipeView: View {
makeSwipeButtonForRouterPath(action: action, destination: .quoteStatusEditor(status: viewModel.status))
case .favorite:
makeSwipeButtonForTask(action: action) {
await statusDataController.toggleFavorite()
await statusDataController.toggleFavorite(remoteStatus: nil)
}
case .boost:
makeSwipeButtonForTask(action: action, privateBoost: privateBoost()) {
await statusDataController.toggleReblog()
await statusDataController.toggleReblog(remoteStatus: nil)
}
.disabled(viewModel.status.visibility == .direct || viewModel.status.visibility == .priv && viewModel.status.account.id != currentAccount.account?.id)
case .bookmark:
makeSwipeButtonForTask(action: action) {
await statusDataController.toggleBookmark()
await statusDataController.toggleBookmark(remoteStatus: nil)
}
case .none:
EmptyView()