Skip to content

Instantly share code, notes, and snippets.

View ntoskrnl's full-sized avatar

Anton Danshin ntoskrnl

View GitHub Profile
@ntoskrnl
ntoskrnl / AddCompetitionResultDialog.java
Created June 9, 2012 22:26
Add competition result dialog
package ru.mipt.sport.gui.competition;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.swing.*;
import ru.mipt.sport.model.entity.RunResultEntity;
import ru.mipt.sport.model.entity.RunResultEntryEntity;
754 ,
783 ,
769 ,
744 ,
783 ,
811 ,
791 ,
758 ,
725 ,
766 ,
public object RxParse {
public fun login(username: String, password: String): Observable<ParseUser> {
return Observable.create { subscriber ->
try {
val user = ParseUser.logIn(username, password)
subscriber.onNext(user)
subscriber.onCompleted()
} catch (ex: ParseException) {
subscriber.onError(ex)
@ntoskrnl
ntoskrnl / Presenter.kt
Created January 18, 2017 23:51 — forked from anonymous/Presenter.kt
Android. Example of how to store presenter in retained fragment and enable it to persist and restore view state.
interface Presenter<in V, in R> {
fun create()
fun attachRouter(router: R)
fun attachView(view: V)
fun detachView()
@ntoskrnl
ntoskrnl / PresenterRetainedFragment.kt
Last active January 20, 2017 21:25
Container for presenters with persistent state
class PresenterRetainedFragment : Fragment() {
private val presenters = mutableMapOf<String, Any>()
private var savedState: Bundle? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
retainInstance = true
@ntoskrnl
ntoskrnl / testnames.py
Created May 22, 2017 16:33
Python script that converts camel case unit test names a_b_c to `a - b - c`.
import re
import sys
def convert(s):
parts = s.split('_')
r = []
for part in parts:
s1 = re.sub('(.)([A-Z][a-z]+)', r'\1 \2', part)
s1 = re.sub('([a-z0-9])([A-Z])', r'\1 \2', s1).lower()
r.append(s1)
@ntoskrnl
ntoskrnl / testnames.py
Last active May 22, 2017 16:36
Python script that converts camel case unit test names a_b_c to `a - b - c`. Usage: `cat SomeTest.kt > testnames.py > SomeTest.kt`
import re
import sys
def convert(s):
parts = s.split('_')
r = []
for part in parts:
s1 = re.sub('(.)([A-Z][a-z]+)', r'\1 \2', part)
s1 = re.sub('([a-z0-9])([A-Z])', r'\1 \2', s1).lower()
r.append(s1)
@ntoskrnl
ntoskrnl / KeyboardVisibility.kt
Created August 28, 2017 10:30
Listen to soft keyboard visibility in Android
/**
* Posts true on soft keyboard open, false on close.
* This creates a strong reference to activity instance. Make sure to call ActivitySubscription.dispoose()
* @param visibleThresholdDp if global activity layout changed more than by 100(default) dp
*
* @return activity subscription object that allows to unregister listener
*/
fun Activity.addSoftKeyboardVisibilityListener(
visibleThresholdDp: Int = 100,
initialState: Boolean = false,
@ntoskrnl
ntoskrnl / JsonRpc.kt
Last active February 23, 2024 06:51
Retrofit-style JSON-RPC client in Kotlin (with gson serialization/deserialization)
fun <T, B> createJsonRpcService(service: Class<T>,
client: JsonRpcClient<B>,
resultDeserializer: Deserializer<B>,
logger: (String) -> Unit = {}): T {
val classLoader = service.classLoader
val interfaces = arrayOf<Class<*>>(service)
val invocationHandler = createInvocationHandler(service, client, resultDeserializer, logger)
@Suppress("UNCHECKED_CAST")
@ntoskrnl
ntoskrnl / JsonRpcProtocolExample.js
Last active September 19, 2017 22:35
JSON-RPC 2.0 request and response example
--> {"jsonrpc": "2.0", "method": "subtract", "params": {"minuend": 42, "subtrahend": 23}, "id": 3}
<-- {"jsonrpc": "2.0", "result": 19, "id": 3}