Skip to content

Instantly share code, notes, and snippets.

View adriantache's full-sized avatar

Adrian Tache adriantache

View GitHub Profile
@adriantache
adriantache / Result.kt
Last active January 15, 2025 14:33
Alternative to the default Kotlin result class
/**
* Alternative to the native Result class, easier to map but less efficient.
*/
sealed class Result<T> {
data class Success<T>(val value: T) : Result<T>()
data class Failure<T>(val error: Error) : Result<T>()
val isSuccess
get() = this is Success
fun View.setVisibility(visibility: Int) {
val motionLayout = parent as MotionLayout
motionLayout.constraintSetIds.forEach {
val constraintSet = motionLayout.getConstraintSet(it) ?: return@forEach
constraintSet.setVisibility(this.id, visibility)
constraintSet.applyTo(motionLayout)
}
}
//First we build the URI and the auth header and pass them to the AsyncTask
private static final String GIT_HUB_API = "api.github.com";
private static final String USER_ENDPOINT = "user";
private static final String REPOS_ENDPOINT = "repos";
private static final String ERROR = "error";
//(...)
Uri.Builder builder = new Uri.Builder();
//First we build the URI and pass it to the AsyncTask
private static final String GIT_HUB_API = "api.github.com";
private static final String USERS_ENDPOINT = "users";
private static final String REPOS_ENDPOINT = "repos";
private static final String ERROR = "error";
//(...)
Uri.Builder builder = new Uri.Builder();
@adriantache
adriantache / RomanNumeralSort.kt
Last active June 7, 2019 06:50
Roman Numeral Sort
fun main() {
val inputString =
"I XV XIII XXX MM CLIV CM LXIX DX III XIX XVI CMXLIV M V IV CMLXIX MX CMX VIII DI XXIX XCIX MMMMMMMMMMCM"
val numberList = getNumberList(inputString)
numberList.sort()
for (i in numberList) {
printRomanNumeral(i)
1544603090837
{"DZD":6945849,"NAD":2402995,"GHS":22713497,"EGP":22713033,"BGN":22707960,"PAB":22709538,"BOB":22698582,"DKK":22709849,"BWP":22634301,"LBP":2654371,"TZS":22714298,"VND":22710255,"AOA":22588263,"KHR":11058885,"MYR":22714724,"KYD":458490,"UAH":22715249,"JOD":22668975,"SAR":22710271,"LTL":901167,"HKD":22714865,"CHF":22713695,"GIP":2803350,"XAR":20352582,"BYR":6439419,"ALL":22578912,"BYN":22716218,"HRK":22688029,"SZL":22694878,"THB":22715854,"XAF":22713460,"BND":22424760,"ISK":22648216,"UYU":22707792,"LAK":883531,"SYP":19011193,"MAD":22715869,"MZN":10688466,"PHP":22715751,"ZAR":22718071,"NPR":18337338,"NGN":22727621,"CRC":22722401,"AED":22723150,"MWK":22722713,"LKR":22727931,"ETH":22726950,"PKR":22730553,"HUF":22724049,"LSL":1477849,"AMD":21650888,"UGX":22729989,"QAR":22668584,"JMD":15494981,"GEL":22682778,"SHP":22403252,"AFN":20729905,"TRY":22730467,"BDT":22727237,"YER":398934,"XRP":22722626,"HTG":22704860,"XOF":22704547,"MGA":11637586,"RWF":22728855,"NOK":22733144,"MOP":10829778,"INR":22765431,"MXN":22762359,"C
This file has been truncated, but you can view the full file.
//build an immediate OneTimeWorkRequest to fetch events from remote
OneTimeWorkRequest getEventJson = new OneTimeWorkRequest
.Builder(UpdateEventsWorker.class)
.setInputData(remoteUrl)
.addTag(EVENTS_JSON_WORK_TAG)
.build();
//we get a ListenableFuture<Void> from enqueue() which we can use below
//using beginUniqueWork to prevent re-enqueuing the same task while it is already running
ListenableFuture<Void> listenableFuture = WorkManager.getInstance().beginUniqueWork(EVENTS_JSON_WORK_TAG,
//WARNING: this code is no longer valid as of WorkManager-alpha-10, please
//refer to this post for more details: https://medium.com/p/8e49c463cbf7
//finally, we get the status from the WorkManager by id since we still have
//a reference, otherwise we could do it by tag, and create an observer which
//checks whether the Worker has succeeded (i.e. returned Worker.Result.SUCCESS)
WorkManager.getInstance()
.getStatusById(getEventJson.getId())
.observe(MainActivity.this, workStatus -> {
if (workStatus != null && workStatus.getState().equals(State.SUCCEEDED)) {