Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class UserRepository {
- private final Webservice webservice;
- private final UserDao userDao;
- private final Executor executor;
- @Inject
- public UserRepository(Webservice webservice, UserDao userDao, Executor executor) {
- this.webservice = webservice;
- this.userDao = userDao;
- this.executor = executor;
- }
- public LiveData<User> getUser(String userId) {
- refreshUser(userId);
- // Returns a LiveData object directly from the database.
- return userDao.load(userId);
- }
- private void refreshUser(final String userId) {
- // Runs in a background thread.
- executor.execute(() -> {
- // Check if user data was fetched recently.
- boolean userExists = userDao.hasUser(FRESH_TIMEOUT);
- if (!userExists) {
- // Refreshes the data.
- Response<User> response = webservice.getUser(userId).execute();
- // Check for errors here.
- // Updates the database. The LiveData object automatically
- // refreshes, so we don't need to do anything else here.
- userDao.save(response.body());
- }
- });
- }
- }
- public void isVerifiedUser(int userId){
- executor.execute(() -> {
- // making request to server for verifying user
- Response<User> response = webservice.getVerifyUser(userId).execute();
- // how to update the UI like for success or error.
- //update the progress dialog also in UI class
- });
- }
Add Comment
Please, Sign In to add comment