Skip to content

Instantly share code, notes, and snippets.

View yukin01's full-sized avatar

Yuji Kinjo yukin01

  • Tokyo / Japan
View GitHub Profile
@yukin01
yukin01 / RxRequest.swift
Last active April 29, 2023 20:31
Simple APIClient with RxSwift + URLSession + Codable
protocol RxRequestProtocol {
func get<U: Decodable>(returnType: U.Type, path: String, query: [String: String]?, token: String?) -> Observable<U>
func get(path: String, query: [String: String]?, token: String?) -> Observable<Void>
func post<T: Encodable, U: Decodable>(returnType: U.Type, path: String, query: [String: String]?, body: T, token: String?) -> Observable<U>
func post<T: Encodable>(path: String, query: [String: String]?, body: T, token: String?) -> Observable<Void>
func put<T: Encodable, U: Decodable>(returnType: U.Type, path: String, query: [String: String]?, body: T, token: String?) -> Observable<U>
func put<T: Encodable>(path: String, query: [String: String]?, body: T, token: String?) -> Observable<Void>
func delete<U: Decodable>(returnType: U.Type, path: String, query: [String: String]?, token: String?) -> Observable<U>
func delete(path: String, query: [String: String]?, token: String?) -> Observable<Void>
@yukin01
yukin01 / synchronize.swift
Created December 30, 2018 13:53
Single to Taple in RxSwift
func synchronize<T>(_ single: Single<T>) -> (element: T?, error: Error?) {
var element: T?
var error: Error?
let condition = NSCondition()
condition.lock()
_ = single.subscribe({ event in
condition.lock()
switch event {
case .success(let elm): element = elm
case .error(let err): error = err
@yukin01
yukin01 / LoggerLikeRx.swift
Created February 6, 2019 08:22
RxSwift の Debug に寄せたロガー(メインスレッド判定もできる)
enum Logger {
static let dateFormatter: DateFormatter = {
let df = DateFormatter()
df.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS"
return df
}()
static func log(_ item: String, file: String = #file, line: UInt = #line, function: String = #function) {
printLikeRxDebug(item, file, line, function)
}
static func isMainThread(file: String = #file, line: UInt = #line, function: String = #function) {
@yukin01
yukin01 / SingleSectionModel.swift
Created March 4, 2019 10:18
RxDataSources+SingleSectionModel
import Foundation
import Differentiator
struct SingleSectionModel<ItemType: Equatable> {
var items: [ItemType]
init(items: [ItemType]) {
self.items = items
}
}
@yukin01
yukin01 / ClipToRoundView.swift
Created April 4, 2019 15:40
ClipToRoundView
@IBDesignable
final class ClipToRoundView: UIView {
@IBInspectable
var fillColor: UIColor = .white {
didSet {
shapeLayer.fillColor = fillColor.cgColor
}
}
@yukin01
yukin01 / DesignableView.swift
Last active February 23, 2021 05:11
Add @IBDesignable and @IBInspectable to UIView, UIButton, UIImageView and UITextField
import UIKit
protocol Roundable {
var cornerRadius: CGFloat { get set }
}
protocol Borderable {
var borderWidth: CGFloat { get set }
var borderColor: UIColor? { get set }
}
protocol Shadowable {
@yukin01
yukin01 / Slackfile
Created April 13, 2019 12:33
Custom Slack Action for Fastlane
# vim: syntax=ruby
private_lane :custom_slack do |options|
ENV["TZ"] = "Asia/Tokyo"
slack(
message: options.fetch(:message, "No messages"),
success: options.fetch(:success, true),
slack_url: "hoge",
payload: {
"Build Date" => Time.new.to_s,
var inputLines = [String]()
while let line = readLine() {
inputLines.append(line)
}
let rows = inputLines.dropFirst()
rows.forEach { print($0) }
@yukin01
yukin01 / sharp-sample01.js
Last active May 23, 2019 06:33
1024 * 1024 を超えないようにリサイズ(トリミングなし、拡大なし)
const sharp = require('sharp')
const path = require('path')
const filePath = './image.png'
const fileName = path.basename(filePath, path.extname(filePath))
sharp(filePath)
.resize(1024, 1024, {
fit: sharp.fit.inside,
withoutEnlargement: true
@yukin01
yukin01 / rezip.ts
Last active October 12, 2019 18:33
Remove root directory from zip file
import { readFile, writeFile } from 'fs'
import { promisify } from 'util'
import * as JSZip from 'jszip'
const main = async () => {
const oldPath = 'old.zip'
const oldBuf = await promisify(readFile)(oldPath)
const oldZip = await JSZip.loadAsync(oldBuf)
const files = oldZip.files