Skip to content

Instantly share code, notes, and snippets.

View brownsoo's full-sized avatar
🐢
Step by step

brownsoo brownsoo

🐢
Step by step
View GitHub Profile
@brownsoo
brownsoo / scrollView-snap.swift
Created July 25, 2018 02:32
UITableView snap tp cells
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
if scrollView == suggestionsTableView {
let cellHeight = CGFloat(60.0)
let y = targetContentOffset.pointee.y + scrollView.contentInset.top + (cellHeight / 2)
let cellIndex = floor(y / cellHeight)
targetContentOffset.pointee.y = cellIndex * cellHeight - scrollView.contentInset.top
}
}
@brownsoo
brownsoo / rx_double_back_key_to_exit.kt
Created October 1, 2018 01:23
Implementation Double back key pressed to exit Using Rx
data class BackKeyPressedTimePair(val prev: Long, val curr: Long)
private var backSubject = PublishSubject.create<Boolean>()
// onBackPressed()
backSubject.throttleFirst(100, TimeUnit.MILLISECONDS)
.observeOn(uiScheduler)
.filter { isAvailable }
.map {
// if there is BottomSheet, hide that.
@brownsoo
brownsoo / stream_based_item_route.dart
Last active January 5, 2021 02:38
dart stream sample
class Item {
Item({this.itemId});
final String itemId;
StreamController<Item> _controller = StreamController<Item>.broadcast();
Stream<Item> get onChanged => _controller.stream;
String _status;
String get status => _status;
set status(String value) {
@brownsoo
brownsoo / handle_blank_target.kt
Last active February 21, 2020 06:48
Android WebView handling _blank target link
override fun onCreateWindow(view: WebView?, isDialog: Boolean,
isUserGesture: Boolean, resultMsg: Message?): Boolean {
val context = view?.context ?: return false
val transport = resultMsg?.obj as? WebViewTransport
val newWebView = WebView(context)
view.addView(newWebView)
transport?.webView = newWebView
resultMsg?.sendToTarget()
newWebView.webViewClient = object : WebViewClient() {
@brownsoo
brownsoo / MakingPasswordDialog.kt
Created April 16, 2020 02:50
Making a input password dialog programmatically
// activity with android:windowSoftInputMode="stateHidden"
private fun showPasswordDialog() {
val context = this.context ?: return
val editText = EditText(context)
editText.inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_VARIATION_PASSWORD
editText.filters = arrayOf(InputFilter.LengthFilter(4))
val frame = FrameLayout(context)
frame.addView(editText)
@brownsoo
brownsoo / water_2d_effect.js
Last active January 5, 2021 01:37
water 2d effect
// Hugo Elias Water 2D Effect
let COLUMNS = 40;
let ROWS = 40;
let BLOCKS = COLUMNS * ROWS;
let LEVEL = 50;
let HIGH_LEVEL = LEVEL * 2;
let HUE = 210;
let SATU = 95;
@brownsoo
brownsoo / publisher_sequence.swift
Created January 5, 2021 02:42
Combine publishers to sequence result
func fetchCoachingRoom(complete: @escaping (Error?) -> Void) {
refreshCoachingRoomStatus = .working
cancellableCoachingRoom?.cancel()
cancellableCoachingRoom = apiService.stream(Api.getCoachingRoom())
.map({ (res) -> [AClass] in
return res.userClassList
})
.receive(on: DispatchQueue.main)
.map { [weak self] list -> [Int] in
self?.coachingRoomClasses = list
@brownsoo
brownsoo / Scheduling an Action.java
Created August 13, 2021 05:31
Scheduling an Action
Scheduler scheduler = Schedulers.immediate();
Scheduler.Worker worker = scheduler.createWorker();
worker.schedule(() -> result += "action");
Assert.assertTrue(result.equals("action"));
@brownsoo
brownsoo / Canceling an Action.java
Created August 13, 2021 05:32
Canceling an Action
Scheduler scheduler = Schedulers.newThread();
Scheduler.Worker worker = scheduler.createWorker();
worker.schedule(() -> {
result += "First_Action";
worker.unsubscribe();
});
worker.schedule(() -> result += "Second_Action");
Assert.assertTrue(result.equals("First_Action"));
@brownsoo
brownsoo / KVOBinding.swift
Created November 23, 2021 08:29
extension for KVO
extension NSObjectProtocol where Self: NSObject {
func observe<Value>(_ keyPath: KeyPath<Self, Value>,
onChange: @escaping (Value?) -> Void) -> NSKeyValueObservation {
return observe(keyPath, options: [.initial, .new]) { _, change in
onChange(change.newValue)
}
}
func bind<Value, Target>(_ sourceKeyPath: KeyPath<Self, Value>,