I was having some problem with Android Architecture Component. What I am trying to do is from the view model, I will execute the functions inside repository class. Here is my view model:
private final ReservationRepository reservationRepository;
private final UserRepository userRepository;
private LiveData<List<ReservationEntity>> reservations;
@Inject
public ReservationViewModel(ReservationRepository reservationRepository, UserRepository userRepository) {
this.reservationRepository = reservationRepository;
this.userRepository = userRepository;
reservations = reservationRepository.loadReservations();
}
public LiveData<List<ReservationEntity>> getAllReservations() {
return reservations;
}
Then in my repository class:
private final ReservationDao reservationDao;
@Inject
public ReservationRepository(ReservationDao reservationDao) {
this.reservationDao = reservationDao;
}
public LiveData<List<ReservationEntity>> loadReservations() {
return reservationDao.getAllReservation();
}
In my DAO class:
@Query("SELECT * FROM reservation")
LiveData<List<ReservationEntity>> getAllReservation();
I got one AppModule to @Provide the @Inject:
@Provides
@Singleton
ReservationDatabase provideReservationDatabase(Application application) {
return Room.databaseBuilder(application,ReservationDatabase.class,
ReservationDatabase.DATABASE_NAME).allowMainThreadQueries().build();
}
@Provides
@Singleton
ReservationDao provideReservationDao(ReservationDatabase reservationDatabase) {
return reservationDatabase.reservationDao();
}
I managed to build the project successfully. However, after successfully built gradle, it does not install the apps onto the emulator. Any ideas?
Thanks!
