Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

Commit

Permalink
Collapse likeEntry and unlikeEntry into setLiked
Browse files Browse the repository at this point in the history
  • Loading branch information
bfollington committed Feb 14, 2024
1 parent 252f4aa commit d980fc1
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 47 deletions.
48 changes: 13 additions & 35 deletions xcode/Subconscious/Shared/Components/AppView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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<Self> {
let fx: Fx<Action> = 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)
}
Expand All @@ -3269,27 +3268,6 @@ struct AppModel: ModelProtocol {

return Update(state: state, fx: fx)
}

static func unlikeEntry(
state: Self,
environment: Environment,
address: Slashlink
) -> Update<Self> {
let fx: Fx<Action> = 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
Expand Down
5 changes: 1 addition & 4 deletions xcode/Subconscious/Shared/Components/Deck/DeckView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
5 changes: 1 addition & 4 deletions xcode/Subconscious/Shared/Components/HomeProfileView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
5 changes: 1 addition & 4 deletions xcode/Subconscious/Shared/Components/Notebook/Notebook.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit d980fc1

Please sign in to comment.