Reimplementing CrytpoNews in Native Android(UPDATE: Storing News in DB)

in #utopian-io4 years ago (edited)

Recently, @mathemandy and I started working on re-implementing the CryptoNews app built by @Johnesan in native android.

The aim of the app is to fetch news from different wordpress websites and display in the application. While the news are displayed, the app gives you the ability to click and view each news within the app. Instead of taking you out of the app, it gives you the comfort of viewing within the app.



  • Storing of news data in the database

In the previous implementation, the news were fetched directly from internet and displayed. But anytime the app is restarted or the device screen is rotated, the news are re-fetched from the source, this is a bad user-experience and also wasteful because this consumes more of the user's mobile data.

So, the proper way to handle this is persisting the data .i.e storing the news in the database. How will that be accomplished? In 2017, Google released a new persisting component called Room.


From the above image, the news are fetched from the internet and while it's getting data, it displays a Progress Bar to indicate to the user that something is happening. At the restart of the app, the news are fetched from the database. You could notice the mobile data is switched off to confirm that it's actually working.

This is how it was implemented

  • Firstly, the News POJO class was annotated with @Entity(tableName = "news")
    The @Entity represents a table within the db.
@Entity(tableName = "news")
public class News{

    private String date;

    private String link;

    private Title title;

    private Embedded embedded;

    private int id;

    private Guid guid;

  • Secondly, an interface class called @Dao was created. This contains the methods used in accessing the db like @Insert, @Query, @Delete, etc.
public interface NewsDao {

    @Insert(onConflict = REPLACE)
    void save(News news);

    @Query("SELECT * FROM news")
    LiveData<List<News>> queryNews();

  • Thirdly, an abstract class @Database was created as the main access point for the underlying connection to the app's persisted data.
@Database(entities = {News.class}, version = 1)
public abstract class NewsDatabase extends RoomDatabase {
    public abstract NewsDao newsDao();
  • Fourthly, in the repository class, all logic is done in fetching and storing data.
    The setupNewsListener method is called to check if more news should be fetched in order to update the UI. Else it fetches the stored data from the db.
 public class NewsRepository {

    NewsApiService newsApiService;

    PreferenceUtils preferenceUtils;

    private Executor diskExecutor;
    private Executor mainThreadExecutor;
    AppExecutors appExecutors;
    NewsDao newsDao;

    private MediatorLiveData<List<News>> newsLiveData = new MediatorLiveData<>();
    private LiveData<List<News>> newdDbSource;
    private static  final String TAG = NewsRepository.class.getSimpleName();

    NewsRepository(AppExecutors appExecutors, NewsDao newsDao) {
        this.appExecutors = appExecutors;
        diskExecutor = appExecutors.getDiskExecutor();
        mainThreadExecutor = appExecutors.getMainThreadExecutor();
        this.newsDao = newsDao;
        newdDbSource = loadUserNewsFromDb();


    private LiveData<List<News>> loadUserNewsFromDb() {
        Log.d("TAG", "Load User Deals from DB");
        return  newsDao.queryNews();

    public void getAllNews() {

        Observable<List<News>> getNews = NetworkModule.api.getLatestNews().subscribeOn(;
        Observable<List<News>> getNews2 = NetworkModule.api2.getLatestNews().subscribeOn(;
        Observable<List<News>> getNews3 = NetworkModule.api3.getLatestNews().subscribeOn(;
        Observable<List<News>> getNews4 = NetworkModule.api4.getLatestNews().subscribeOn(;, getNews2, getNews3, getNews4,
                new Function4<List<News>, List<News>, List<News>, List<News>, List<News>>() {
                    public List<News> apply(List<News> news, List<News> news2, List<News> news3, List<News> news4) throws Exception {
                        List<News> newsList = new ArrayList<>();
                        for (News newsObj : news) {
                        for (News newsObj : news2) {
                        for (News newsObj : news3) {
                        for (News newsObj : news4) {

                        return newsList;
                // Run on a background thread
                // Be notified on the main thread
                .subscribe(new Observer<List<News>>() {
                               public void onSubscribe(Disposable d) {


                               public void onNext(List<News> newsList) {


                               public void onError(Throwable e) {


                               public void onComplete() {


    private void getNewsForuser(List<News> newsList) {
        Log.e(TAG, "Loading News for user was successful");
        Log.e(TAG, "Now Saving");
        diskExecutor.execute(() -> {
            mainThreadExecutor.execute(() -> {
                newsLiveData.addSource(loadUserNewsFromDb(), this::setValueNews);


    private void saveUserDealsTODb(List<News> newsList) {
        Log.d("TAG", "Saving User Deals to DB");

            for (News news : newsList){


    private void fetchFromServer(){
        Log.d("TAG", "Fetching Deals From Server");

    private void setupNewsListener(){
        newsLiveData.addSource(newdDbSource,  newsList -> {
            Log.d("TAG", "News Livedata Listener");
            if (shouldFetchNews(newsList)){
            }else {


    private void setValueNews(List<News> newsValue) {
        if (newsValue != null){
            mainThreadExecutor.execute(() -> newsLiveData.setValue(newsValue));

    private boolean shouldFetchNews(List<News> newsList){
        long lastFetched = preferenceUtils.getLastTimeNewsWasStored();
        long timeOut = TimeUnit.MINUTES.toMillis(30) + lastFetched;
        return  newsList.size() <1 || Calendar.getInstance().getTimeInMillis() > timeOut;

    public LiveData<List<News>> asLiveData(){
        return newsLiveData;



Github Pull Request:

Android Apk File:


  • Searching all news
  • Persisting user news
  • Providing different layouts
  • Providing different themes for user
  • Push Notifications
  • News Posts sharing
  • Incorporating more news and giving the user the flexibility of deciding what he wants to read per time.

Posted on - Rewarding Open Source Contributors


Thank you for the contribution. It has been approved.

Great post, very descriptive! Keep up the good work!

You can contact us on Discord.

Hey @princessdharmy I am @utopian-io. I have just upvoted you!


  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!


Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord