Last active
December 9, 2022 22:22
-
-
Save thepearl/0acbd20b994b2fd34fd16f7afa2d9c1b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import Foundation | |
import SwiftUI | |
import Combine | |
// MARK: - Presentation Layer | |
struct MyView: View { | |
@ObservedObject var viewModel: MyViewModel | |
var body: some View { | |
Text(viewModel.data) | |
} | |
} | |
// MARK: - Domain Layer | |
class MyViewModel: ObservableObject { | |
@Published var data: String = "say my name" | |
func fetchData() { | |
// Fetch data from the network or a local database | |
// using the data access layer | |
} | |
} | |
// MARK: - Data Access Layer | |
protocol DataFetcher { | |
func fetchData() -> AnyPublisher<String, Error> | |
} | |
class NetworkDataFetcher: DataFetcher { | |
func fetchData() -> AnyPublisher<String, Error> { | |
// Fetch data from the network and call the completion handler | |
Just("Hello") | |
.setFailureType(to: Error.self) | |
.eraseToAnyPublisher() | |
} | |
} | |
class DatabaseDataFetcher: DataFetcher { | |
func fetchData() -> AnyPublisher<String, Error> { | |
// Fetch data from the local database and call the completion handler | |
Just("Mom") | |
.setFailureType(to: Error.self) | |
.eraseToAnyPublisher() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment