Skip to content

Instantly share code, notes, and snippets.

View huynguyencong's full-sized avatar

Huy Nguyen huynguyencong

  • Ho Chi Minh City, Vietnam
View GitHub Profile
@huynguyencong
huynguyencong / Binding+Ex.swift
Last active October 4, 2020 13:42
Binding extensions:
import SwiftUI
extension Binding {
func map<MappedValue>(
valueToMappedValue: @escaping (Value) -> MappedValue,
mappedValueToValue: @escaping (MappedValue) -> Value
) -> Binding<MappedValue> {
Binding<MappedValue>.init { () -> MappedValue in
return valueToMappedValue(wrappedValue)
} set: { mappedValue in
//
// CalendarView.swift
// iWidget
//
// Created by Huy Nguyen on 10/2/20.
//
import SwiftUI
fileprivate extension DateFormatter {
@huynguyencong
huynguyencong / CodableTransformerPropertyWrapper.swift
Last active May 28, 2021 10:15
Use property wrapper to reduce meaningless code in a decodable, when you need to customize how to parse just 1 key, but have to implement all keys. It makes decoding a custom key using Codable is short and simple like TransformerOf of ObjectMapper open source.
// IMPORTANCE: I don't use this anymore, because it has problem with a json that missing key. In normal, we can ignore a missing key by set a property optional. But in this case, setting property optional still causes no key error.
/* Huy Nguyen
Use property wrapper to reduce meaningless code in a decodable, when you need to customize how to parse just 1 key, but have to implement all keys.
This snippet assumes you need to decode some models that currency values are Strings with "$" prefix ("$20.3" to 20.3), and you want to parse them to Double values.
For example, the JSON of Product model:
{
"price": "$20.3",
@huynguyencong
huynguyencong / Fastfile.rb
Created January 13, 2021 08:15
Build iOS app and deliver it to an app manager with Fastlane
# This file contains the fastlane.tools configuration
# You can find the documentation at https://docs.fastlane.tools
#
# For a list of all available actions, check out
#
# https://docs.fastlane.tools/actions
#
# For a list of all available plugins, check out
#
# https://docs.fastlane.tools/plugins/available-plugins
@huynguyencong
huynguyencong / github-action-on-pull-request.yml
Created January 13, 2021 10:33
Github action: On pull request to master, develop, release branch, run Test and Swiftlint
name: On pull request
on:
pull_request:
branches:
- master
- develop
jobs:
test:
@huynguyencong
huynguyencong / github-action-on-push-release.yml
Last active January 13, 2021 10:48
Github action: On push to release branch, test and build.
# Set ADHOC_CERTIFICATE base64, ADHOC_PROVISIONING base64, ADHOC_CERTIFICATE_PASSWORD to Github secrets
# - File and base64:
# base64 -i <any-file> -o <text-file>
# base64 -D -i <text-file> -o <any-file>
name: Release using fastlane
on:
push:
branches:
@huynguyencong
huynguyencong / CodableTransformer.swift
Last active September 27, 2022 06:08
A way to implement transformer when using Codable
import Foundation
// Assume the server return `Int` number in a `String`. If it happen on all models, we can't customise all models with `init(decoder:)` and `encode(encoder:)`, because it will make the code a mess.
// Conform this protocol to implement transformation
protocol Transformer {
associatedtype Origin: Codable
associatedtype Target
static func decode(value: Origin) throws -> Target
@huynguyencong
huynguyencong / ContentSizeCollectionView.swift
Created November 2, 2021 11:39
A collection view that has intrinsic content size is its content size
class ContentSizeCollectionView: UICollectionView {
var contentSizeObservation: NSKeyValueObservation?
override var intrinsicContentSize: CGSize {
return shouldFullSize ? contentSizeWithInset : CGSize(width: UIView.noIntrinsicMetric, height: UIView.noIntrinsicMetric)
}
var shouldFullSize: Bool = true {
didSet {
invalidateIntrinsicContentSize()
@huynguyencong
huynguyencong / XibLoaderView.swift
Created February 10, 2022 10:20
A class that helps us to create a custom view from XIB file easier
//
// XibLoaderView.swift
//
//
// Created by Nguyen Cong Huy on 20/11/2021.
//
import UIKit
/**
@huynguyencong
huynguyencong / MeasurementModifier.swift
Created September 30, 2022 07:44
Measure a view size with GeometryReader but doesn't take all spaces from parent view
import SwiftUI
struct MeasurementModifier: ViewModifier {
@Binding var isEnabled: Bool
let sizeChanged: (CGSize) -> Void
func body(content: Content) -> some View {
content.background(MeasurementView(isEnabled: $isEnabled, sizeChanged: sizeChanged))
}