Skip to content

Instantly share code, notes, and snippets.

Avatar

Michał Ziobro michzio

  • Cracow
View GitHub Profile
@michzio
michzio / DocumentPreview
Created Mar 16, 2022
DocumentPreview wrapping UIDocumentInteractionController
View DocumentPreview
struct DocumentPreview: UIViewControllerRepresentable {
final class Coordintor: NSObject, UIDocumentInteractionControllerDelegate {
private let parent: DocumentPreview
init(_ parent: DocumentPreview) {
self.parent = parent
}
@michzio
michzio / WordsetCategoriesViewModel+Simplifies.swift
Created Dec 12, 2021
SwiftUI MVVM architecture - ViewModel simplified
View WordsetCategoriesViewModel+Simplifies.swift
@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 Dec 12, 2021
SwiftUI MVVM architecture - fetching cached data with refreshing
View FetchCachedDataAndRefresh.swift
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 Dec 12, 2021
SwiftUI MVVM architecture - Entity
View WordsetCategory.swift
@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 Dec 12, 2021
SwiftUI MVVM architecture - Dao
View WordsetCategoriesDao.swift
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 Dec 12, 2021
SwiftUI MVVM architectures - Dao protocol
View WordsetCategoriesDao.swift
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 Dec 12, 2021
SwiftUI MVVM architecture - DTO
View WordsetCategoryDTO.swift
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 Dec 12, 2021
SwiftUI MVVM architecture - Router
View WordsetRouter.swift
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 Dec 12, 2021
SwiftUI MVVM architecture - Service
View WordsetService.swift
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 Dec 12, 2021
SwiftUI MVVM architecture - Repository
View WordsetCategoriesRepository.swift
public protocol WordsetCategoriesRepositoryProtocol {
func syncWordsetCategories() -> AnyPublisher<UpdateResult, Error>
func insertWordsetCategory() -> AnyPublisher<WordsetCategory, Error>
}
public final class WordsetCategoriesRepository: WordsetCategoryRepositoryProtocol {
@Inject private var service: WordsetCategoriesServiceProtocol