Skip to content

Instantly share code, notes, and snippets.

View AkshayChordiya's full-sized avatar
🔱

Akshay Chordiya AkshayChordiya

🔱
View GitHub Profile
@AkshayChordiya
AkshayChordiya / ApplyExample.kt
Last active May 25, 2024 16:29
Example of apply function in Kotlin
val person = Person().apply {
name = "Tony Stark"
age = 52
// More such stuff
}
@AkshayChordiya
AkshayChordiya / LetExample.kt
Last active July 21, 2017 11:21
Example of let function in Kotlin
person?.let {
// person object is not null in here
it.name = "Tony Stark"
}
@AkshayChordiya
AkshayChordiya / WithExample.kt
Last active July 10, 2017 06:28
Person setter with `with`
with(Person()) {
name = "Tony Stark"
age = 52
// More such stuff
}
@AkshayChordiya
AkshayChordiya / Person.java
Created July 5, 2017 15:16
Person setter
Person person = new Person();
person.setName("Tony Stark");
person.setAge(52);
// More such stuff
@AkshayChordiya
AkshayChordiya / ExecutionTime.kt
Created July 5, 2017 14:44
Execution Time in Kotlin
val executionTime = measureTimeMillis {
// Do your task
}
println("Execution Time = $executionTime ms")
@AkshayChordiya
AkshayChordiya / ExecutionTime.java
Last active August 26, 2017 17:24
Execution Time in Java
long startTime = System.currentTimeMillis();
// Do your task
long executionTime = System.currentTimeMillis() - startTime;
System.out.println("Execution Time = " + executionTime + " ms");
public class ViewModelStore {
private final HashMap<String, ViewModel> mMap = new HashMap();
public ViewModelStore() {}
final void put(String key, ViewModel viewModel) {
ViewModel oldViewModel = (ViewModel)this.mMap.get(key);
if(oldViewModel != null) {
oldViewModel.onCleared();
}
@AkshayChordiya
AkshayChordiya / MainActivity.kt
Last active July 5, 2017 11:01
Main Activity with ViewModel instance
class MainActivity : LifecycleActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Get the ViewModel instance
val simpleViewModel = ViewModelProviders.of(this).get(SimpleViewModel::class.java)
val data = simpleViewModel.getDataList()
}
@AkshayChordiya
AkshayChordiya / ContextViewModel.kt
Last active July 5, 2017 10:15
Simple ViewModel class with context
class ContextViewModel(application: Application) : AndroidViewModel(application) {
/**
* The data
*/
private var data: List<Data>
init {
// Load the data over here
// data = ....
@AkshayChordiya
AkshayChordiya / SimpleViewModel.kt
Last active July 5, 2017 10:15
Simple ViewModel
class SimpleViewModel() : ViewModel() {
/**
* The data
*/
private var data: List<Data>
init {
// Load the data over here
// data = ....