Skip to content

Commit

Permalink
Code Improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamalifard, Mehran committed Apr 26, 2024
1 parent dabb921 commit e2ff80b
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 18 deletions.
12 changes: 6 additions & 6 deletions EasyCrypto/Base/BaseViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ protocol ViewModelService: AnyObject {
typealias BaseViewModel = BaseViewModelEventSource & ViewModelService

open class DefaultViewModel: BaseViewModel, ObservableObject {

var loadingState = CurrentValueSubject<ViewModelStatus, Never>(.dismissAlert)
let subscriber = Cancelable()

func call<ReturnType>(callWithIndicator: Bool = true,
argument: AnyPublisher<ReturnType?,
APIError>,
callback: @escaping (_ data: ReturnType?) -> Void) {

if callWithIndicator {
self.loadingState.send(.loadStart)
}

let completionHandler: (Subscribers.Completion<APIError>) -> Void = { [weak self] completion in
switch completion {
case .failure(let error):
Expand All @@ -51,11 +51,11 @@ open class DefaultViewModel: BaseViewModel, ObservableObject {
self?.loadingState.send(.dismissAlert)
}
}

let resultValueHandler: (ReturnType?) -> Void = { data in
callback(data)
}

argument
.subscribe(on: WorkScheduler.backgroundWorkScheduler)
.receive(on: WorkScheduler.mainScheduler)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import Combine
struct CoinDetailCoordinator: CoordinatorProtocol {

@ObservedObject var viewModel: CoinDetailViewModel

@State var activeRoute: Destination? = Destination(route: .first(url: nil))
@State var transition: Transition?

let id: String

let subscriber = Cancelable()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import SwiftUI
struct CoinDetailHeaderView: View {

var item: CoinUnit

var url: ((String?) -> Void)

var body: some View {
Expand Down
6 changes: 6 additions & 0 deletions EasyCrypto/Presentation/CoinDetail/View/CoinDetailView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ struct CoinDetailView: Coordinatable {
}

@ObservedObject private(set) var viewModel: CoinDetailViewModel

@State private var isLoading: Bool = false
@State private var presentAlert = false
@State private var alertMessage: String = ""

let subscriber = Cancelable()

var id: String?
var coinData: CoinUnit {
return self.viewModel.coinData
Expand All @@ -33,6 +35,10 @@ struct CoinDetailView: Coordinatable {
}

var body: some View {
content
}

var content: some View {
NavigationView {
GeometryReader { geo in
let geoSize = geo.size
Expand Down
14 changes: 8 additions & 6 deletions EasyCrypto/Presentation/Detail/View/PriceView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import SwiftUI
struct PriceView: View {

let item: MarketsPrice

var viewModel: DetailViewModel

@State var isPersist: Bool = false
Expand All @@ -23,7 +24,7 @@ struct PriceView: View {
.font(FontManager.body)
Spacer()
Button {
self.handleDataPersistance(item: item)
self.handleDataPersistence(for: item)
} label: {
Image(isPersist ? Assets.save : Assets.unsave)
.resizable()
Expand Down Expand Up @@ -60,13 +61,14 @@ struct PriceView: View {
self.isPersist = self.viewModel.checkIfItemExist(item)
}
}
func handleDataPersistance(item: MarketsPrice) {

private func handleDataPersistence(for item: MarketsPrice) {
isPersist.toggle()

if isPersist {
self.isPersist = false
self.viewModel.deleteFromWishlist(item)
viewModel.addToWishlist(item)
} else {
self.isPersist = true
self.viewModel.addToWishlist(item)
viewModel.deleteFromWishlist(item)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Foundation
final class DetailViewModel: ObservableObject {

let title: String = Constants.Title.detailTitle

@Published var marketPrice = MarketsPrice()

private let repository: CacheRepositoryProtocol
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ extension MainCoordinator {
CoinDetailCoordinator(viewModel: CoinDetailViewModel(), id: data)
}
}

var transition: Transition {
switch route {
case .first: return .push
Expand Down
4 changes: 2 additions & 2 deletions EasyCrypto/Presentation/Main/View/MainView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ struct MainView: Coordinatable {
}
}

var coinsList: some View {
private var coinsList: some View {
ScrollView {
LazyVStack {
ForEach(viewModel.marketData, id: \.id) { item in
Expand Down Expand Up @@ -120,7 +120,7 @@ struct MainView: Coordinatable {
}
}

var whishList: some View {
private var whishList: some View {
ScrollView {
VStack {
ForEach(viewModel.wishListData, id: \.symbol) { item in
Expand Down
6 changes: 5 additions & 1 deletion EasyCrypto/Presentation/Main/View/SearchBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ struct SearchBar: View {
@Binding var isEditing: Bool

var body: some View {
content
}

var content: some View {
ZStack(alignment: .leading) {
HStack {
TextField("", text: $text)
Expand Down Expand Up @@ -49,7 +53,7 @@ struct SearchBar: View {
}
}
}.padding(.horizontal)
.frame(height: 40.0)
.frame(height: 40.0)
}
}
}
Expand Down
1 change: 1 addition & 0 deletions EasyCrypto/Presentation/Main/View/SortView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import SwiftUI
struct SortView: View {

@ObservedObject var viewModel: MainViewModel

@State var viewState: Bool = false

var body: some View {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
import SwiftUI

struct HandleViewModelStateModifier: ViewModifier {

@ObservedObject var viewModel: DefaultViewModel
@Binding var isLoading: Bool
@Binding var alertMessage: String
@Binding var presentAlert: Bool

func body(content: Content) -> some View {
content
.onReceive(viewModel.loadingState.receive(on: DispatchQueue.main)) { state in
Expand Down

0 comments on commit e2ff80b

Please sign in to comment.