//database helper
@Database(entities = {Contact.class}, version = 1, exportSchema = false)
@TypeConverters(AddressTypeConverter.class)
public abstract class TaskDatabase extends RoomDatabase {
public abstract ContactDao taskDao();
public static TaskDatabase sInstance;
// Get a database instance public static synchronized TaskDatabase getDatabaseInstance(Context context) {
if (sInstance == null) {
sInstance = create(context);
}
return sInstance;
}
// Create the database static TaskDatabase create(Context context) {
Builder<TaskDatabase> builder = Room.databaseBuilder(context.getApplicationContext(),
TaskDatabase.class,
"Contact");
return builder.build();
}
}
public class ContactDatabaseViewModel extends AndroidViewModel {
private ContactDao mTaskDao;
public ContactDatabaseViewModel(@NonNull Application application) {
super(application);
mTaskDao = TaskDatabase.getDatabaseInstance(application).taskDao();
}
public int createFirstRecord() {
Contact model = new Contact();
model.setId(1);
return (int) mTaskDao.createFirstRecord(model);
}
/* public int reset() { mTaskDao.reset(); return (int) createFirstRecord(); }*/
public LiveData<String> getNameFromDatabase() {
return mTaskDao.getNameFromDatabase();
}
public Contact getContact() {
return mTaskDao.getContact();
}
public LiveData<String> getEmailId() {
return mTaskDao.getEmailId();
}
public LiveData<String> getMobileNo() {
return mTaskDao.getMobileNo();
}
public LiveData<Boolean> getIsAlternateAddress() {
return mTaskDao.isAlternateAddress();
}
public LiveData<Address> getAddress() {
return mTaskDao.getAddress();
}
public Address getAddress1() {
return mTaskDao.getAddress1();
}
public int updateAlterNateAddress(Boolean state) {
return (int) mTaskDao.updateAlterNateAddress(state);
}
public long updateName(Contact model) {
return (int) mTaskDao.updateName(model.getName());
}
public long updateEmailId(String value) {
return (int) mTaskDao.updateEmailId(value);
}
public long updateMobileNo(String value) {
return (int) mTaskDao.updatemobileNo(value);
}
public long updateAddress(String key, String value) {
Address address = mTaskDao.getAddress1();
if (address == null) {
address = new Address();
}
switch (key) {
case "state":
address.setState(value);
break;
case "district":
address.setDistrict(value);
break;
case "location":
address.setLocation(value);
break;
case "pincode":
if (value.equals(""))
value = "0";
address.setPincode(Integer.parseInt(value));
break;
}
return (int) mTaskDao.updateAddress(address);
}
}
//to qury fire
ew AsyncTask<String, Integer, String>() {
@Override protected String doInBackground(String... args) {
Contact model = new Contact();
model.setName(args[0]);
contactDatabaseViewModel = ViewModelProviders.of(context).get(ContactDatabaseViewModel.class);
contactDatabaseViewModel.updateName(model);
return "";
}
}.execute(textView.getText().toString().trim());
//to set live data on observer
contactDatabaseViewModel.getNameFromDatabase().observe(mContext, new Observer<String>() {
@Override public void onChanged(@Nullable String s) {
if (s != null)
if (!s.equals("")) {
nameCardVisiblity = false;
notifyVisiblity(BR.nameCardVisiblity);
}
}
});