-
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
7 changed files
with
436 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,8 @@ struct PokemonApp: App { | |
var body: some Scene { | ||
|
||
WindowGroup { | ||
//ContentView() | ||
|
||
PokemonWorldView() | ||
} | ||
} | ||
} |
151 changes: 151 additions & 0 deletions
151
Pokemon/Pokemon/Scenes/PokemonWorld/PokemonWorldView.swift
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,151 @@ | ||
// | ||
// PokemonWorldView.swift | ||
// Pokemon | ||
// | ||
// Created by Xavier Ramos on 27/5/22. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct PokemonWorldView: View { | ||
|
||
@StateObject var viewModel = PokemonWorldViewModel() | ||
|
||
var body: some View { | ||
|
||
VStack { | ||
|
||
content | ||
} | ||
} | ||
|
||
@ViewBuilder | ||
private var content: some View { | ||
|
||
switch viewModel.state { | ||
case .empty: | ||
emptyView() | ||
case .loading: | ||
loadingView() | ||
case .failed(let errorMessage): | ||
failedView(errorMessage: errorMessage) | ||
case .loaded: | ||
loadedView() | ||
} | ||
} | ||
|
||
private func emptyView() -> some View { | ||
|
||
VStack { | ||
|
||
Text(""" | ||
\"Hello there! | ||
Welcome to the world of Pokémon! | ||
My name is Oak! | ||
People call me the Pokémon Prof! | ||
This world is inhabited by creatures called Pokémon! For some people, Pokémon are pets. Other use them for fights. Myself… I study Pokémon as a profession. | ||
Your very own Pokémon legend is about to unfold! | ||
A world of dreams and adventures with Pokémon awaits! Let's go!\" | ||
""") | ||
|
||
Spacer() | ||
|
||
ButtonView(imageName: "pokeball", | ||
text: "Find Pokémon") { | ||
|
||
self.viewModel.getPokemon() | ||
} | ||
|
||
Spacer() | ||
} | ||
.padding(16) | ||
} | ||
|
||
private func loadingView() -> some View { | ||
|
||
VStack { | ||
|
||
Spacer() | ||
|
||
HStack { | ||
|
||
Spacer() | ||
|
||
ProgressView("Finding Pokémon") | ||
|
||
Spacer() | ||
} | ||
|
||
Spacer() | ||
} | ||
} | ||
|
||
private func failedView(errorMessage: String) -> some View { | ||
|
||
VStack { | ||
|
||
Spacer() | ||
|
||
Text(errorMessage) | ||
|
||
Spacer().frame(height: 16) | ||
|
||
ButtonView(imageName: "pokeball", | ||
text: "Find Pokémon") { | ||
|
||
self.viewModel.getPokemon() | ||
} | ||
|
||
Spacer() | ||
} | ||
.padding(16) | ||
} | ||
|
||
private func loadedView() -> some View { | ||
|
||
VStack { | ||
|
||
Spacer() | ||
|
||
PokemonCardView(name: viewModel.pokemon?.name ?? "", | ||
image: viewModel.pokemon?.url ?? "", | ||
weight: viewModel.pokemon?.weight ?? 0, | ||
height: viewModel.pokemon?.height ?? 0) | ||
|
||
Spacer() | ||
|
||
HStack { | ||
|
||
ButtonView(imageName: "pokeball", | ||
text: "Find Pokémon", | ||
width: 60) { | ||
|
||
self.viewModel.getPokemon() | ||
} | ||
|
||
Spacer().frame(width: 10) | ||
|
||
ButtonView(imageName: "pokedex", | ||
text: "Catch Pokémon", | ||
width: 60) { | ||
|
||
self.viewModel.caughtPokemon() | ||
} | ||
.disabled(viewModel.isCatched ? true : false) | ||
.blur(radius: viewModel.isCatched ? 4 : 0) | ||
} | ||
|
||
Spacer() | ||
} | ||
.padding(16) | ||
} | ||
} | ||
|
||
struct PokemonWorldView_Previews: PreviewProvider { | ||
|
||
static var previews: some View { | ||
|
||
PokemonWorldView() | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
Pokemon/Pokemon/Scenes/PokemonWorld/PokemonWorldViewModel.swift
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,70 @@ | ||
// | ||
// PokemonWorldViewModel.swift | ||
// Pokemon | ||
// | ||
// Created by Xavier Ramos on 27/5/22. | ||
// | ||
|
||
import Foundation | ||
import SwiftUI | ||
import Combine | ||
|
||
class PokemonWorldViewModel: ObservableObject { | ||
|
||
// MARK: - Properties | ||
|
||
@Published var state: PokemonWorldViewState = .empty | ||
@Published public private(set) var pokemon: Pokemon? | ||
@Published public private(set) var isCatched: Bool = false | ||
|
||
private var cancellable: AnyCancellable? | ||
|
||
// MARK: - Methods | ||
|
||
init() { | ||
|
||
if pokemon != nil { | ||
|
||
state = .loaded | ||
} | ||
} | ||
|
||
func getPokemon() { | ||
|
||
state = .loading | ||
|
||
cancellable = GetPokemonUseCase().execute() | ||
.receive(on: DispatchQueue.main) | ||
.sink(receiveCompletion: { completion in | ||
|
||
switch completion { | ||
case .finished: | ||
self.state = .loaded | ||
self.isPokemonCatched() | ||
case .failure(let error): | ||
self.state = .failed("Get Pokémon Failed!") | ||
print("Get Pokemon failed \(error), \(error.localizedDescription)") | ||
} | ||
|
||
}, receiveValue: { (pokemon: Pokemon) in | ||
|
||
self.pokemon = pokemon | ||
}) | ||
} | ||
|
||
func isPokemonCatched() { | ||
|
||
isCatched = ExistsPokemonUseCase().execute(pokemon: pokemon!) | ||
} | ||
|
||
func caughtPokemon() { | ||
|
||
if !isCatched { | ||
|
||
SavePokemonUseCase().execute(pokemon: pokemon!) | ||
|
||
// Show message pokemon caught! | ||
|
||
} | ||
} | ||
} |
Oops, something went wrong.