Created
August 12, 2022 08:32
-
-
Save ankitchauhan20/cc371e7660b397bc35a5fa2aa08bc3a7 to your computer and use it in GitHub Desktop.
room database with mvvm
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
<?xml version="1.0" encoding="utf-8"?> | |
<layout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
> | |
<data> | |
<variable | |
name="viewModel" | |
type="com.example.profilescreen.viewModel.UserDetailsViewModel" | |
/> | |
</data> | |
<androidx.constraintlayout.widget.ConstraintLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context=".view.activity.ProfileActivity"> | |
<androidx.constraintlayout.widget.ConstraintLayout | |
android:id="@+id/topLayout" | |
android:layout_width="match_parent" | |
android:layout_height="250dp" | |
android:background="@color/ui_kit_rewards_gold_90" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintTop_toTopOf="parent" | |
app:layout_constraintBottom_toTopOf="@id/scrollView" | |
> | |
<androidx.appcompat.widget.AppCompatImageView | |
android:layout_width="30dp" | |
android:layout_height="146dp" | |
android:src="@drawable/group_566" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" | |
/> | |
<androidx.appcompat.widget.AppCompatImageView | |
android:layout_width="41dp" | |
android:layout_height="68dp" | |
android:src="@drawable/group" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintBottom_toBottomOf="parent" | |
/> | |
<androidx.appcompat.widget.AppCompatTextView | |
android:id="@+id/p_information" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="50dp" | |
android:text="@string/personal_info" | |
android:fontFamily="sans-serif" | |
android:textStyle="bold" | |
android:textSize="14sp" | |
android:textColor="@color/black" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintTop_toTopOf="parent" | |
/> | |
<androidx.appcompat.widget.AppCompatImageView | |
android:id="@+id/profile_image" | |
android:layout_width="86dp" | |
android:layout_height="86dp" | |
android:layout_marginTop="41dp" | |
android:src="@drawable/group_569" | |
app:civ_border_width="2dp" | |
app:civ_border_color="#FF000000" | |
android:layout_marginStart="24dp" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintEnd_toStartOf="@+id/p_Name" | |
app:layout_constraintTop_toBottomOf="@id/p_information" | |
/> | |
<androidx.appcompat.widget.AppCompatImageView | |
android:layout_width="22dp" | |
android:layout_height="22dp" | |
android:src="@drawable/group_695" | |
app:layout_constraintBottom_toBottomOf="@id/profile_image" | |
app:layout_constraintStart_toStartOf="@id/profile_image" | |
app:layout_constraintEnd_toEndOf="@id/profile_image" | |
/> | |
<androidx.appcompat.widget.AppCompatTextView | |
android:id="@+id/p_Name" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="17dp" | |
android:layout_marginStart="20dp" | |
android:fontFamily="sans-serif" | |
android:text="@string/profile_name" | |
android:textColor="@color/greens_house_green" | |
android:textSize="16sp" | |
android:textStyle="bold" | |
app:layout_constraintBottom_toTopOf="@id/p_rewards" | |
app:layout_constraintStart_toEndOf="@id/profile_image" | |
app:layout_constraintTop_toTopOf="@id/profile_image" /> | |
<TextView | |
android:id="@+id/p_rewards" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:fontFamily="sans-serif" | |
android:text="@string/gold_tier" | |
android:textColor="@color/ui_kit_rewards_gold" | |
android:textSize="18sp" | |
android:textStyle="bold" | |
app:layout_constraintStart_toStartOf="@id/p_Name" | |
app:layout_constraintTop_toBottomOf="@id/p_Name" | |
/> | |
</androidx.constraintlayout.widget.ConstraintLayout> | |
<androidx.core.widget.NestedScrollView | |
android:id="@+id/scrollView" | |
android:layout_width="match_parent" | |
android:fillViewport="true" | |
android:layout_height="0dp" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toBottomOf="@id/topLayout"> | |
<androidx.constraintlayout.widget.ConstraintLayout | |
android:id="@+id/bottomLayout" | |
android:layout_width="match_parent" | |
android:layout_height="573dp" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toBottomOf="@id/topLayout"> | |
<androidx.appcompat.widget.AppCompatTextView | |
android:id="@+id/f_name_text" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="50dp" | |
android:text="@string/first_name" | |
android:textColor="@color/black" | |
android:textSize="12sp" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintHorizontal_bias="0.08" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
<androidx.appcompat.widget.AppCompatEditText | |
android:id="@+id/et_firstName" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginHorizontal="24dp" | |
android:hint="@string/type" | |
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
android:textSize="15sp" | |
android:text="@={viewModel.firstName}" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="@id/f_name_text" | |
app:layout_constraintTop_toBottomOf="@id/f_name_text" /> | |
<androidx.appcompat.widget.AppCompatTextView | |
android:id="@+id/l_name_text" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="20dp" | |
android:text="@string/last_name" | |
android:textColor="@color/black" | |
android:textSize="12sp" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintHorizontal_bias="0.08" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toBottomOf="@id/et_firstName" /> | |
<androidx.appcompat.widget.AppCompatEditText | |
android:id="@+id/et_lastName" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginHorizontal="24dp" | |
android:hint="@string/type" | |
android:text="@={viewModel.lastName}" | |
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
android:textSize="15sp" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="@id/l_name_text" | |
app:layout_constraintTop_toBottomOf="@id/l_name_text" /> | |
<androidx.appcompat.widget.AppCompatTextView | |
android:id="@+id/mobile_text" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="20dp" | |
android:text="@string/mobile_number" | |
android:textAllCaps="true" | |
android:textColor="@color/black" | |
android:textSize="12sp" | |
app:layout_constraintStart_toStartOf="@id/et_Mobile" | |
app:layout_constraintTop_toBottomOf="@id/et_lastName" | |
/> | |
<androidx.appcompat.widget.AppCompatEditText | |
android:id="@+id/et_Mobile" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginHorizontal="24dp" | |
android:drawableEnd="@drawable/ui_24_edit_thick" | |
android:hint="@string/type" | |
android:inputType="phone" | |
android:maxLength="10" | |
android:text="@={viewModel.number}" | |
android:textSize="15sp" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="@id/mobile_text" | |
app:layout_constraintTop_toBottomOf="@id/mobile_text" | |
/> | |
<androidx.appcompat.widget.AppCompatButton | |
android:id="@+id/submitBtn" | |
android:layout_width="match_parent" | |
android:layout_height="40dp" | |
android:layout_marginHorizontal="24dp" | |
android:background="@drawable/button_round_shape" | |
android:fontFamily="sans-serif" | |
android:onClick="@{() -> viewModel.onClick()}" | |
android:text="@string/submit" | |
android:textAllCaps="false" | |
android:textColor="@color/white" | |
android:textSize="12sp" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toBottomOf="@+id/et_Mobile" /> | |
</androidx.constraintlayout.widget.ConstraintLayout> | |
</androidx.core.widget.NestedScrollView> | |
</androidx.constraintlayout.widget.ConstraintLayout> | |
</layout> |
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.example.profilescreen.view.activity | |
import androidx.appcompat.app.AppCompatActivity | |
import android.os.Bundle | |
import androidx.databinding.DataBindingUtil | |
import com.example.profilescreen.R | |
import com.example.profilescreen.databinding.ActivityDataBinding | |
class DataActivity : AppCompatActivity() { | |
lateinit var dataBinding:ActivityDataBinding | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_data) | |
val bundle: Bundle = intent.extras!! | |
if (bundle!=null) { | |
val first = bundle.getString("1") | |
val last = bundle.getString("2") | |
val birth = bundle.getString("3") | |
val phone = bundle.getString("4") | |
val email = bundle.getString("5") | |
dataBinding.firstName.text = first | |
dataBinding.lastname.text = last | |
dataBinding.birthDate.text = birth | |
dataBinding.mobileNumber.text = phone | |
dataBinding.emailId.text = email | |
} | |
} | |
} |
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.example.profilescreen.view.activity | |
import android.os.Bundle | |
import androidx.appcompat.app.AppCompatActivity | |
import androidx.databinding.DataBindingUtil | |
import androidx.lifecycle.ViewModelProvider | |
import com.example.profilescreen.R | |
import com.example.profilescreen.databinding.ActivityProfileBinding | |
import com.example.profilescreen.viewModel.UserDetailsViewModel | |
class ProfileActivity : AppCompatActivity() { | |
lateinit var binding: ActivityProfileBinding | |
lateinit var userDetailsViewModel : UserDetailsViewModel | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
binding = DataBindingUtil.setContentView(this, R.layout.activity_profile) | |
userDetailsViewModel = ViewModelProvider(this).get(UserDetailsViewModel::class.java) | |
binding.viewModel = userDetailsViewModel | |
userDetailsViewModel.setContext(this) | |
} | |
} |
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.example.profilescreen.model.dataModel | |
import androidx.room.ColumnInfo | |
import androidx.room.Entity | |
import androidx.room.PrimaryKey | |
@Entity(tableName = "userData") | |
data class UserDetails( | |
@PrimaryKey(autoGenerate = true) | |
var userId : Int, | |
@ColumnInfo(name = "firstname") | |
val firstName: String, | |
@ColumnInfo(name = "lastname") | |
val lastName: String, | |
@ColumnInfo(name = "number") | |
val number: Long | |
) |
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.example.profilescreen.model.room | |
import androidx.lifecycle.LiveData | |
import androidx.room.* | |
import com.example.profilescreen.model.dataModel.UserDetails | |
@Dao | |
interface UserDetailsDao { | |
@Insert | |
fun insertData(userDetails: UserDetails) | |
@Update | |
fun updateData(userDetails: UserDetails) | |
@Delete | |
fun deleteData(userDetails: UserDetails) | |
@Query("Select * from userData") | |
fun getAllData() : LiveData<UserDetails> | |
} |
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.example.profilescreen.model.room | |
import android.content.Context | |
import androidx.room.Database | |
import androidx.room.Room | |
import androidx.room.RoomDatabase | |
import com.example.profilescreen.model.dataModel.UserDetails | |
@Database(entities = [UserDetails::class], version = 1) | |
abstract class UserDetailsDatabase : RoomDatabase() { | |
abstract fun userDao() : UserDetailsDao | |
companion object{ | |
@Volatile | |
private var INSTANCE : UserDetailsDatabase? = null | |
fun getDatabase(context: Context): UserDetailsDatabase{ | |
if (INSTANCE == null){ | |
synchronized(this){ | |
INSTANCE = Room.databaseBuilder(context.applicationContext, | |
UserDetailsDatabase::class.java, "UserDB") | |
.allowMainThreadQueries() | |
.build() | |
} | |
} | |
return INSTANCE!! | |
} | |
} | |
} |
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.example.profilescreen.viewModel | |
import android.content.Context | |
import android.widget.Toast | |
import androidx.lifecycle.MutableLiveData | |
import androidx.lifecycle.ViewModel | |
import com.example.profilescreen.model.dataModel.UserDetails | |
import com.example.profilescreen.model.room.UserDetailsDatabase | |
class UserDetailsViewModel() : ViewModel() { | |
private var context: Context? = null | |
var database = UserDetailsDatabase | |
var firstName = MutableLiveData<String>("") | |
var lastName = MutableLiveData<String>("") | |
val number = MutableLiveData<String>("") | |
fun setContext(myContext: Context) { | |
this.context = myContext | |
} | |
fun onClick() { | |
if (firstName.value.isNullOrEmpty() || lastName.value.isNullOrEmpty() || number.value.isNullOrEmpty()) { | |
Toast.makeText(context, "Enter Above Values", Toast.LENGTH_SHORT).show() | |
} else { | |
val detail = UserDetails(0, firstName.value!!, lastName.value!!, number.value!!.toLong()) | |
database.getDatabase(context!!).userDao().insertData(detail) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment