Skip to content

Instantly share code, notes, and snippets.

@alashow
alashow / CalendarDateRange.kt
Created April 17, 2019 00:16
Kotlin Calendar/Date utils
import java.util.Calendar
import java.util.Date
class CalendarRange(override val start: Calendar, override val endInclusive: Calendar, val field: Int = Calendar.DAY_OF_YEAR) :
ClosedRange<Calendar>, Iterable<Calendar> {
override fun iterator(): Iterator<Calendar> {
return CalendarIterator(start, endInclusive, field)
}
}
@alashow
alashow / OkHttp3Stack.java
Last active May 29, 2020 08:59 — forked from bryanstern/OkHttpStack.java
An OkHttp backed HttpStack for Volley (okhttp3 version)
/**
* The MIT License (MIT)
*
* Copyright (c) 2015 Circle Internet Financial
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
import android.graphics.Typeface
import android.text.SpannableString
import android.text.TextUtils
import android.text.style.CharacterStyle
import android.text.style.StyleSpan
import android.widget.TextView
import java.text.Normalizer
import java.util.Locale
import java.util.regex.Pattern
@alashow
alashow / Fibonacci.kt
Created October 2, 2017 11:30
Fibonacci in Kotlin
package tm.alashow.homework.datastructures.second.kt
import java.math.BigInteger
fun main(args: Array<String>) {
Fibonacci.init()
}
object Fibonacci {
@alashow
alashow / webtextsSolver.js
Created November 15, 2019 02:50
Solves multiple choice question parts of webtexts/soomo learning. Just run this code on browser's console.
prepareAnswers();
setTimeout(resetQuestions, 1500);
setTimeout(solveQuestions, 3000);
function prepareAnswers() {
$.each($.find('.multiple-choice-question-element'), function(i, wrapper) {
wrapper = $(wrapper);
var button = wrapper.find('.save-answers');
var choice = $(wrapper.find('.question-choice')[0]).find('input');
package tm.alashow.x.ui.base
import android.arch.lifecycle.ViewModel
import android.arch.lifecycle.ViewModelProvider
import android.arch.lifecycle.ViewModelProviders
import android.databinding.DataBindingUtil
import android.databinding.ViewDataBinding
import android.os.Bundle
import android.support.annotation.LayoutRes
import android.support.annotation.StringRes
// activity
private val onAppBarLiftListeners = HashSet<AppBarLiftListener>()
val appBar = findViewById<AppBarLayout>(R.id.appBar)
if (appBar != null) {
appBar.addOnOffsetChangedListener(AppBarLayout.OnOffsetChangedListener { _, verticalOffset ->
onAppBarLiftListeners.forEach { it.onAppBarLift(verticalOffset) }
})
}
@alashow
alashow / DateTimePickerLocalizer.kt
Created May 4, 2019 17:25
Android Localize months in DatePickerDialog or am/pm in TimePickerDialog
import timber.log.Timber
/**
* Patch date picker or time picker to use custom months or am/pm
* From: https://stackoverflow.com/a/33599403/2897341
*
* @param picker [android.widget.DatePicker] or [android.widget.TimePicker]
* @param values months or am/pm
*/
fun localizePicker(picker: Any, values: Array<String>) {
@alashow
alashow / AppViewModelFactory.kt
Last active April 1, 2019 05:18
ViewModel dynamic parameters hack
class AppViewModelFactory @Inject constructor(
private val creators: @JvmSuppressWildcards Map<Class<out ViewModel>, Provider<ViewModel>>
) : ViewModelProvider.Factory {
var onViewModelCreate: ((viewModel: ViewModel) -> Unit)? = null
override fun <T : ViewModel> create(modelClass: Class<T>): T {
var creator: Provider<out ViewModel>? = creators[modelClass]
if (creator == null) {
for ((key, value) in creators) {
/*
* Copyright (c) 2016 Alashov Berkeli
* It is licensed under GNU GPL v. 2 or later. For full terms see the file LICENSE.
*/
package tm.asmanoky.aziada.util.glide;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;