Skip to content

Instantly share code, notes, and snippets.

View yosshi4486's full-sized avatar

yosshi4486 yosshi4486

View GitHub Profile
@yosshi4486
yosshi4486 / CustomOperatorSample.swift
Last active March 22, 2020 07:37
Make custom operator without subscription for Combine.
import Combine
public struct Double<Upstream,Output>: Publisher where Upstream:Publisher, Output : Numeric, Upstream.Output == Output {
public typealias Failure = Upstream.Failure
/// The upstream publisher.
public let upstream: Upstream
public init(upstream: Upstream) {
self.upstream = upstream
@yosshi4486
yosshi4486 / MANIFEST.md
Last active April 5, 2020 08:01
宣言的UI時代のアプリケーション開発マニフェスト

iOSのCombine/SwiftUIや、AndroidのJetpackComponentなど、宣言的記法を利用したパラダイムがモバイルアプリ開発で当たり前になろうとしている。 考え方をそもそも変えないといけない場面が出そうなので、gistに残しておく。今はただの考えにすぎないので、実装出来てきたらレポジトリやQiitaで公開する

1. 変更はやってくる

今まではRepositoryに対してgetを投げて、非同期のクロージャで結果を返して...。のようにしていたが、 宣言的パラダイムでは変更はSubscribeしており、流れきてたら自動で更新がなされる。getのようなリクエストを送ることはない。

実現のためにポーリングなどが必要な場合も、データレイヤーでカプセル化する。

2. ライフサイクルに頼らない

@yosshi4486
yosshi4486 / AppleStylePreviewView.swift
Last active July 9, 2020 03:09
Previews that apple showed in wwdc20 "Intdoduce SwiftUI" session. We can test dynamic-type, dark-mode, right-to-left language and location by using it.
import SwiftUI
/// Previews that apple show in wwdc20 "Intdoduce SwiftUI" session. We can test dynamic-type, dark-mode, right-to-left language and location by using it.
struct AppleStylePreviewView<Content : View>: View {
var childView: Content
init(@ViewBuilder builder: () -> (Content)) {
self.childView = builder()
}
@yosshi4486
yosshi4486 / CustomNewLineTextView.swift
Created October 23, 2020 09:33
CustomNewLineTextView is able to make new line by shift + enter.
//
// CustomNewLineTextView.swift
// LifeList
//
// Created by seijin4486 on 2020/10/23.
//
import UIKit
/// Shift + Enterで改行可能なTextView
@yosshi4486
yosshi4486 / Sample.swift
Created January 3, 2021 03:46
UITextView on UITableViewCellの実装例
func textViewDidChange(_ textView: UITextView) {
// https://www.damienpontifex.com/posts/self-sizing-uitableviewcell-with-uitextview-in-ios8/
// UITextView on UITableViewCellの良い解説記事
let size = textView.bounds.size
let newSize = textView.sizeThatFits(CGSize(width: size.width, height: CGFloat.greatestFiniteMagnitude))
// 上記のheightを直接比較すると0.5ptぐらい誤差が出てうまく処理を弾けないことがあるので
// 判定範囲を広げた
let difference = abs(newSize.height - size.height)
@yosshi4486
yosshi4486 / ErrorCast.swift
Last active April 1, 2021 01:58
What happen when casting Swift.Error or Swift.LocalizedError to NSError?
// Try them in Playground file.
/* Background when executing cast(swiftError as NSError)
1. NSError uses Swift.Error type name as its `domain` property.
2. NSError uses a eumeration number as its `code` property.
*/
import UIKit
enum SwiftError: Error {
@yosshi4486
yosshi4486 / TestEnvironment.swift
Last active April 11, 2021 00:42
テストターゲットとメインターゲットの両方から読み込んでテスト環境値の注入を行うフレームワークのファイルサンプル
// .gitignoreで該当のtestEnvironmentBackingStore.jsonファイルだけ取り除いておくようにする。
// ProcessInfoを使わずに値をメインターゲットに渡したかったので考えてみた。各種設定項目をいじることで流用可能。
// これをProcessInfoの代わりに、TestEnvironment.sharedとして値を取り出すようにすれば、ちょっとマシになるかなと。
// フレームワーク自体にアプリケーションのテストに関する知識が書き込まれているので、OSS化は難しい?
// 参考にしたのはSKTestSessionの設計
//
// TestEnvironment.swift
// BulistsTestEnvironment
//
@yosshi4486
yosshi4486 / Accessibility.swift
Created April 13, 2021 11:24
カスタムView(タスクチェックボタン)のアクセシビリティ対応
// 画像のサイズよりもタップ領域を広げたいので、contentViewを別途設けており、それのアクセシビリティ要素にしたい
taskContentView.isAccessibilityElement = true
taskContentView.accessibilityLabel = NSLocalizedString("Task Status", comment: "")
taskContentView.accessibilityCustomActions = [
    UIAccessibilityCustomAction(name: NSLocalizedString("Toggle task status", comment: ""), actionHandler: { [weak self] (action) -> Bool in
self?.toggleDone(action)
return true
    })
]
@yosshi4486
yosshi4486 / AccessibleDate.swift
Created April 14, 2021 05:32
日付表示のアクセシビリティ
private func accessibilityLabelForCreatedDateLabel(from date: Date) -> String {
return String(format: NSLocalizedString("Created: %@", comment: "An accessibility label of created date."), self.accessibilityLabel(for: date) ?? "")
}
private func accessibilityLabelForUpdatedDateLabel(from date: Date) -> String {
return String(format: NSLocalizedString("Updated: %@", comment: "An accessibility label of updated date."), self.accessibilityLabel(for: date) ?? "")
}
private func accessibilityLabel(for date: Date) -> String? {
let timeComponentFormatter = DateComponentsFormatter()
@yosshi4486
yosshi4486 / FirstRelease.swift
Created April 23, 2021 23:16
ファーストリリースに関しての自分の脳内モデル
struct Feature {
var title: String
}
extension Feature: CustomStringConvertible {
var description: String {
return title
}