Created
July 4, 2023 14:23
-
-
Save senamit2708/8d613f9ba8d45cc02779a49bd3974e85 to your computer and use it in GitHub Desktop.
room database
This file contains hidden or 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.example.coroutinelearning.database | |
| import android.content.Context | |
| import androidx.room.Database | |
| import androidx.room.Room | |
| import androidx.room.RoomDatabase | |
| import com.example.coroutinelearning.entity.User | |
| @Database(entities = [User::class], version = 1, exportSchema = false) | |
| abstract class AppDatabase: RoomDatabase() { | |
| abstract fun getUserDao(): UserDao | |
| companion object{ | |
| private var INSTANCE: AppDatabase? = null | |
| private const val DB_NAME = "database" | |
| fun getDatabase(context: Context): AppDatabase { | |
| return INSTANCE ?: synchronized(this){ | |
| val instance = Room.databaseBuilder( | |
| context.applicationContext, | |
| AppDatabase::class.java, | |
| DB_NAME | |
| ).build() | |
| INSTANCE = instance | |
| instance | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or 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 'kotlin-kapt' | |
| } | |
| //room database | |
| def room_version = "2.5.0" | |
| def lifecycle_version = "2.2.0" | |
| implementation "androidx.room:room-ktx:$room_version" | |
| kapt "androidx.room:room-compiler:$room_version" | |
| implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version" |
This file contains hidden or 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.example.coroutinelearning.fragments | |
| import android.os.Bundle | |
| import android.view.LayoutInflater | |
| import android.view.View | |
| import android.view.ViewGroup | |
| import androidx.fragment.app.Fragment | |
| import androidx.lifecycle.Observer | |
| import androidx.lifecycle.ViewModelProvider | |
| import androidx.navigation.Navigation | |
| import com.example.coroutinelearning.R | |
| import com.example.coroutinelearning.databinding.DashboardBinding | |
| import com.example.coroutinelearning.entity.User | |
| import com.example.coroutinelearning.viewModels.UserViewModel | |
| class DashboardFragment : Fragment() { | |
| private val TAG = DashboardFragment::class.simpleName | |
| private lateinit var binding: DashboardBinding | |
| private lateinit var mUserViewModel: UserViewModel | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| mUserViewModel = ViewModelProvider(this).get(UserViewModel::class.java) | |
| } | |
| override fun onCreateView( | |
| inflater: LayoutInflater, | |
| container: ViewGroup?, | |
| savedInstanceState: Bundle? | |
| ): View? { | |
| binding = DashboardBinding.inflate(inflater, container, false) | |
| return binding.root | |
| } | |
| override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
| super.onViewCreated(view, savedInstanceState) | |
| bindView() | |
| saveUserDetail() | |
| viewUserDetail() | |
| } | |
| private fun viewUserDetail() { | |
| mUserViewModel.viewUserDetail().observe(requireActivity(), Observer { userList -> | |
| userList?.let { | |
| if (userList.size > 0) { | |
| binding.txtName.text = userList[0].name | |
| } | |
| } | |
| }) | |
| } | |
| private fun saveUserDetail() { | |
| val user = User(1, "freedom", "8117087821") | |
| mUserViewModel.saveUserDetail(user) | |
| } | |
| private fun bindView() { | |
| binding.txtName.text = getString(R.string.uselessText) | |
| binding.btnSubmit.setOnClickListener { | |
| Navigation.findNavController(requireView()) | |
| .navigate(R.id.action_dashboardFragment_to_userDetailFragment) | |
| } | |
| } | |
| } |
This file contains hidden or 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.example.coroutinelearning.entity | |
| import androidx.room.ColumnInfo | |
| import androidx.room.Entity | |
| import androidx.room.PrimaryKey | |
| @Entity(tableName = "user") | |
| data class User( | |
| @PrimaryKey val id: Int, | |
| val name: String, | |
| @ColumnInfo(name = "mobNo", defaultValue = "8117087821") val mobileNumber: String | |
| ) |
This file contains hidden or 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.example.coroutinelearning.database | |
| import androidx.room.Dao | |
| import androidx.room.Delete | |
| import androidx.room.Insert | |
| import androidx.room.OnConflictStrategy | |
| import androidx.room.Query | |
| import com.example.coroutinelearning.entity.User | |
| @Dao | |
| interface UserDao { | |
| @Query("SELECT * FROM user") | |
| suspend fun getAll(): List<User> | |
| @Query("SELECT * FROM user WHERE id IN (:userIds)") | |
| fun loadAllById(userIds: IntArray): List<User> | |
| @Insert(onConflict = OnConflictStrategy.REPLACE) | |
| suspend fun insertAll(users: List<User>): LongArray | |
| @Delete | |
| suspend fun delete(user: User) | |
| @Query("DELETE FROM user") | |
| suspend fun clearAllUser() | |
| } |
This file contains hidden or 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.example.coroutinelearning.repositories | |
| import android.app.Application | |
| import android.util.Log | |
| import com.example.coroutinelearning.database.AppDatabase | |
| import com.example.coroutinelearning.database.UserDao | |
| import com.example.coroutinelearning.entity.User | |
| class UserRepository(application: Application) { | |
| private val TAG = UserRepository::class.simpleName | |
| private val appDatabase: AppDatabase | |
| private val userDao: UserDao | |
| init { | |
| appDatabase = AppDatabase.getDatabase(application) | |
| userDao = appDatabase.getUserDao() | |
| } | |
| suspend fun saveUserDetail(user: User) { | |
| val userList = listOf(user) | |
| val insertDataSize = userDao.insertAll(userList) | |
| Log.i(TAG, "data inserted to db: $insertDataSize") | |
| } | |
| suspend fun viewUserDetail(): List<User> { | |
| return userDao.getAll() | |
| } | |
| } |
This file contains hidden or 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.example.coroutinelearning.viewModels | |
| import android.app.Application | |
| import androidx.lifecycle.AndroidViewModel | |
| import androidx.lifecycle.LiveData | |
| import androidx.lifecycle.MutableLiveData | |
| import androidx.lifecycle.viewModelScope | |
| import com.example.coroutinelearning.entity.User | |
| import com.example.coroutinelearning.repositories.UserRepository | |
| import kotlinx.coroutines.launch | |
| class UserViewModel(application: Application) : AndroidViewModel(application) { | |
| private val userRepository: UserRepository | |
| private var userListLiveData: MutableLiveData<List<User>> = MutableLiveData<List<User>>() | |
| init { | |
| userRepository = UserRepository(application) | |
| } | |
| fun saveUserDetail(user: User) { | |
| viewModelScope.launch { | |
| userRepository.saveUserDetail(user) | |
| } | |
| } | |
| fun viewUserDetail(): LiveData<List<User>> { | |
| viewModelScope.launch { | |
| val userList = userRepository.viewUserDetail() | |
| userListLiveData.value = userList | |
| } | |
| return userListLiveData | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment