Skip to content

Instantly share code, notes, and snippets.

View codecat15's full-sized avatar

Codecat15 codecat15

View GitHub Profile
@codecat15
codecat15 / TCA_LoginExample.swift
Last active September 29, 2023 15:57
TCA example 1
enum AppAction {
case loginButtonTapped
case logoutButtonTapped
// ... other actions
}
struct AppState {
var isUserLoggedIn: Bool
// ... other state properties
}
@codecat15
codecat15 / BuilderPattern.swift
Created September 9, 2023 22:51
Builder Pattern for creating instance of ViewModel with Dependency Injection
// Dependency Inversion Principle: Define protocols for Service and Repository
protocol Service {
func fetchData() -> String
}
protocol Repository {
func saveData(data: String)
}
@codecat15
codecat15 / FactoryDesignPatternExample.swift
Created September 9, 2023 22:48
Swift Factory Design Pattern with Dependency Inversion Principle
// Define protocols for Service and Repository
protocol Service {
func fetchData() -> String
}
protocol Repository {
func saveData(data: String)
}
@codecat15
codecat15 / HttpUtility.swift
Created August 8, 2023 15:30
SwiftUI+ MVVM: This Swift code demonstrates a SwiftUI-based movie listing app. It employs the MVVM architecture pattern for clear separation of concerns and code reusability
import Foundation
final class HttpUtility {
static let shared = HttpUtility()
func get<T: Decodable>(endpoint: String) async throws -> T {
let url = URL(string: "https://example.com/api/\(endpoint)")!
let (data, _) = try await URLSession.shared.data(from: url)
return try JSONDecoder().decode(T.self, from: data)
}
@codecat15
codecat15 / CustomJSONDecoding.swift
Created December 19, 2022 15:10
Custom Decoding when all JSON properties are received as string from API response
import Foundation
// MARK: Project model
struct Project: Decodable {
let id: Int
let name: String
let teamId: Int
let isActive: Bool
enum CodingKeys: String, CodingKey {
@codecat15
codecat15 / LoginServiceImplementation.swift
Last active October 7, 2022 16:58
Dependency injection example of a service in MVVM
// LoginService Protocol
protocol LoginService: AnyObject {
func authenticateUser(request: LoginRequest) - > UserResponse
}
// Concrete implementation
class LoginServiceImplementation: LoginService {
func authenticateUser(request: LoginRequest) - > UserResponse {
// create the url request
@codecat15
codecat15 / UTCDateFormatter.swift
Created April 19, 2022 03:06
Format UTC date string using DateFormatter
let dateString = "2022-04-12T18:41:20.000000Z"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let result = dateFormatter.date(from: dateString)
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
dateFormatter.timeZone = TimeZone(identifier: "UTC")
@codecat15
codecat15 / serialApiCallWithDispatchGroup.swift
Last active March 28, 2021 15:21
using combination of dispatch group and operation queue to wait for URLSession
// let's say the project operation needs to be executed first before the employee
let group = DispatchGroup()
// employee block operation
let employeeBlockOperation = BlockOperation()
employeeBlockOperation.addExecutionBlock {
let employeeDataResource = EmployeeDataResource()
employeeDataResource.getEmployee { (employees) in
@codecat15
codecat15 / SerialApiCall.swift
Last active March 28, 2021 02:46
Calling api serially in swift
// suppose we want to call two apis one after the other then in that case we can use combination of operation queue and dispatch group
func syncDataResources()
{
let group = DispatchGroup()
// employee block operation
let employeeBlockOperation = BlockOperation()
employeeBlockOperation.addExecutionBlock {
@codecat15
codecat15 / OperationQueue_WithApi.swift
Created March 15, 2021 16:20
using api with operation queue dependencies
import UIKit
// MARK: - SyncManager
struct SyncManager
{
func GetRecordsInSync()
{
debugPrint("inside the GetRecordsInSync")
let animalOperation = BlockOperation()