Skip to content

Instantly share code, notes, and snippets.

View michzio's full-sized avatar

Michał Ziobro michzio

  • Cracow
View GitHub Profile
@michzio
michzio / DocumentPreview
Created March 16, 2022 13:31
DocumentPreview wrapping UIDocumentInteractionController
struct DocumentPreview: UIViewControllerRepresentable {
final class Coordintor: NSObject, UIDocumentInteractionControllerDelegate {
private let parent: DocumentPreview
init(_ parent: DocumentPreview) {
self.parent = parent
}
@michzio
michzio / WordsetCategoriesViewModel+Simplifies.swift
Created December 12, 2021 14:49
SwiftUI MVVM architecture - ViewModel simplified
@Published var categories: [WorsetCategory] = []
@Published var error: Error?
@Published var isLoading: Bool = false
func fetchWordsetCategories() {
guard !isLoading else { return }
repo.fetchWordsetCategories()
.sink(receiveCompletion: { completion in
switch completion {
@michzio
michzio / FetchCachedDataAndRefresh.swift
Created December 12, 2021 14:28
SwiftUI MVVM architecture - fetching cached data with refreshing
public func fetchWordsetCategories() -> AnyPublisher<[WordsetCategory], Error> {
let cachedDataPublisher = dao.fetchCategories()
let refreshedDataPublisher = service.getCategories(from: "pl", to: "en")
.flatMap { [weak self] categories in
self?.dao.update(categories)
}
.eraseToAnyPublisher()
return Publishers.MergeMany(cachedDataPublisher, refreshedDataPublisher)
@michzio
michzio / WordsetCategory.swift
Created December 12, 2021 14:01
SwiftUI MVVM architecture - Entity
@objc(WordsetCategory)
public class WordsetCategory: NSManagedObject { }
extension WordsetCategory {
@nonobjc public class func fetchRequest() -> NSFetchRequest<WordsetCategory> {
return NSFetchRequest<WordsetCategory>(entityName: "WordsetCategory")
}
@NSManaged public var categoryId: String?
@michzio
michzio / WordsetCategoriesDao.swift
Created December 12, 2021 13:55
SwiftUI MVVM architecture - Dao
class WordsetCategoryDao: BaseDao<WordsetCategoryDTO, WordsetCategory>, WordsetCategoryDaoProtocol {
func update(_ entities: [WordsetCategoryDTO]) -> Future<UpdateResult, Error> {
Future { [weak self] promise in
let context = self?.storage.backgroundContext
context.perform {
switch Self.update(context: context, entities) {
case .success(let change):
@michzio
michzio / WordsetCategoriesDao.swift
Created December 12, 2021 13:49
SwiftUI MVVM architectures - Dao protocol
public protocol WordsetCategoryDaoProtocol {
// DaoProtocol
func insert(_ e: WordsetCategoryDTO) -> Future<WordsetCategory, Error>
func delete(_ e: WordsetCategoryDTO) -> Future<Int, Error>
func update(_ e: WordsetCategoryDTO) -> Future<WordsetCategory, Error>
func load(id: String) -> Future<WordsetCategory, Error>
// WordsetCategoryDaoProtocol
func update(_ entities: [WordsetCategoryDTO]) -> Future<UpdateResult, Error>
@michzio
michzio / WordsetCategoryDTO.swift
Created December 12, 2021 13:37
SwiftUI MVVM architecture - DTO
struct WordsetCategoryDTO: Codable {
let categoryId: String
let foreignName: String
let nativeName: String
var image: String?
enum CodingKeys: String, CodingKey {
case categoryId = "cid"
case foreignName = "foreignName"
case nativeName = "nativeName"
@michzio
michzio / WordsetRouter.swift
Created December 12, 2021 13:31
SwiftUI MVVM architecture - Router
enum WordsetRouter: Router {
case getWordsets
case getWordset(id: String)
static var baseURL: String = "https://mockapi.com"
var path: String {
switch self {
case .getWordsets:
@michzio
michzio / WordsetService.swift
Created December 12, 2021 13:22
SwiftUI MVVM architecture - Service
public protocol WordsetServiceProtocol {
func getCategories(from fromLang: String, to toLang: String) -> AnyPublisher<[WordsetCategoryDTO], Error>
}
public final class WordsetService: BaseService, WordsetServiceProtocol {
func getCategories(from fromLang: String, to toLang: String) -> AnyPublisher<[WordsetCategoryDTO], Error> {
let endpoint = Router.wordsetCategories(fromLang: fromLang, toLang: toLang)
let xml : AnyPublisher<WordsetCategoriesDTO, Error> = request(endpoint)
@michzio
michzio / WordsetCategoriesRepository.swift
Created December 12, 2021 10:38
SwiftUI MVVM architecture - Repository
public protocol WordsetCategoriesRepositoryProtocol {
func syncWordsetCategories() -> AnyPublisher<UpdateResult, Error>
func insertWordsetCategory() -> AnyPublisher<WordsetCategory, Error>
}
public final class WordsetCategoriesRepository: WordsetCategoryRepositoryProtocol {
@Inject private var service: WordsetCategoriesServiceProtocol