Guest User

Untitled

a guest
Dec 18th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. public class UserRepository {
  2. private final Webservice webservice;
  3. private final UserDao userDao;
  4. private final Executor executor;
  5.  
  6. @Inject
  7. public UserRepository(Webservice webservice, UserDao userDao, Executor executor) {
  8. this.webservice = webservice;
  9. this.userDao = userDao;
  10. this.executor = executor;
  11. }
  12.  
  13. public LiveData<User> getUser(String userId) {
  14. refreshUser(userId);
  15. // Returns a LiveData object directly from the database.
  16. return userDao.load(userId);
  17. }
  18.  
  19. private void refreshUser(final String userId) {
  20. // Runs in a background thread.
  21. executor.execute(() -> {
  22. // Check if user data was fetched recently.
  23. boolean userExists = userDao.hasUser(FRESH_TIMEOUT);
  24. if (!userExists) {
  25. // Refreshes the data.
  26. Response<User> response = webservice.getUser(userId).execute();
  27.  
  28. // Check for errors here.
  29.  
  30. // Updates the database. The LiveData object automatically
  31. // refreshes, so we don't need to do anything else here.
  32. userDao.save(response.body());
  33. }
  34. });
  35. }
  36. }
  37.  
  38. public void isVerifiedUser(int userId){
  39. executor.execute(() -> {
  40. // making request to server for verifying user
  41.  
  42. Response<User> response = webservice.getVerifyUser(userId).execute();
  43.  
  44. // how to update the UI like for success or error.
  45. //update the progress dialog also in UI class
  46. });
  47. }
Add Comment
Please, Sign In to add comment