Skip to content

Instantly share code, notes, and snippets.

View dewa1993's full-sized avatar
🎯
Focusing

dewa1993

🎯
Focusing
View GitHub Profile
@dewa1993
dewa1993 / vscode.config
Created March 10, 2022 08:16
this contains my vs code config to be synced on multiple device.
// temp
public class RoomDatabaseMigrationProvider {
public static final Migration MIGRATION_1_2 = new Migration(1, 2) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase database) {
// we have not altered any tables
Log.d("##### 1 - 2 Migration", "started....");
migrateDatabase_1_2(database);
Log.d("##### 1 - 2 Migration", "completed....");
fun EditText.afterTextChanged(afterTextChanged: (String) -> Unit) {
this.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
override fun afterTextChanged(editable: Editable?) {
afterTextChanged.invoke(editable.toString())
@dewa1993
dewa1993 / MarginItemDecoration
Created December 12, 2019 13:17
Right way to provide margin to recyclerview Item
import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView
class MarginItemDecoration(private val spaceHeight: Int) : RecyclerView.ItemDecoration() {
override fun getItemOffsets(outRect: Rect, view: View,
parent: RecyclerView, state: RecyclerView.State) {
with(outRect) {
if (parent.getChildAdapterPosition(view) == 0) {
top = spaceHeight
@dewa1993
dewa1993 / DateUtils.java
Last active December 12, 2019 13:18
Convert date from any server format to desired date format
private static final Map<String, String> DATE_FORMAT_REGEXPS;
static {
DATE_FORMAT_REGEXPS = new HashMap<>();
DATE_FORMAT_REGEXPS.put("^\\d{8}$", "yyyyMMdd");
DATE_FORMAT_REGEXPS.put("^\\d{1,2}-\\d{1,2}-\\d{4}$", "dd-MM-yyyy");
DATE_FORMAT_REGEXPS.put("^\\d{4}-\\d{1,2}-\\d{1,2}$", "yyyy-MM-dd");
DATE_FORMAT_REGEXPS.put("^\\d{1,2}/\\d{1,2}/\\d{4}$", "MM/dd/yyyy");
DATE_FORMAT_REGEXPS.put("^\\d{4}/\\d{1,2}/\\d{1,2}$", "yyyy/MM/dd");
DATE_FORMAT_REGEXPS.put("^\\d{1,2}\\s[a-z]{3}\\s\\d{4}$", "dd MMM yyyy");
const printA = (target: Object, key: string | symbol, descriptor: TypedPropertyDescriptor<Function>) => {
console.log("A");
}
const printB = (target: Object, key: string | symbol, descriptor: TypedPropertyDescriptor<Function>) => {
console.log("B");
}
class Printer {
@printA
const greaterOrEqual = (n: number) => {
return (target: Object, key: string | symbol) => {
let value = target[key];
const getter = () => value;
const setter = (val) => {
if (val < n) {
throw new Error(`Value smaller than ${n}`);
}
value = val;
const log = (target: Object, key: string | symbol) => {
let value = target[key];
const getter = () => {
console.log("Getting value: ", value);
return value;
};
const setter = (val) => {
console.log("Setting value: ", val);
value = val;
const log = (target: Object, key: string | symbol, descriptor: TypedPropertyDescriptor<Function>) => {
return {
value: function( ... args: any[]) {
console.log("Arguments: ", args.join(", "));
const result = descriptor.value.apply(target, args);
console.log("Result: ", result);
return result;
}
}
}
const log = <T>(originalConstructor: new(...args: any[]) => T) => {
function newConstructor(... args) {
console.log("Arguments: ", args.join(", "));
new originalConstructor(args);
}
newConstructor.prototype = originalConstructor.prototype;
return newConstructor;
}
@log