Leonid32

Untitled

May 18th, 2020
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.62 KB | None | 0 0
  1. public class UserRepository {
  2.  
  3. final String TAG ="myLog";
  4. private String Name;
  5. private String LastName;
  6. private String Birthday;
  7. private String Avatr_url;
  8. private int number;
  9.  
  10. private UserDao mUserDao;
  11. private LiveData<User> AllUser;
  12. private RetrofitApi retrofitApi;
  13. private UserRepository userRepository;
  14.  
  15.  
  16. public UserRepository getInstance(){
  17. if(userRepository == null){
  18. userRepository = new UserRepository();
  19. }
  20. return userRepository;
  21. }
  22.  
  23. public UserRepository(){
  24. retrofitApi = RetrofitService.cteateService(RetrofitApi.class);
  25. }
  26.  
  27. public UserRepository(Application application){
  28. Database65 db = Database65.getDatabase65(application);
  29. mUserDao = db.userDao();
  30. AllUser = mUserDao.getAll();
  31. retrofitApi = RetrofitService.cteateService(RetrofitApi.class);
  32. }
  33.  
  34. public LiveData<User> getAllUser(){
  35. return AllUser;
  36. }
  37.  
  38. public MutableLiveData<Response> getNews(){
  39. MutableLiveData<Response> newsData = new MutableLiveData<>();
  40. retrofitApi.example()
  41. .enqueue(new Callback<Example>() {
  42. @Override
  43. public void onResponse(Call<Example> call, retrofit2.Response<Example> response) {
  44. Example example = response.body();
  45.  
  46.  
  47. int num = example.response.size();
  48.  
  49. for (int i =0; i< num; i++){
  50. number = i;
  51. // Log.d(TAG, String.valueOf(number));
  52.  
  53. Name = example.response.get(i).fName;
  54. LastName = example.response.get(i).lName;
  55. Birthday = example.response.get(i).birthday;
  56. Avatr_url = example.response.get(i).avatrUrl;
  57.  
  58. /* int siz = example.response.get(i).specialty.size();
  59. for (int j = 0; j < siz; j++){
  60. Specialty[i] = example.response.get(i).specialty.get(j).name;
  61. SpecialtyId[i] = String.valueOf(example.response.get(i).specialty.get(j).specialtyId);
  62. }*/
  63. }
  64. }
  65.  
  66. @Override
  67. public void onFailure(Call<Example> call, Throwable t) {
  68.  
  69. }
  70. });
  71.  
  72. return newsData;
  73. }
  74. }
  75.  
  76.  
  77.  
  78. @Database(entities = {User.class},version = 1,exportSchema = false)
  79. public abstract class Database65 extends RoomDatabase {
  80.  
  81. public abstract UserDao userDao();
  82.  
  83. private static volatile Database65 INSTANCE;
  84. private static final int NUMBER_OF_THREADS = 4;
  85.  
  86. public static final ExecutorService databaseWrieExecutor =
  87. Executors.newFixedThreadPool(NUMBER_OF_THREADS);
  88.  
  89. public static Database65 getDatabase65(final Context context){
  90. if(INSTANCE == null) {
  91. synchronized (Database65.class) {
  92. if (INSTANCE == null) {
  93. INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
  94. Database65.class,"database")
  95. .addCallback(sRoomDatabaseCallback)
  96. .build();
  97. }
  98. }
  99. }
  100. return INSTANCE;
  101. }
  102.  
  103. private static RoomDatabase.Callback sRoomDatabaseCallback = new
  104. RoomDatabase.Callback() {
  105. @Override
  106. public void onOpen(@NonNull SupportSQLiteDatabase db){
  107. super.onOpen(db);
  108. databaseWrieExecutor.execute(()->{
  109. /* UserDao dao = INSTANCE.userDao();
  110. User user = new User(1,"Вася", "петров",
  111. "12.03.2019", "fsfds");
  112. dao.insertAll(user);*/
  113. });
  114. }
  115. };
  116. }
  117.  
  118.  
  119. public class UserViewModel extends AndroidViewModel {
  120.  
  121.  
  122. private MutableLiveData<Response> mutableLiveData;
  123. private Response response;
  124. private UserRepository userRepository;
  125. private LiveData<User> AllUser;
  126.  
  127. public UserViewModel(Application application){
  128. super(application);
  129. userRepository = new UserRepository(application);
  130. AllUser = userRepository.getAllUser();
  131. }
  132.  
  133. public void init(){
  134. if (mutableLiveData != null){
  135. return;
  136. }
  137. userRepository = userRepository.getInstance();
  138. mutableLiveData = userRepository.getNews();
  139.  
  140. }
  141.  
  142. public LiveData<User> getAllUser(){
  143. return AllUser;
  144. }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment