Skip to content

Instantly share code, notes, and snippets.

@sakiwei
sakiwei / KeyboardObserver.swift
Created June 26, 2024 22:58
A super basic version of keyboard avoid logic in swiftui
import UIKit
import SwiftUI
import Combine
final class KeyboardObserver: ObservableObject {
@Published var keyboardHeight: CGFloat = 0
@Published var isVisible = false
var keyboardCancellable = Set<AnyCancellable>()
@sakiwei
sakiwei / Language - string
Last active March 30, 2021 02:59
Language - string
const localizationContext = useLocalizationContext()
const t = localizationContext.t
t('Language', {})
class MainActivity : LifecycleActivity() {
// create a LifecycleCompositeDisposable which will be disposed at onPause()
val disposableBag by lazy { createOnPauseCompositeDisposable() }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// keep firing a number on each second,
@sakiwei
sakiwei / RxTTTAttributedLabel.swift
Last active December 12, 2018 10:16
A sample of creating custom delegate for RxSwift [Reference: http://blog.edenmsg.com/rxswift-migrate-delegates-to-beautiful-observables/]
import TTTAttributedLabel
import RxSwift
import RxCocoa
fileprivate class RxTTTAttributedLabelDelegateProxy: DelegateProxy, TTTAttributedLabelDelegate, DelegateProxyType {
//We need a way to read the current delegate
static func currentDelegateFor(_ object: AnyObject) -> AnyObject? {
let label: TTTAttributedLabel = object as! TTTAttributedLabel
return label.delegate
}
@sakiwei
sakiwei / UICollectionViewExtension.swift
Created September 23, 2016 04:47
Get UICollectionViewCell viewport percentage
import UIKit
extension UICollectionView {
var cellOffsetForVisibleItems: [IndexPath: CGFloat] {
let result = self.indexPathsForVisibleItems
.map { indexPath in
[indexPath: UICollectionView.cellOffset(with: self, at: indexPath)]
}
.reduce([IndexPath: CGFloat]()) { result, map in
var newResult = result
import Foundation
// ref: http://stackoverflow.com/questions/3707427/how-to-read-data-from-nsfilehandle-line-by-line
class FileReader {
private let fileHanlde: NSFileHandle
private let lineDelimiter = "\n"
private let chunkSize: Int = 16
private let totalFileLen: UInt64
private var currentOffset: UInt64 = 0
class FileReader {
// 1.
private let fileHanlde: NSFileHandle
private let lineDelimiter = "\n"
private let chunkSize: Int = 16
private let totalFileLen: UInt64
private var currentOffset: UInt64 = 0
// 2.
extension NSData {
func rangeOfData_dd(dataToFind: NSData) -> NSRange {
let bytes = self.bytes
let len = self.length
let searchBytes = dataToFind.bytes
let searchLen = dataToFind.length
var searchIndex = 0
@interface NSData (DDAdditions)
- (NSRange) rangeOfData_dd:(NSData *)dataToFind;
@end
@implementation NSData (DDAdditions)
- (NSRange) rangeOfData_dd:(NSData *)dataToFind {
@sakiwei
sakiwei / convertImages.sh
Last active September 18, 2018 08:58
Convert 3x scaled images to Android drawable resources
#!/bin/bash
# images (@3x) to
# drawable-xxhdpi(3x),
# drawable-xhdpi(2x),
# drawable-mdpi(1x)
# Usage
# -------