Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class UserRepository {
- final String TAG ="myLog";
- private String Name;
- private String LastName;
- private String Birthday;
- private String Avatr_url;
- private int number;
- private UserDao mUserDao;
- private LiveData<User> AllUser;
- private RetrofitApi retrofitApi;
- private UserRepository userRepository;
- public UserRepository getInstance(){
- if(userRepository == null){
- userRepository = new UserRepository();
- }
- return userRepository;
- }
- public UserRepository(){
- retrofitApi = RetrofitService.cteateService(RetrofitApi.class);
- }
- public UserRepository(Application application){
- Database65 db = Database65.getDatabase65(application);
- mUserDao = db.userDao();
- AllUser = mUserDao.getAll();
- retrofitApi = RetrofitService.cteateService(RetrofitApi.class);
- }
- public LiveData<User> getAllUser(){
- return AllUser;
- }
- public MutableLiveData<Response> getNews(){
- MutableLiveData<Response> newsData = new MutableLiveData<>();
- retrofitApi.example()
- .enqueue(new Callback<Example>() {
- @Override
- public void onResponse(Call<Example> call, retrofit2.Response<Example> response) {
- Example example = response.body();
- int num = example.response.size();
- for (int i =0; i< num; i++){
- number = i;
- // Log.d(TAG, String.valueOf(number));
- Name = example.response.get(i).fName;
- LastName = example.response.get(i).lName;
- Birthday = example.response.get(i).birthday;
- Avatr_url = example.response.get(i).avatrUrl;
- /* int siz = example.response.get(i).specialty.size();
- for (int j = 0; j < siz; j++){
- Specialty[i] = example.response.get(i).specialty.get(j).name;
- SpecialtyId[i] = String.valueOf(example.response.get(i).specialty.get(j).specialtyId);
- }*/
- }
- }
- @Override
- public void onFailure(Call<Example> call, Throwable t) {
- }
- });
- return newsData;
- }
- }
- @Database(entities = {User.class},version = 1,exportSchema = false)
- public abstract class Database65 extends RoomDatabase {
- public abstract UserDao userDao();
- private static volatile Database65 INSTANCE;
- private static final int NUMBER_OF_THREADS = 4;
- public static final ExecutorService databaseWrieExecutor =
- Executors.newFixedThreadPool(NUMBER_OF_THREADS);
- public static Database65 getDatabase65(final Context context){
- if(INSTANCE == null) {
- synchronized (Database65.class) {
- if (INSTANCE == null) {
- INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
- Database65.class,"database")
- .addCallback(sRoomDatabaseCallback)
- .build();
- }
- }
- }
- return INSTANCE;
- }
- private static RoomDatabase.Callback sRoomDatabaseCallback = new
- RoomDatabase.Callback() {
- @Override
- public void onOpen(@NonNull SupportSQLiteDatabase db){
- super.onOpen(db);
- databaseWrieExecutor.execute(()->{
- /* UserDao dao = INSTANCE.userDao();
- User user = new User(1,"Вася", "петров",
- "12.03.2019", "fsfds");
- dao.insertAll(user);*/
- });
- }
- };
- }
- public class UserViewModel extends AndroidViewModel {
- private MutableLiveData<Response> mutableLiveData;
- private Response response;
- private UserRepository userRepository;
- private LiveData<User> AllUser;
- public UserViewModel(Application application){
- super(application);
- userRepository = new UserRepository(application);
- AllUser = userRepository.getAllUser();
- }
- public void init(){
- if (mutableLiveData != null){
- return;
- }
- userRepository = userRepository.getInstance();
- mutableLiveData = userRepository.getNews();
- }
- public LiveData<User> getAllUser(){
- return AllUser;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment