aboutsummaryrefslogblamecommitdiff
path: root/app/src/main/java/SubredditDatabase/SubredditRepository.java
blob: 2c66ad4bfbbb6d2e509a327ef045d652d7200cb3 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
                          
 

                            



                                                                   

                                       
                                                       
 

                                                                                              
                                                                                     

     

                                                    


                                                     
                                                                  

     
                                                                                       


                                           
                                           









                                                                      
package SubredditDatabase;

import android.os.AsyncTask;

import androidx.lifecycle.LiveData;

import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;

public class SubredditRepository {
    private SubredditDao mSubredditDao;
    private LiveData<SubredditData> mSubredditLiveData;

    SubredditRepository(RedditDataRoomDatabase redditDataRoomDatabase, String subredditName) {
        mSubredditDao = redditDataRoomDatabase.subredditDao();
        mSubredditLiveData = mSubredditDao.getSubredditLiveDataByName(subredditName);
    }

    LiveData<SubredditData> getSubredditLiveData() {
        return mSubredditLiveData;
    }

    public void insert(SubredditData subredditData) {
        new InsertAsyncTask(mSubredditDao).execute(subredditData);
    }

    private static class InsertAsyncTask extends AsyncTask<SubredditData, Void, Void> {

        private SubredditDao mAsyncTaskDao;

        InsertAsyncTask(SubredditDao dao) {
            mAsyncTaskDao = dao;
        }

        @Override
        protected Void doInBackground(final SubredditData... params) {
            mAsyncTaskDao.insert(params[0]);
            return null;
        }
    }
}