Skip to content

Commit

Permalink
Change the ios viewmodel naming
Browse files Browse the repository at this point in the history
  • Loading branch information
bpedryc committed Jun 9, 2023
1 parent f1cbc9f commit 08e4c8a
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 155 deletions.
20 changes: 16 additions & 4 deletions ios/KaMPKitiOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
objects = {

/* Begin PBXBuildFile section */
3C73D04A2A335103003E6929 /* BreedsViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C73D0492A335103003E6929 /* BreedsViewModel.swift */; };
3CD290EC2A251417004C7AD1 /* KMPNativeCoroutinesCombine in Frameworks */ = {isa = PBXBuildFile; productRef = 3CD290EB2A251417004C7AD1 /* KMPNativeCoroutinesCombine */; };
3CD290EE2A251417004C7AD1 /* KMPNativeCoroutinesCore in Frameworks */ = {isa = PBXBuildFile; productRef = 3CD290ED2A251417004C7AD1 /* KMPNativeCoroutinesCore */; };
3DFF917C64A18A83DA010EE1 /* Pods_KaMPKitiOS.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B859F3FB23133D22AB9DD835 /* Pods_KaMPKitiOS.framework */; };
46A5B5EF26AF54F7002EFEAA /* BreedListScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46A5B5EE26AF54F7002EFEAA /* BreedListScreen.swift */; };
46A5B5EF26AF54F7002EFEAA /* BreedsScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46A5B5EE26AF54F7002EFEAA /* BreedsScreen.swift */; };
46A5B60826B04921002EFEAA /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 46A5B60626B04920002EFEAA /* Main.storyboard */; };
46B5284D249C5CF400A7725D /* Koin.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46B5284C249C5CF400A7725D /* Koin.swift */; };
F1465F0123AA94BF0055F7C3 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = F1465F0023AA94BF0055F7C3 /* AppDelegate.swift */; };
Expand Down Expand Up @@ -40,7 +41,8 @@
/* Begin PBXFileReference section */
1DFCC00C8DAA719770A18D1A /* Pods-KaMPKitiOS.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-KaMPKitiOS.release.xcconfig"; path = "Pods/Target Support Files/Pods-KaMPKitiOS/Pods-KaMPKitiOS.release.xcconfig"; sourceTree = "<group>"; };
2A1ED6A4A2A53F5F75C58E5F /* Pods-KaMPKitiOS.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-KaMPKitiOS.release.xcconfig"; path = "Pods/Target Support Files/Pods-KaMPKitiOS/Pods-KaMPKitiOS.release.xcconfig"; sourceTree = "<group>"; };
46A5B5EE26AF54F7002EFEAA /* BreedListScreen.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BreedListScreen.swift; sourceTree = "<group>"; };
3C73D0492A335103003E6929 /* BreedsViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BreedsViewModel.swift; sourceTree = "<group>"; };
46A5B5EE26AF54F7002EFEAA /* BreedsScreen.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BreedsScreen.swift; sourceTree = "<group>"; };
46A5B60726B04920002EFEAA /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
46B5284C249C5CF400A7725D /* Koin.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Koin.swift; sourceTree = "<group>"; };
B859F3FB23133D22AB9DD835 /* Pods_KaMPKitiOS.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_KaMPKitiOS.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -87,6 +89,15 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
3C73D0482A335061003E6929 /* Breeds */ = {
isa = PBXGroup;
children = (
46A5B5EE26AF54F7002EFEAA /* BreedsScreen.swift */,
3C73D0492A335103003E6929 /* BreedsViewModel.swift */,
);
path = Breeds;
sourceTree = "<group>";
};
6278498AD96A4D949D39BF44 /* Frameworks */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -131,13 +142,13 @@
F1465EFF23AA94BF0055F7C3 /* KaMPKitiOS */ = {
isa = PBXGroup;
children = (
3C73D0482A335061003E6929 /* Breeds */,
F1465F0023AA94BF0055F7C3 /* AppDelegate.swift */,
46A5B60626B04920002EFEAA /* Main.storyboard */,
46B5284C249C5CF400A7725D /* Koin.swift */,
F1465F0923AA94BF0055F7C3 /* Assets.xcassets */,
F1465F0B23AA94BF0055F7C3 /* LaunchScreen.storyboard */,
F1465F0E23AA94BF0055F7C3 /* Info.plist */,
46A5B5EE26AF54F7002EFEAA /* BreedListScreen.swift */,
);
path = KaMPKitiOS;
sourceTree = "<group>";
Expand Down Expand Up @@ -362,7 +373,8 @@
buildActionMask = 2147483647;
files = (
46B5284D249C5CF400A7725D /* Koin.swift in Sources */,
46A5B5EF26AF54F7002EFEAA /* BreedListScreen.swift in Sources */,
3C73D04A2A335103003E6929 /* BreedsViewModel.swift in Sources */,
46A5B5EF26AF54F7002EFEAA /* BreedsScreen.swift in Sources */,
F1465F0123AA94BF0055F7C3 /* AppDelegate.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
2 changes: 1 addition & 1 deletion ios/KaMPKitiOS/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

startKoin()

let viewController = UIHostingController(rootView: BreedListScreen())
let viewController = UIHostingController(rootView: BreedsScreen())

self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = viewController
Expand Down
145 changes: 0 additions & 145 deletions ios/KaMPKitiOS/BreedListScreen.swift

This file was deleted.

90 changes: 90 additions & 0 deletions ios/KaMPKitiOS/Breeds/BreedsScreen.swift
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: {}
)
}
}
45 changes: 45 additions & 0 deletions ios/KaMPKitiOS/Breeds/BreedsViewModel.swift
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()
}
}
Loading

0 comments on commit 08e4c8a

Please sign in to comment.