Skip to content

Instantly share code, notes, and snippets.

View yimajo's full-sized avatar
:octocat:

Yoshinori Imajo yimajo

:octocat:
  • Curiosity Software inc.
  • Tokyo, Japan
  • 17:14 (UTC +09:00)
View GitHub Profile
@yimajo
yimajo / RemoteConfig+addOnConfigUpdateListener.swift
Created May 26, 2023 10:58
Wrap Firestore Remote Config's addOnConfigUpdateListener method for use with AsyncThrowingStream.
import Foundation
import FirebaseRemoteConfig
extension RemoteConfig {
func addOnConfigUpdateListener() -> AsyncThrowingStream<Set<String>, Error> {
.init { continuation in
addOnConfigUpdateListener { configUpdate, error in
if let error {
continuation.finish(throwing: error)
} else {
import FirebaseFirestore
import FirebaseFirestoreSwift
import Foundation
// MARK: - CollectionReference
public extension CollectionReference {
@discardableResult
func add<T: Encodable>(
_ value: T,
@yimajo
yimajo / Query+AsyncTrowingStream.swift
Created March 17, 2023 15:22
Wrap Firestore's addSnapshotListner method for use with AsyncThrowingStream.
import FirebaseFirestore
// MARK: - async
extension Query {
func addSnapshotListener<T>(
includeMetadataChanges: Bool = false
) -> AsyncThrowingStream<[T], Error> where T: Decodable{
.init { continuation in
let listener = addSnapshotListener(includeMetadataChanges: includeMetadataChanges) { result in
@yimajo
yimajo / FirestoreClient.swift
Last active March 13, 2023 09:21
An example of using TCA FirestoreClient with Firestore ios sdk automatically bridged from Objective-C to Swift Concurrency.
import Foundation
import ComposableArchitecture
import FirebaseFirestore
import FirebaseFirestoreSwift
// MARK: - FirestoreClient
struct FirestoreClient {
enum Input {
enum Create {
@yimajo
yimajo / FirebaseAuthClient.swift
Last active August 5, 2023 03:24
Monitor FirebaseAuth login status with TCA
import ComposableArchitecture
import FirebaseCore
import FirebaseAuth
import GoogleSignIn
struct FirebaseAuthClient {
var observeSignInState: () async -> AsyncStream<Bool>
}
extension FirebaseAuthClient {
@yimajo
yimajo / Query+Result.swift
Created January 23, 2023 06:59
FirebaseFirestore Query Snapshot listener to Result conversion wrapper.
import FirebaseFirestore
@available(swift 5.0)
public extension Query {
func addSnapshotListener(
includeMetadataChanges: Bool = false,
listener: @escaping (Result<QuerySnapshot, Error>) -> ()
) -> some ListenerRegistration {
addSnapshotListener(includeMetadataChanges: includeMetadataChanges) { snapshot, error in
if let error {
@yimajo
yimajo / DispatchQueueLabel.swift
Created June 19, 2022 08:54
DispatchQueueをlabelで毎回初期化しても同じqueueが作られるわけではなくデータ競合を起こす
import Foundation
var count = 0
// 結果を保持させてすでに重複しているかを確認
var set = Set<Int>()
for item in 0..<10000 {
let queue = DispatchQueue(label: "fuga")
// データ競合以前にそもそもここでエラーになることもある。 error: Execution was interrupted.
// The process has been left at the point where it was interrupted, use "thread return -x" to return to the state before expression evaluation.
@yimajo
yimajo / swiftlee.di.swift
Created May 3, 2022 14:07
SwiftLeeのDIがとても良いので紹介したい
// https://www.avanderlee.com/swift/dependency-injection/
// MARK: - Example
struct DataController {
@Injected(\.networkProvider) var networkProvider: NetworkProviding
func performDataRequest() {
networkProvider.requestData()
}
@yimajo
yimajo / loan.swift
Last active November 29, 2021 13:56
クリーンアーキテクチャ本のLoanというEntityをSwiftで表現してみる
class Loan {
// 借入元金
private var principle: Double
// 貸出金利 もしくは 貸出利率 の 百分率。0%や100%
private var rate: Double
// 貸し出し期間
var period: TimeInterval
// 借金総額
private var balance: Double {
didSet {
@yimajo
yimajo / effective_swift_toc.md
Created November 21, 2021 12:33
Effective Swift本の目次と目的を考える

目的

Effective Swift本があるとしたなら、どんな目的で誰のための本?

  • 何のため
    • 言語の正しい理解と、簡潔で明瞭で正確なソフトウェアの設計に役立つ内容にしたい
  • どうやる
    • Effective〜な流儀に従って書籍スタイルは逆引きまとめスタイル
    • 言語仕様が常に進化しているSwiftの動向をしっかり押さえながら、それらの最新の機能をどのように使うべきかについて実践的な観点から逆引きにしてその技法をまとめる
  • 本当はどうあれば効果的?