Skip to content

Instantly share code, notes, and snippets.

@shivarajp
Created March 4, 2021 07:46
Show Gist options
  • Save shivarajp/d02febe8b3ba1fcc2ceea7c8937230d1 to your computer and use it in GitHub Desktop.
Save shivarajp/d02febe8b3ba1fcc2ceea7c8937230d1 to your computer and use it in GitHub Desktop.
package com.masai.mvvmapplication.data.local
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
@Dao
interface MyAgeDAO {
@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun insert(myAgeEntity: MyAgeEntity)
}
package com.masai.mvvmapplication.data.local
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
@Entity(tableName = "my_age_table")
data class MyAgeEntity(
@ColumnInfo(name = "name") val name: String,
@ColumnInfo(name = "age") val age : Int,
@ColumnInfo(name = "count") val count: Int) {
@PrimaryKey(autoGenerate = true) @ColumnInfo(name = "id") var id: Int? = null
}
package com.masai.mvvmapplication.data.local
import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
@Database(entities = [MyAgeEntity::class], version = 1)
abstract class MyAgeDatabase : RoomDatabase(){
abstract fun getMyAgeDao(): MyAgeDAO
companion object{
private var INSTANCE: MyAgeDatabase? = null
fun getDatabase(context: Context): MyAgeDatabase{
if (INSTANCE == null){
val builder = Room.databaseBuilder(
context.applicationContext,
MyAgeDatabase::class.java,
"age_database"
)
builder.fallbackToDestructiveMigration()
INSTANCE = builder.build()
return INSTANCE!!
}else{
return INSTANCE!!
}
}
}
}
package com.masai.mvvmapplication.repository
import com.masai.mvvmapplication.data.local.MyAgeDAO
import com.masai.mvvmapplication.data.local.MyAgeEntity
import com.masai.mvvmapplication.data.remote.APIService
import com.masai.mvvmapplication.data.remote.ResponseHandler
import com.masai.mvvmapplication.data.remote.RetrofitGenerator
class MyRespository(val myAgeDaoObject: MyAgeDAO) {
private val CONTENT_TYPE = "application/json"
val api = RetrofitGenerator.getInstance()
.create(APIService::class.java)
val responseHandler = ResponseHandler()
suspend fun guessMyAge(name: String){
val result = api.guessMyAge(CONTENT_TYPE, name)
val myAgeEntity = MyAgeEntity(result.name, result.age, result.count)
myAgeDaoObject.insert(myAgeEntity)
}
}
package com.masai.mvvmapplication.data.remote
import com.masai.mvvmapplication.data.models.AgeResponse
import retrofit2.http.*
interface APIService {
@Headers("Accept: application/json")
@GET("/")
suspend fun guessMyAge(
@Header("Content-Type") contentType: String,
@Query("name") name: String
): AgeResponse
}
package com.masai.mvvmapplication.viewmodels
import androidx.lifecycle.*
import com.masai.mvvmapplication.data.local.MyAgeDAO
import com.masai.mvvmapplication.data.models.AgeResponse
import com.masai.mvvmapplication.repository.MyRespository
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
class MyViewModel(
val repository: MyRespository) : ViewModel() {
fun guessMyAge(name: String){
CoroutineScope(Dispatchers.IO).launch {
repository.guessMyAge(name)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment