Skip to content

Instantly share code, notes, and snippets.

View droid-lover's full-sized avatar

Sachin Rajput droid-lover

View GitHub Profile
View Lesson 27 - Class, Objects & Primary Constructor
/**
* Native Mobile Bits Evening Coding Paper @5pm IST
* Class, Objects & Primary Constructor
*/
class Player constructor(val playerName:String) {
}
fun main(){
View lateinit example in Kotlin
/**
* Native Mobile Bits Evening Coding Paper @5pm IST
* lateinit in Kotlin
*/
class Main {
lateinit var name: String
fun checkValues() {
if (this::name.isInitialized){
println("Value of name is $name")
View Infix_NativeMobileBits.kt
/**
* Native Mobile Bits Evening Coding Paper @5pm IST
* "Infix - very useful"
*/
fun main() {
// val result = 20.add(3) Traditional way
val result = 20 add 3 //with Infix function
println(result)
@droid-lover
droid-lover / Android12 PendingIntent examples using AlaramManager.kt
Created February 3, 2022 13:18
a normal sample of using alarm manager in android12 with pendingIntent changes by using its mutability.
View Android12 PendingIntent examples using AlaramManager.kt
val intent = Intent(this, AlarmReceiver::class.java)
val pendingIntent = PendingIntent.getBroadcast(
this,
ALARM_SERVICE_CODE,
intent,
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
)
val mAlarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
mAlarmManager.setExactAndAllowWhileIdle(
@droid-lover
droid-lover / Android11 PendingIntent examples using AlaramManager.kt
Last active February 3, 2022 13:19
a normal sample of using alarm manager in android11 with pendingIntent.
View Android11 PendingIntent examples using AlaramManager.kt
val intent = Intent(this, AlarmReceiver::class.java)
val pendingIntent = PendingIntent.getBroadcast(
this,
ALARM_SERVICE_CODE,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
)
val mAlarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
mAlarmManager.setExactAndAllowWhileIdle(
View data class
package com.vs.kotlinserialization
import kotlinx.serialization.Serializable
//we need to add this annotation
@Serializable
data class Fruit (val name:String)
View build.gradle(app)
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
//Plugin
apply plugin: 'kotlinx-serialization'
android {
defaultConfig {
...
}
View build.gradle(project_level)
buildscript {
ext.kotlin_version = "1.4.10"
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.0.1"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
View MainActivity.kt
//add this annotation to make it accessible all dependencies from DI Graph created using HILT
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
//access viewmodel like this
private val sampleViewModel: SampleViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
View SampleViewModel.kt
class SampleViewModel @ViewModelInject constructor(
private val networkRepository: NetworkRepository
) : ViewModel() {
//code
}