Last active
January 30, 2022 21:29
-
-
Save svanellewee/434947122548d3112ee56d40b08dc276 to your computer and use it in GitHub Desktop.
ViewModel, Room
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
plugins { | |
id 'com.android.application' | |
id 'kotlin-android' | |
id "kotlin-kapt" | |
} | |
android { | |
compileSdk 31 | |
buildFeatures { | |
viewBinding true | |
} | |
defaultConfig { | |
applicationId "com.svanellewee.hackdbagain" | |
minSdk 26 | |
targetSdk 31 | |
versionCode 1 | |
versionName "1.0" | |
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | |
} | |
buildTypes { | |
release { | |
minifyEnabled false | |
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | |
} | |
} | |
compileOptions { | |
sourceCompatibility JavaVersion.VERSION_1_8 | |
targetCompatibility JavaVersion.VERSION_1_8 | |
} | |
kotlinOptions { | |
jvmTarget = '1.8' | |
} | |
} | |
dependencies { | |
// Room components | |
implementation "androidx.room:room-ktx:$rootProject.roomVersion" | |
kapt "androidx.room:room-compiler:$rootProject.roomVersion" | |
androidTestImplementation "androidx.room:room-testing:$rootProject.roomVersion" | |
// Lifecycle components | |
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$rootProject.lifecycleVersion" | |
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$rootProject.lifecycleVersion" | |
implementation "androidx.lifecycle:lifecycle-common-java8:$rootProject.lifecycleVersion" | |
implementation "androidx.fragment:fragment-ktx:1.4.1" | |
// Kotlin components | |
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" | |
api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$rootProject.coroutines" | |
api "org.jetbrains.kotlinx:kotlinx-coroutines-android:$rootProject.coroutines" | |
implementation 'androidx.core:core-ktx:1.7.0' | |
implementation 'androidx.appcompat:appcompat:1.4.1' | |
implementation 'com.google.android.material:material:1.5.0' | |
implementation 'androidx.constraintlayout:constraintlayout:2.1.3' | |
testImplementation 'junit:junit:4.+' | |
androidTestImplementation 'androidx.test.ext:junit:1.1.3' | |
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' | |
} |
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
// Top-level build file where you can add configuration options common to all sub-projects/modules. | |
buildscript { | |
ext.kotlin_version = '1.5.31' | |
repositories { | |
google() | |
mavenCentral() | |
} | |
dependencies { | |
classpath "com.android.tools.build:gradle:7.0.0" | |
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10" | |
// NOTE: Do not place your application dependencies here; they belong | |
// in the individual module build.gradle files | |
} | |
} | |
task clean(type: Delete) { | |
delete rootProject.buildDir | |
} | |
ext { | |
activityVersion = '1.4.0' | |
appCompatVersion = '1.4.0' | |
constraintLayoutVersion = '2.1.2' | |
coreTestingVersion = '2.1.0' | |
coroutines = '1.5.2' | |
lifecycleVersion = '2.4.0' | |
materialVersion = '1.4.0' | |
roomVersion = '2.4.1' | |
// testing | |
junitVersion = '4.13.2' | |
espressoVersion = '3.4.0' | |
androidxJunitVersion = '1.1.3' | |
} |
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 com.svanellewee.hackdbagain | |
import android.content.Context | |
import androidx.appcompat.app.AppCompatActivity | |
import android.os.Bundle | |
import android.util.Log | |
import androidx.activity.viewModels | |
import androidx.lifecycle.* | |
import androidx.room.* | |
import com.svanellewee.hackdbagain.databinding.ActivityMainBinding | |
import kotlinx.coroutines.flow.Flow | |
import kotlinx.coroutines.launch | |
@Entity(tableName = "word_table") | |
class Word(@PrimaryKey @ColumnInfo(name = "word") val word: String) | |
@Dao | |
interface WordDao { | |
@Query("SELECT * FROM word_table ORDER BY word ASC") | |
fun getAlphabetizedWords(): Flow<List<Word>> | |
@Insert(onConflict = OnConflictStrategy.IGNORE) | |
suspend fun insert(word: Word) | |
@Query("DELETE FROM word_table") | |
suspend fun deleteAll() | |
} | |
@Database(entities = [Word::class], version = 1, exportSchema = false) | |
public abstract class WordRoomDatabase : RoomDatabase() { | |
abstract fun wordDao(): WordDao | |
// This is how you make a singleton in Kotlin it seems... | |
companion object { | |
@Volatile | |
private var INSTANCE: WordRoomDatabase? = null | |
fun makeDatabase(context: Context): WordRoomDatabase { | |
if (INSTANCE == null) { | |
INSTANCE = Room.databaseBuilder( | |
context, | |
WordRoomDatabase::class.java, | |
"word_database" | |
).build() | |
} | |
return INSTANCE as WordRoomDatabase | |
} | |
} | |
} | |
class WordViewModel(private val wordDB: WordRoomDatabase): ViewModel() { | |
// Kotlin flows are interesting... | |
val allWords: LiveData<List<Word>> = wordDB.wordDao().getAlphabetizedWords().asLiveData() | |
fun insert(word: Word) = viewModelScope.launch { | |
// this is a suspend function... | |
wordDB.wordDao().insert(word) | |
} | |
} | |
class WordViewModelFactory(private val wordDB: WordRoomDatabase) : ViewModelProvider.Factory { | |
override fun <T : ViewModel> create(modelClass: Class<T>): T { | |
return WordViewModel(wordDB) as T | |
} | |
} | |
class MainActivity : AppCompatActivity() { | |
private lateinit var actMainBinding : ActivityMainBinding | |
// "androidx.fragment:fragment-ktx:1.4.1" | |
private val wordViewModel: WordViewModel by viewModels { | |
WordViewModelFactory(WordRoomDatabase.makeDatabase(applicationContext)) | |
} | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
actMainBinding = ActivityMainBinding.inflate(layoutInflater) | |
setContentView(actMainBinding.root) | |
wordViewModel.allWords.observe(this, Observer { | |
wordList -> wordList?.forEach { wordElem -> | |
Log.i("NARF", "...${wordElem.word}") | |
} | |
}) | |
actMainBinding.btAdd.setOnClickListener { | |
var result = actMainBinding.edInput.text.toString() | |
Log.i("BLA", result) | |
wordViewModel.insert(Word(result)) | |
actMainBinding.edInput.text.clear() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment