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






                                       
                                                       
 
                                                                        

                                                                                  

                                                                                     

     

                                                    




















                                                                                       
package SubredditDatabase;

import android.app.Application;
import android.arch.lifecycle.LiveData;
import android.os.AsyncTask;

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

    SubredditRepository(Application application, String subredditName) {
        SubredditRoomDatabase db = SubredditRoomDatabase.getDatabase(application);
        mSubredditDao = db.subredditDao();

        mSubredditLiveData = mSubredditDao.getSubredditLiveDataByName(subredditName);
    }

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

    public void insert(SubredditData subredditData) {
        new SubredditRepository.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;
        }
    }
}