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 / FileLogger.swift
Last active June 6, 2024 05:38
Log to a file, then use `copyLogToClipboard()` or `printLog()` function to read it.
import Foundation
import UIKit
class FileLogger {
private static let defaultFileName = "log.txt"
static let shared = FileLogger()
private init() {
read()
}
@huynguyencong
huynguyencong / AspectRatioImage.swift
Created April 16, 2024 04:16
A way to put an image in a parent view, filled in an area with a specific aspect ratio
VStack {
Color.clear
.aspectRatio(16/9, contentMode: .fit)
.overlay {
Image(systemName: "icon")
.resizable()
.scaledToFill()
}
.clipped()
import SwiftUI
struct ReadPositionModifier<Key: PreferenceKey>: ViewModifier where Key.Value == CGRect {
let parentCoordinateSpaceName: String
func body(content: Content) -> some View {
content
.background {
GeometryReader { proxy in
Color.clear
@huynguyencong
huynguyencong / #BuildiOSWithGitHubAction.md
Last active September 11, 2024 11:15
Script to build iOS and deploy it to TestFlight using GitHub action

When merging code to build/test-flight branch, it is built and uploaded to TestFlight automatically by GitHub Action.

Set up/update the following secrets:

  • CERTIFICATES_FILE_BASE64: Base64 of the App Store distribution certificate.
  • CERTIFICATES_PASSWORD: App Store distribution certificate password.
  • APPSTORE_ISSUER_ID: App Store Connect API key's issuer ID.
  • APPSTORE_KEY_ID: App Store Connect API key's key ID.
  • APPSTORE_PRIVATE_KEY: App Store Connect API key's private key (raw p8 file).
@huynguyencong
huynguyencong / FlutterGitHooks.md
Created July 10, 2023 03:56
Flutter project's githook
  • Create a .githooks directory in the root directory.
  • Add githook files to it.
  • Ask developers to run this command to use those githooks.
git config core.hooksPath .githooks
@huynguyencong
huynguyencong / PassDynamicDataFromParentToChildView.swift
Created May 29, 2023 10:06
Pass dynamic data from parent view to child view in SwiftUI using MVVM architecture.
import SwiftUI
import Combine
struct ContentView: View {
var body: some View {
ParentView()
}
}
struct ParentView: View {
@huynguyencong
huynguyencong / CounterPublisher.swift
Created April 4, 2023 22:03
Custom Publisher and Subscriber with Combine framework
import Combine
/// A publisher that emit counter number every
class CounterPublisher: Publisher {
typealias Output = Int
typealias Failure = Error
private let completion: Completion?
init(completion: Completion? = nil) {
@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))
}
@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 / 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()