show dbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. HiltViewModel (hiltvm) - it assumes you are using State and Event classes for ViewModel, as well as UiEvent class for handling one time UI events: | |
import androidx.lifecycle.ViewModel | |
import androidx.lifecycle.viewModelScope | |
import com.mssmk.cleanarchitecturetemplate.ui.utils.UiEvent | |
import kotlinx.coroutines.channels.Channel | |
import dagger.hilt.android.lifecycle.HiltViewModel | |
import kotlinx.coroutines.flow.MutableStateFlow | |
import kotlinx.coroutines.flow.receiveAsFlow | |
import kotlinx.coroutines.launch | |
import javax.inject.Inject |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Q. What are some examples of collections ? | |
A. Lists, Arrays, Maps, Sets are collections. | |
Q. How many types of lists are there? | |
A. Lists are of two types : Mutable Lists and Immutable Lists. | |
Q. What is a map? | |
A. A map is a collection that holds and key and a value pair. | |
Q. What datatypes can be stored in collections? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Coroutine-based solution for delayed and periodic work. May fire once (if [interval] omitted) | |
* or periodically ([startDelay] defaults to [interval] in this case), replacing both | |
* `Observable.timer()` & `Observable.interval()` from RxJava. | |
* | |
* In contrast to RxJava, intervals are calculated since previous run completion; this is more | |
* convenient for potentially long work (prevents overlapping) and does not suffer from queueing | |
* multiple invocations in Doze mode on Android. | |
* | |
* Dispatcher is inherited from scope, may be overridden via [context] parameter. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import androidx.appcompat.app.AppCompatActivity | |
import android.os.Bundle | |
import androidx.fragment.app.Fragment | |
import dagger.hilt.android.AndroidEntryPoint | |
import javax.inject.Inject | |
@AndroidEntryPoint | |
class MainActivity : AppCompatActivity() { | |
@Inject |
Notes:
- The file name has to end in
.main.kts
andkotlin-main-kts.jar
has to be on classpath. See https://youtrack.jetbrains.com/issue/KT-27853 - Transitive dependencies have to be explicitly listed, and version conflicts manually resolved by hard-coding the right versions.
kotlinc
: https://kotlinlang.org/docs/tutorials/command-line.html
To edit this in IntelliJ IDEA:
- create and
idea
folder next to the.kts
file - save
build.gradle
to that folder - import the project from
idea/build.gradle
.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.fasterxml.jackson.core.type.TypeReference | |
import com.fasterxml.jackson.databind.ObjectMapper | |
import org.gradle.api.Task | |
import org.gradle.api.Project | |
import org.gradle.api.UnknownTaskException | |
import org.gradle.api.GradleException | |
import org.gradle.api.plugins.ExtraPropertiesExtension | |
@SuppressWarnings("UNCHECKED_CAST") | |
fun <T> Project.ext(key: String): T { |
Source: Cedric's blog, EricLin, Cedric's blog
For some in depth explanation on these standard functions and Function Literal with Receiver see KotlinExpertise and coding convention for apply/with/run/also/let.
Quick overview of some important funtion in Kotlin's standard library
let()
fun T.let(f: (T) -> R): R = f(this)```
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package peterho.kotlintipstricks | |
import android.content.Context | |
import android.support.annotation.AttrRes | |
import android.support.annotation.StyleRes | |
import android.support.v4.app.Fragment | |
import android.support.v4.app.FragmentManager | |
import android.support.v7.app.AlertDialog | |
import android.support.v7.widget.RecyclerView | |
import android.text.TextUtils |
NewerOlder