Skip to content

Instantly share code, notes, and snippets.

View ktvipin27's full-sized avatar
🏠
Working from home

Vipin K T ktvipin27

🏠
Working from home
View GitHub Profile
@ktvipin27
ktvipin27 / BaseActivity.kt
Created May 20, 2020 15:28
Implementation of RoomInspector in BaseActivity
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
if (BuildConfig.DEBUG && keyCode == KeyEvent.KEYCODE_VOLUME_DOWN && event?.isLongPress == true) {
RoomInspector.inspect(this, MyDatabase::class.java, MyDatabase.DB_NAME)
return true
}
return super.onKeyDown(keyCode, event)
}
@ktvipin27
ktvipin27 / BaseActivity.kt
Last active May 29, 2020 03:35
BaseActivity for the demo of template creation.
abstract class BaseActivity<D : ViewDataBinding, V : BaseViewModel> : AppCompatActivity(){
@get:LayoutRes
protected abstract val layoutId: Int
protected abstract val viewModelClass: Class<V>
internal val viewModel:V by lazy { ViewModelProvider(this).get(viewModelClass) }
internal lateinit var binding: D
@ktvipin27
ktvipin27 / BaseViewModel.kt
Created May 29, 2020 03:36
BaseViewModel for the demo of template creation.
abstract class BaseViewModel : ViewModel() {
open fun onActivityCreated(extras: Bundle?) {}
open fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {}
}
@ktvipin27
ktvipin27 / activity_sample.xml
Last active May 29, 2020 04:41
Sample layout file for the demo of template creation.
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="viewModel"
type="yourpackage.YourViewModel" />
</data>
<!-- Add your views here.-->
@ktvipin27
ktvipin27 / template.xml
Created May 29, 2020 05:48
Sample template.xml file
<?xml version="1.0"?>
<template
format="5"
revision="5"
name="App Activity"
minApi="21"
minBuildApi="21"
description="Creates a new app activity">
<category value="Activity" />
@ktvipin27
ktvipin27 / recipe.xml.ftl
Last active May 29, 2020 07:17
Sample recipe.xml.ftl file
<?xml version="1.0"?>
<recipe>
<merge from="root/res/values/strings.xml.ftl"
to="${escapeXmlAttribute(resOut)}/values/strings.xml" />
<merge from="root/AndroidManifest.xml.ftl"
to="${escapeXmlAttribute(manifestOut)}/AndroidManifest.xml" />
<instantiate from="root/src/app_package/Activity.kt.ftl"
@ktvipin27
ktvipin27 / DemoActivity.kt
Last active May 29, 2020 07:54
Demo files created using Android studio template engine.
package com.ktvipin.templatedemo.demo
import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.ktvipin.templatedemo.R
import com.ktvipin.templatedemo.core.BaseActivity
import com.ktvipin.templatedemo.databinding.ActivityDemoBinding
class DemoActivity : BaseActivity<ActivityDemoBinding, DemoViewModel>() {
@ktvipin27
ktvipin27 / Activity.kt.ftl
Last active May 29, 2020 07:55
Sample template files for demo of template architecture.
package ${escapeKotlinIdentifiers(packageName)}
import android.content.Context
import android.content.Intent
import android.os.Bundle
import ${applicationPackage}.R
import ${applicationPackage}.core.BaseActivity
import ${applicationPackage}.databinding.${underscoreToCamelCase(layoutName)}Binding
class ${activityClass} : BaseActivity<${underscoreToCamelCase(layoutName)}Binding,${viewModelClass}>() {
@ktvipin27
ktvipin27 / TimberRemoteTree.kt
Last active June 11, 2020 12:39
Timber tree for adding logs to firebase realtime database.
class TimberRemoteTree(private val deviceDetails: DeviceDetails) : Timber.DebugTree() {
private val dateFormat = SimpleDateFormat("dd-MM-yyyy", Locale.getDefault())
private val timeFormat = SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS a zzz", Locale.getDefault())
private val date = dateFormat.format(Date(System.currentTimeMillis()))
private val logRef = Firebase.database.getReference("logs/$date/${deviceDetails.deviceId}")
override fun log(priority: Int, tag: String?, message: String, t: Throwable?) {
if (BuildConfig.REMOTE_LOG_ENABLED) {
@ktvipin27
ktvipin27 / TimberRemoteApp.kt
Last active June 11, 2020 14:23
Sample application class with Timber configuration for pushing logs to Firebase realtime database.
class TimberRemoteApp : Application() {
@SuppressLint("HardwareIds")
override fun onCreate() {
super.onCreate()
if (BuildConfig.DEBUG) {
val deviceId = Settings.Secure.getString(contentResolver, Settings.Secure.ANDROID_ID)
val deviceDetails = DeviceDetails(deviceId)
val remoteTree = TimberRemoteTree(deviceDetails)