Skip to content

Commit

Permalink
Implement the favorite behavior changes on iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
bpedryc committed Jul 3, 2023
1 parent 3938b59 commit 93f484d
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fun BreedDetailsScreen(
Spacer(Modifier.width(4.dp))
FavoriteIcon(
breed = state.breed,
onClick = viewModel::toggleFavorite
onClick = viewModel::onFavoriteClick
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fun BreedsScreen(
onRefresh = { viewModel.refreshBreeds() },
onSuccess = { data -> log.v { "View updating with ${data.size} breeds" } },
onError = { exception -> log.e { "Displaying error: $exception" } },
onBreedClick = { viewModel.navigateToDetails(it) },
onBreedClick = { viewModel.onBreedClick(it) },
)
}

Expand Down
26 changes: 20 additions & 6 deletions ios/KaMPKitiOS/BreedDetailsScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,23 @@ import KMPNativeCoroutinesCombine
import Foundation

class BreedDetailsViewModel: ObservableObject {
private var viewModel: BreedDetailsViewModelDelegate
private var viewModelDelegate: BreedDetailsViewModelDelegate

init(breedId: Int64) {
self.viewModel = KotlinDependencies.shared.getBreedDetailsViewModel(breedId: breedId)
self.viewModelDelegate = KotlinDependencies.shared.getBreedDetailsViewModel(breedId: breedId)
}

@Published
var detailsState: BreedDetailsViewState = BreedDetailsViewState.companion.default()

private var cancellables = [AnyCancellable]()

func onFavoriteClick() {
viewModelDelegate.onFavoriteClick()
}

func activate() {
createPublisher(for: viewModel.detailsStateFlow)
createPublisher(for: viewModelDelegate.detailsStateFlow)
.sink { _ in } receiveValue: { [weak self] (detailsState: BreedDetailsViewState) in
self?.detailsState = detailsState
}
Expand All @@ -38,7 +42,7 @@ class BreedDetailsViewModel: ObservableObject {
}

deinit {
viewModel.clear()
viewModelDelegate.clear()
}
}

Expand All @@ -48,7 +52,9 @@ struct BreedDetailsScreen: View {

var body: some View {
BreedDetailsContent(
breedName: viewModel.detailsState.breed.name
breedName: viewModel.detailsState.breed.name,
isBreedFavorite: viewModel.detailsState.breed.favorite,
onFavoriteClick: { viewModel.onFavoriteClick() }
)
.onAppear(perform: {
viewModel.activate()
Expand All @@ -61,7 +67,15 @@ struct BreedDetailsScreen: View {

struct BreedDetailsContent: View {
var breedName: String
var isBreedFavorite: Bool
var onFavoriteClick: () -> Void
var body: some View {
Text(breedName)
HStack {
Text(breedName)
Button(action: onFavoriteClick) {
Image(systemName: (!isBreedFavorite) ? "heart" : "heart.fill")
.padding(4.0)
}
}
}
}
14 changes: 7 additions & 7 deletions ios/KaMPKitiOS/Breeds/BreedsViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ import shared
import KMPNativeCoroutinesCombine

class BreedsViewModel: ObservableObject {

@Published var state: BreedViewState = BreedViewState.companion.default()

private var navCoordinator: BreedsNavCoordinator
private var viewModelDelegate: BreedsViewModelDelegate = KotlinDependencies.shared.getBreedsViewModel()
init(navCoordinator: BreedsNavCoordinator) {
self.navCoordinator = navCoordinator
}
private var cancellables = [AnyCancellable]()

deinit {
viewModelDelegate.clear()
}

func subscribeState() {
createPublisher(for: viewModelDelegate.breedStateFlow)
.sink { _ in } receiveValue: { [weak self] (breedState: BreedViewState) in
Expand All @@ -34,7 +34,7 @@ class BreedsViewModel: ObservableObject {
}
.store(in: &cancellables)
}

private func handleNavRequests(breedsState: BreedViewState) {
if let navRequest = breedsState.breedsNavRequest as? BreedsNavRequest.ToDetails {
self.navCoordinator.onBreedDetailsRequest(breedId: navRequest.breedId)
Expand All @@ -47,8 +47,8 @@ class BreedsViewModel: ObservableObject {
cancellables.removeAll()
}

func onBreedFavorite(_ breed: Breed) {
viewModelDelegate.updateBreedFavorite(breed: breed)
func onBreedClick(_ breedId: Int64) {
viewModelDelegate.onBreedClick(breedId: breedId)
}

func refresh() {
Expand Down
8 changes: 4 additions & 4 deletions ios/KaMPKitiOS/BreedsScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct BreedsScreen: View {
loading: viewModel.state.isLoading,
breeds: viewModel.state.breeds,
error: viewModel.state.error,
onBreedFavorite: { viewModel.onBreedFavorite($0) },
onBreedClick: { viewModel.onBreedClick($0) },
refresh: { viewModel.refresh() }
)
.onAppear(perform: {
Expand All @@ -39,7 +39,7 @@ struct BreedListContent: View {
var loading: Bool
var breeds: [Breed]?
var error: String?
var onBreedFavorite: (Breed) -> Void
var onBreedClick: (Int64) -> Void
var refresh: () -> Void

var body: some View {
Expand All @@ -48,7 +48,7 @@ struct BreedListContent: View {
if let breeds = breeds {
List(breeds, id: \.id) { breed in
BreedRowView(breed: breed) {
onBreedFavorite(breed)
onBreedClick(breed.id)
}
}
}
Expand Down Expand Up @@ -91,7 +91,7 @@ struct BreedListScreen_Previews: PreviewProvider {
Breed(id: 1, name: "australian", favorite: true)
],
error: nil,
onBreedFavorite: { _ in },
onBreedClick: { _ in },
refresh: {}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ class BreedDetailsViewModel(
}
}

fun toggleFavorite() = viewModelScope.launch {
dogRepository.updateBreedFavorite(breedId)
fun onFavoriteClick() {
viewModelScope.launch {
dogRepository.updateBreedFavorite(breedId)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class BreedsViewModel(
}
}

fun navigateToDetails(breedId: Long): Job {
fun onBreedClick(breedId: Long): Job {
return viewModelScope.launch {
mutableBreedState.update {
it.copy(breedsNavRequest = BreedsNavRequest.ToDetails(breedId))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class BreedViewModelTest {
@Test
fun `Navigate to breed details`() = runTest {
dbHelper.insertBreeds(breedNames)
viewModel.navigateToDetails(1).join()
viewModel.onBreedClick(1).join()

viewModel.breedState.test {
val state = awaitItem()
Expand Down

0 comments on commit 93f484d

Please sign in to comment.