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.
{"DZD":"{\"2015-11-19\":[54273.24,0.1794,0.1794,9736.619256,9736.619256,1],\"2015-11-24\":[51899.89,0.1794,0.1794,9310.840266,9310.840266,1],\"2015-11-27\":[58842.59,0.0695,0.0695,4089.5600050000003,4089.5600050000003,1],\"2016-05-02\":[109649.12,0.0456,0.0456,4999.999872,4999.999872,1],\"2016-06-13\":[141843.97,0.0141,0.0141,1999.999977,1999.999977,1],\"2016-06-26\":[144300.14,0.1386,0.1386,19999.999404000002,19999.999404000002,1],\"2017-03-02\":[211301.27,0.0474,0.0474,10015.680197999998,10015.680197999998,1],\"2017-03-05\":[215053.76,0.0093,0.0093,1999.9999679999999,1999.9999679999999,1],\"2017-03-09\":[207468.88,0.0482,0.0482,10000.000016,10000.000016,1],\"2017-04-29\":[265300.26,0.03769314,0.03769314,9999.9998422164,9999.9998422164,1]}","NAD":"{\"2014-03-20\":[7793.73,1.0816977,1.0816977,8430.459815421,8430.459815421,1],\"2014-10-14\":[5339.03,0.1873,0.1873,1000.0003189999999,1000.0003189999999,1],\"2014-10-25\":[5347.59,0.0187,0.0187,99.99993300000001,99.99993300000001,1],\"2015-01-13\":[7080.2617291507
//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)) {