Skip to content

Instantly share code, notes, and snippets.

View vinhnx's full-sized avatar
🍀
learn by doing

Vinh Nguyen vinhnx

🍀
learn by doing
View GitHub Profile
import Foundation
public protocol NetworkLoggable {
func log<T: CustomStringConvertible>(
label: String,
value: T?,
level: NetworkLogger,
function: StaticString,
line: UInt,
file: String
@vinhnx
vinhnx / packages.json
Created September 19, 2021 02:28
Swift Package Index
{"packages":[{"url":"https:\/\/github.com\/vinhnx\/DictionaryNestedSubscript.git","versions":[{"license":{"url":"https:\/\/github.com\/vinhnx\/DictionaryNestedSubscript\/blob\/master\/LICENSE","name":"MIT"},"defaultToolsVersion":"5.1.0","verifiedCompatibility":[{"swiftVersion":"5.1","platform":{"name":"ios"}},{"swiftVersion":"5.2","platform":{"name":"ios"}},{"swiftVersion":"5.3","platform":{"name":"ios"}},{"swiftVersion":"5.4","platform":{"name":"ios"}},{"swiftVersion":"5.5","platform":{"name":"ios"}},{"swiftVersion":"5.1","platform":{"name":"linux"}},{"swiftVersion":"5.2","platform":{"name":"linux"}},{"swiftVersion":"5.3","platform":{"name":"linux"}},{"swiftVersion":"5.4","platform":{"name":"linux"}},{"swiftVersion":"5.5","platform":{"name":"linux"}},{"swiftVersion":"5.1","platform":{"name":"macos"}},{"swiftVersion":"5.2","platform":{"name":"macos"}},{"swiftVersion":"5.3","platform":{"name":"macos"}},{"swiftVersion":"5.4","platform":{"name":"macos"}},{"swiftVersion":"5.5","platform":{"name":"macos"}},{"swift
@vinhnx
vinhnx / bump_build_version_with_timestamp
Created April 12, 2021 02:45
Xcode bump build version with current timestamp
agvtool new-version -all $(TZ="Asia/Ho_Chi_Minh" date +%Y%m%d.%H%M)
@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 {