SHARE
TWEET

Untitled

a guest Jul 19th, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class MainViewModel extends AndroidViewModel {
  2.  
  3.     private static SomethingDatabase database;
  4.     private LiveData<List<Something>> something;
  5.  
  6.     public MainViewModel(@NonNull Application application) {
  7.         super(application);
  8.         database = SomethingDatabase.getInstance(getApplication());
  9.         something = database.somethingDao().getAllSomething();
  10.     }
  11.  
  12.     public Something getSomethingById(int id) {
  13.         try {
  14.             return new GetSomethingTask().execute(id).get();
  15.         } catch (ExecutionException e) {
  16.             e.printStackTrace();
  17.         } catch (InterruptedException e) {
  18.             e.printStackTrace();
  19.         }
  20.         return null;
  21.     }
  22.  
  23.     public void deleteAllSomething() {
  24.         new DeleteSomethingTask().execute();
  25.     }
  26.  
  27.     public void insertSomething(Something something) {
  28.         new InsertTask().execute(something);
  29.     }
  30.  
  31.     public void deleteSomething(Something something) {
  32.         new DeleteTask().execute(something);
  33.     }
  34.  
  35.     public LiveData<List<Something>> getSomething() {
  36.         return something;
  37.     }
  38.  
  39.     private static class DeleteTask extends AsyncTask<Something, Void, Void> {
  40.         @Override
  41.         protected Void doInBackground(Something... something) {
  42.             if (something != null && something.length > 0) {
  43.                 database.somethingDao().deleteSomething(something[0]);
  44.             }
  45.             return null;
  46.         }
  47.     }
  48.  
  49.     private static class InsertTask extends AsyncTask<Something, Void, Void> {
  50.         @Override
  51.         protected Void doInBackground(Something... something) {
  52.             if (something != null && something.length > 0) {
  53.                 database.somethingDao().insertSomething(something[0]);
  54.             }
  55.             return null;
  56.         }
  57.     }
  58.  
  59.     private static class DeleteSomethingTask extends AsyncTask<Void, Void, Void> {
  60.         @Override
  61.         protected Void doInBackground(Void... integers) {
  62.             database.somethingDao().deleteAllSomething();
  63.             return null;
  64.         }
  65.     }
  66.  
  67.     private static class GetSomethingTask extends AsyncTask<Integer, Void, Something> {
  68.         @Override
  69.         protected Something doInBackground(Integer... integers) {
  70.             if (integers != null && integers.length > 0) {
  71.                 return database.somethingDao().getSomethingById(integers[0]);
  72.             }
  73.             return null;
  74.         }
  75.     }
  76. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top