forked from touchlab/KaMPKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
165 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// | ||
// BreedListView.swift | ||
// KaMPKitiOS | ||
// | ||
// Created by Russell Wolf on 7/26/21. | ||
// Copyright © 2021 Touchlab. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
import shared | ||
|
||
struct BreedsScreen: View { | ||
@StateObject | ||
var viewModel = BreedsViewModel() | ||
|
||
var body: some View { | ||
BreedsContent( | ||
state: viewModel.state, | ||
onBreedFavorite: { viewModel.onBreedFavorite($0) }, | ||
refresh: { viewModel.refresh() } | ||
) | ||
.onAppear(perform: { | ||
viewModel.subscribeState() | ||
}) | ||
.onDisappear(perform: { | ||
viewModel.unsubscribeState() | ||
}) | ||
} | ||
} | ||
|
||
struct BreedsContent: View { | ||
var state: BreedViewState | ||
var onBreedFavorite: (Breed) -> Void | ||
var refresh: () -> Void | ||
|
||
var body: some View { | ||
ZStack { | ||
VStack { | ||
List(state.breeds, id: \.id) { breed in | ||
BreedRowView(breed: breed) { | ||
onBreedFavorite(breed) | ||
} | ||
} | ||
if let error = state.error { | ||
Text(error) | ||
.foregroundColor(.red) | ||
} | ||
Button("Refresh") { | ||
refresh() | ||
} | ||
} | ||
if state.isLoading { Text("Loading...") } | ||
} | ||
} | ||
} | ||
|
||
struct BreedRowView: View { | ||
var breed: Breed | ||
var onTap: () -> Void | ||
|
||
var body: some View { | ||
Button(action: onTap) { | ||
HStack { | ||
Text(breed.name) | ||
.padding(4.0) | ||
Spacer() | ||
Image(systemName: (!breed.favorite) ? "heart" : "heart.fill") | ||
.padding(4.0) | ||
} | ||
} | ||
} | ||
} | ||
|
||
struct BreedsScreen_Previews: PreviewProvider { | ||
static var previews: some View { | ||
BreedsContent( | ||
state: BreedViewState( | ||
breeds: [ | ||
Breed(id: 0, name: "appenzeller", favorite: false), | ||
Breed(id: 1, name: "australian", favorite: true) | ||
], | ||
error: nil, | ||
isLoading: false, | ||
isEmpty: false | ||
), | ||
onBreedFavorite: { _ in }, | ||
refresh: {} | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// | ||
// BreedsViewModel.swift | ||
// KaMPKitiOS | ||
// | ||
// Created by Bartłomiej Pedryc on 09/06/2023. | ||
// Copyright © 2023 Touchlab. All rights reserved. | ||
// | ||
|
||
import Combine | ||
import Foundation | ||
import shared | ||
import KMPNativeCoroutinesCombine | ||
|
||
class BreedsViewModel: ObservableObject { | ||
|
||
@Published var state: BreedViewState = BreedViewState.companion.default() | ||
|
||
private var viewModelDelegate: BreedViewModelDelegate = KotlinDependencies.shared.getBreedViewModel() | ||
private var cancellables = [AnyCancellable]() | ||
|
||
deinit { | ||
viewModelDelegate.clear() | ||
} | ||
|
||
func subscribeState() { | ||
createPublisher(for: viewModelDelegate.breedStateFlow) | ||
.sink { _ in } receiveValue: { [weak self] (breedState: BreedViewState) in | ||
self?.state = breedState | ||
} | ||
.store(in: &cancellables) | ||
} | ||
|
||
func unsubscribeState() { | ||
cancellables.forEach { $0.cancel() } | ||
cancellables.removeAll() | ||
} | ||
|
||
func onBreedFavorite(_ breed: Breed) { | ||
viewModelDelegate.updateBreedFavorite(breed: breed) | ||
} | ||
|
||
func refresh() { | ||
viewModelDelegate.refreshBreeds() | ||
} | ||
} |
Oops, something went wrong.