Skip to content

Instantly share code, notes, and snippets.

View vinhnx's full-sized avatar
🎯
focusing

Vinh Nguyen vinhnx

🎯
focusing
View GitHub Profile
@vinhnx
vinhnx / auto_timestamp_build_number.sh
Created March 3, 2021 08:29
Increment build version on Release
#!/usr/bin/env bash
# docs: https://docs.microsoft.com/en-us/appcenter/build/custom/scripts/
# Increment build version on Release
# format: %Y%m%d.%H%M
# reference: https://gist.github.com/sekati/3172554
if [ "${CONFIGURATION}" = "Release" ]; then
echo "----"
buildNumber=$(TZ="Asia/Ho_Chi_Minh" date +%Y%m%d.%H%M)
echo "Build # set to: $buildNumber"
@vinhnx
vinhnx / SwiftUI_MVVM.swift
Created March 2, 2021 11:04
SwiftUI with MVVM example
// https://kean.blog/post/swiftui-data-flow
struct SearchView: View {
@ObservedObject var viewModel: SearchViewModel
var body: some View {
VStack {
TextField("Search", text: $viewModel.query)
List(viewModel.songs) {
Text($0.name)
}
@vinhnx
vinhnx / simple_remote_config.swift
Created December 13, 2020 01:54
simple remote config loader with persistent (default config if loads via network fails). Reference : https://www.donnywals.com/building-a-simple-remote-configuration-for-your-apps/
class LocalConfigLoader: LocalConfigLoading {
private var cachedConfigUrl: URL? {
guard let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else {
return nil
}
return documentsUrl.appendingPathComponent("config.json")
}
private var cachedConfig: AppConfig? {
//Orginal code from: https://gist.github.com/mecid/f8859ea4bdbd02cf5d440d58e936faec
//I just made some modification in appearnce, show monthly navigator and weekdays.
import SwiftUI
struct ContentView: View {
@Environment(\.calendar) var calendar
private var year: DateInterval {
calendar.dateInterval(of: .month, for: Date())!
@vinhnx
vinhnx / keyboard_avoiding.swift
Created October 28, 2020 04:08
iOS Swift keyboard avoiding
let notificationCenter = NotificationCenter.default
notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIResponder.keyboardWillHideNotification, object: nil)
notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
@objc func adjustForKeyboard(notification: Notification) {
guard let keyboardValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else { return }
let keyboardScreenEndFrame = keyboardValue.cgRectValue
let keyboardViewEndFrame = view.convert(keyboardScreenEndFrame, from: view.window)
@vinhnx
vinhnx / Diffable+Compositional.swift
Last active October 29, 2020 10:13
Diffable datasource and compositional layout
Diffable datasource and compositional layout:
+ https://www.donnywals.com/modern-table-views-with-diffable-data-sources/
+ https://www.swiftbysundell.com/articles/building-modern-collection-views-in-swift/
> **UICollectionViewDiffableDataSource** let us compute UICollectionView datasource and configuration, as a replacement for plain old UICollectionViewDatasource/UICollectionViewDelegate. We also have table view counter part, they are **UITableViewDiffableDataSource**.
> we can now use **UICollectionViewComposableLayout** as replacement for **UITableView**.
```swift
func makeCollectionView() -> UICollectionView {
@vinhnx
vinhnx / latency.txt
Created October 18, 2020 06:19 — forked from jboner/latency.txt
Latency Numbers Every Programmer Should Know
Latency Comparison Numbers (~2012)
----------------------------------
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns 14x L1 cache
Mutex lock/unlock 25 ns
Main memory reference 100 ns 20x L2 cache, 200x L1 cache
Compress 1K bytes with Zippy 3,000 ns 3 us
Send 1K bytes over 1 Gbps network 10,000 ns 10 us
Read 4K randomly from SSD* 150,000 ns 150 us ~1GB/sec SSD
@vinhnx
vinhnx / Fastfile
Last active June 24, 2022 04:43
advanced Fastfile config
# 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
#
# Uncomment the line if you want fastlane to automatically update itself
# update_fastlane
@vinhnx
vinhnx / Mocking.swift
Created September 18, 2019 07:27
Mocking in Swift
// mocking in swift
// reference: https://www.vadimbulavin.com/real-world-unit-testing-in-swift/
struct User {}
class AuthService {
var client: HTTPClient
// init
init(_ client: HTTPClient) {
@vinhnx
vinhnx / GitHubAPISingle.swift
Created August 20, 2019 08:31
Simple RxSwift's usage of Single for async network events
enum GitHubAPIError: Error {
case userIsEmpty
case invalidData
}
func fetchGithubUser(_ user: String) -> Single<[String: Any]> {
guard user.isEmpty == false else { return Single.error(GitHubAPIError.userIsEmpty) }
return Single<[String: Any]>.create(subscribe: { completion in
let task = URLSession.shared
.dataTask(with: URLRequest(url: URL(string: "https://api.github.com/users/\(user)")!), completionHandler: { data, _, error in