Android Realm Starter Code
This is MainViewModel.kt
package com.example.first
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.first.models.Address
import com.example.first.models.Course
import com.example.first.models.Teacher
import io.realm.kotlin.ext.query
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
class MainViewModel: ViewModel() {
private val realm = MyApp.realm
val courses = realm
.query<Course>(
)
.asFlow()
.map { results ->
results.list.toList()
}
.stateIn(
viewModelScope,
SharingStarted.WhileSubscribed(),
emptyList()
)
var courseDetails: Course? by mutableStateOf(null)
private set
fun showCourseDetails(course: Course) {
courseDetails = course
}
fun hideCourseDetails() {
courseDetails = null
}
fun deleteCourse() {
viewModelScope.launch {
realm.write {
val course = courseDetails ?: return@write
val latestCourse = findLatest(course) ?: return@write
delete(latestCourse)
courseDetails = null
}
}
}
fun addNewCourse(courseName: String, teacherFullName: String, teacherStreet: String, teacherHouseNumber: Int, teacherZip: Int, teacherCity: String) {
viewModelScope.launch {
realm.write {
val address = Address().apply {
fullName = teacherFullName
street = teacherStreet
houseNumber = teacherHouseNumber
zip = teacherZip
city = teacherCity
}
val teacher = Teacher().apply {
this.address = address
}
val course = Course().apply {
name = courseName
this.teacher = teacher
}
copyToRealm(course)
}
}
}
// init {
// createSampleEntries()
// }
// private fun createSampleEntries(){
// viewModelScope.launch {
// realm.write {
// val address1= Address().apply{
// fullName= "John Doe"
// street = " John Doe Street"
// houseNumber = 25
// zip = 12345
// city = "Johncity"
// }
// val address2= Address().apply{
// fullName= "Jane Doe"
// street = " Jane Doe Street"
// houseNumber = 25
// zip = 12345
// city = "Johncity"
// }
// val course1 = Course().apply {
// name = "Kotlin Programming"
// }
// val course2 = Course().apply {
// name = "Android Basics"
// }
// val course3 = Course().apply {
// name = "Asynchronous Programming"
// }
// val teacher1 = Teacher().apply {
// address = address1
// courses = realmListOf(course1, course2)
// }
// val teacher2 = Teacher().apply {
// address = address2
// courses = realmListOf(course3)
// }
//
// course1.teacher = teacher1
// course2.teacher = teacher1
// course3.teacher = teacher2
//
// address1.teacher = teacher1
// address2.teacher = teacher2
//
// val student1 = Student().apply {
// name = "John Doe"
// }
// val student2 = Student().apply {
// name = "Jane Doe"
// }
//
// course1.enrolledStudents.add(student1)
// course2.enrolledStudents.add(student2)
// course3.enrolledStudents.addAll(listOf(student1,student2))
//
// copyToRealm(teacher1, updatePolicy = UpdatePolicy.ALL)
// copyToRealm(teacher2, updatePolicy = UpdatePolicy.ALL)
//
// copyToRealm(course1, updatePolicy = UpdatePolicy.ALL)
// copyToRealm(course2, updatePolicy = UpdatePolicy.ALL)
// copyToRealm(course3, updatePolicy = UpdatePolicy.ALL)
//
// copyToRealm(student1, updatePolicy = UpdatePolicy.ALL)
// copyToRealm(student2, updatePolicy = UpdatePolicy.ALL)
//
// }
// }
// }
}
This is Realm Initializer MyApp.kt
package com.example.first
import android.app.Application
import com.example.first.models.Address
import com.example.first.models.Course
import com.example.first.models.Student
import com.example.first.models.Teacher
import io.realm.kotlin.Realm
import io.realm.kotlin.RealmConfiguration
class MyApp:Application() {
companion object {
lateinit var realm: Realm
}
override fun onCreate() {
super.onCreate()
realm = Realm.open(
configuration = RealmConfiguration.create(
schema = setOf(
Address::class,
Teacher::class,
Course::class,
Student::class
)
)
)
}
}
And also Add the MyApp.kt file
package com.example.first
import android.app.Application
import com.example.first.ExpenseModels.Category
import com.example.first.ExpenseModels.Expense
import io.realm.kotlin.Realm
import io.realm.kotlin.RealmConfiguration
class ExpenseApp: Application() {
companion object {
lateinit var realm: Realm
}
override fun onCreate() {
super.onCreate()
realm = Realm.open(
configuration = RealmConfiguration.create(
schema = setOf(
Category::class,
Expense::class
)
)
)
}
}
In AndroidManifest.xml file as
<application
android:name=".ExpenseApp"
Comments
Post a Comment