diff --git a/xcode/Subconscious/Shared/Components/AppView.swift b/xcode/Subconscious/Shared/Components/AppView.swift index b1d3c9d5..a1f050a5 100644 --- a/xcode/Subconscious/Shared/Components/AppView.swift +++ b/xcode/Subconscious/Shared/Components/AppView.swift @@ -306,8 +306,7 @@ enum AppAction: Hashable { case moveEntry(from: Slashlink, to: Slashlink) case updateAudience(address: Slashlink, audience: Audience) case assignColor(address: Slashlink, color: ThemeColor) - case likeEntry(address: Slashlink) - case unlikeEntry(address: Slashlink) + case setLiked(address: Slashlink, liked: Bool) // These notifications will be passe down to child stores to update themselves accordingly. case succeedSaveEntry(address: Slashlink, modified: Date) @@ -1293,17 +1292,12 @@ struct AppModel: ModelProtocol { address: address, color: color ) - case let .likeEntry(address): - return likeEntry( + case let .setLiked(address, liked): + return setLiked( state: state, environment: environment, - address: address - ) - case let .unlikeEntry(address): - return unlikeEntry( - state: state, - environment: environment, - address: address + address: address, + liked: liked ) case let .failSaveEntry(address, error): return operationFailed( @@ -3249,13 +3243,18 @@ struct AppModel: ModelProtocol { return Update(state: state, fx: fx) } - static func likeEntry( + static func setLiked( state: Self, environment: Environment, - address: Slashlink + address: Slashlink, + liked: Bool ) -> Update { let fx: Fx = Future.detached { - try await environment.userLikes.persistLike(for: address) + if liked { + try await environment.userLikes.persistLike(for: address) + } else { + try await environment.userLikes.removeLike(for: address) + } return .succeedUpdateLikeStatus(address: address, liked: true) } @@ -3269,27 +3268,6 @@ struct AppModel: ModelProtocol { return Update(state: state, fx: fx) } - - static func unlikeEntry( - state: Self, - environment: Environment, - address: Slashlink - ) -> Update { - let fx: Fx = Future.detached { - try await environment.userLikes.removeLike(for: address) - - return .succeedUpdateLikeStatus(address: address, liked: false) - } - .recover { error in - return .failUpdateLikeStatus( - address: address, - error: error.localizedDescription - ) - } - .eraseToAnyPublisher() - - return Update(state: state, fx: fx) - } } // MARK: Environment diff --git a/xcode/Subconscious/Shared/Components/Deck/DeckView.swift b/xcode/Subconscious/Shared/Components/Deck/DeckView.swift index 11f7db6d..943c0992 100644 --- a/xcode/Subconscious/Shared/Components/Deck/DeckView.swift +++ b/xcode/Subconscious/Shared/Components/Deck/DeckView.swift @@ -149,10 +149,7 @@ extension AppAction { case let .requestAppendToEntry(address, append): return .appendToEntry(address: address, append: append) case let .requestUpdateLikeStatus(address, liked): - if liked { - return .likeEntry(address: address) - } - return .unlikeEntry(address: address) + return .setLiked(address: address, liked: liked) default: return nil } diff --git a/xcode/Subconscious/Shared/Components/HomeProfileView.swift b/xcode/Subconscious/Shared/Components/HomeProfileView.swift index 3489700d..c0547f1b 100644 --- a/xcode/Subconscious/Shared/Components/HomeProfileView.swift +++ b/xcode/Subconscious/Shared/Components/HomeProfileView.swift @@ -241,10 +241,7 @@ extension AppAction { case let .requestAssignNoteColor(address, color): return .assignColor(address: address, color: color) case let .requestUpdateLikeStatus(address, liked): - if liked { - return .likeEntry(address: address) - } - return .unlikeEntry(address: address) + return .setLiked(address: address, liked: liked) default: return nil } diff --git a/xcode/Subconscious/Shared/Components/Notebook/Notebook.swift b/xcode/Subconscious/Shared/Components/Notebook/Notebook.swift index d81382f7..834a70ca 100644 --- a/xcode/Subconscious/Shared/Components/Notebook/Notebook.swift +++ b/xcode/Subconscious/Shared/Components/Notebook/Notebook.swift @@ -299,10 +299,7 @@ extension AppAction { case let .requestAssignNoteColor(address, color): return .assignColor(address: address, color: color) case let .requestUpdateLikeStatus(address, liked): - if liked { - return .likeEntry(address: address) - } - return .unlikeEntry(address: address) + return .setLiked(address: address, liked: liked) default: return nil }