Skip to content

Instantly share code, notes, and snippets.

View michaelevensen's full-sized avatar

Michael Nino Evensen michaelevensen

View GitHub Profile
import Foundation
struct EvolutionProposal: Codable {
var id: String
var title: String
var reviewStartDate: Date
var reviewEndDate: Date
enum CodingKeys: String, CodingKey {
@michaelevensen
michaelevensen / CustomDecoding.swift
Created March 21, 2023 09:21
Simple custom JSON decoding in Swift for specific keys. Hot tip is to nest the CodingKeys similarly to how the JSON is nested.
struct Data: Decodable {
var age: Int
var address: Address
struct Address: Decodable {
var street: String
var city: String
}
private enum CodingKeys: String, CodingKey {
@michaelevensen
michaelevensen / GenericStyleExtension.swift
Last active March 20, 2023 11:51
Generics in extensions
struct InputFieldButtonStyle<Content: View>: ButtonStyle {
@Environment(\.colorScheme) var colorScheme
let mimicksDropdown: Bool
let placeholderContent: () -> Content?
init(
mimicksDropdown: Bool = false,
@ViewBuilder placeholderContent: @escaping () -> Content? = { nil }
struct GridView: View {
var lineWidth: CGFloat = 1.5
var color = Color(
.displayP3,
red: 225/255,
green: 225/255,
blue: 225/255,
opacity: 1.0
@michaelevensen
michaelevensen / CircleView.swift
Created January 18, 2023 21:08
Draws a funky Circle. Probably better to do this as a custom `Shape`?
struct CircleView: View {
@State var spacing: Double = 12
var radius: Double = 250
var body: some View {
GeometryReader { proxy in
let center = CGPoint(x: proxy.frame(in: .local).midX, y: proxy.frame(in: .local).midY)
Path { path in
@michaelevensen
michaelevensen / ViewBuilder.swift
Last active January 5, 2023 21:15
Use `@ViewBuilder` in extensions. Basically cast to `AnyView`.
extension ButtonStyle where Self == InputFieldButtonStyle<AnyView> {
internal static func input<Content: View>(mimicksDropdown: Bool = false,
@ViewBuilder placeholderContent: @escaping () -> Content? = { nil }) -> InputFieldButtonStyle<AnyView> {
InputFieldButtonStyle(mimicksDropdown: mimicksDropdown,
placeholderContent: {
AnyView(placeholderContent())
})
}
}
@michaelevensen
michaelevensen / EditView.swift
Last active January 18, 2023 21:15
An example of data edit flow in SwiftUI. Using Apple's examples.
//
// BindingTest.swift
// MutatingState
//
// Created by Michael Nino Evensen on 21/12/2022.
//
import SwiftUI
struct Object: Identifiable, Hashable {
@michaelevensen
michaelevensen / MasterDetailEdit.swift
Created December 21, 2022 12:36
Example of how you can create a Master Detail data editing and creation flow in SwiftUI.
//
// BindingTest.swift
// MutatingState
//
// Created by Michael Nino Evensen on 21/12/2022.
//
import SwiftUI
struct Object: Identifiable {
@michaelevensen
michaelevensen / CustomEncodingDecoding.swift
Last active March 21, 2023 08:56
Custom encoding and decoding of JSON data. Wouldn't necessary recommend this as your models should mirror your API.
import Swift
import Foundation
let json = """
{
"amount": 3,
"confirmed": false,
"context": {
"wishlist": {
"mTy7R1Y7OI22w1jAqKvo": {
@michaelevensen
michaelevensen / Example.swift
Created December 8, 2022 12:19 — forked from gromwel/Example.swift
Decoding and encoding JSON with dynamic keys.
import Foundation
struct Film: Codable {
let actor: String
let year: Int
let key: String
// Объявляем ключи только для тех которые внутри модели
enum CodingKeys: CodingKey {
case actor