@WorkerThread public void getSpecificSection( Context context, int sectionPos, OnSectionDetailReadyListener listener) { SectionModel section = mTopSections.get(sectionPos); String sectionName = section.getName(); if (CommonUtil.isEmpty(mDetailSections) || !mDetailSections.containsKey(sectionName)) { if (CommonUtil.checkInternetConnection(context)) { // try downloading data from web RawViaplayModel detail = null; try { detail = getSectionFromWeb(section.getUrl()); populateSectionDetailCache(section, detail); returnSectionDetailToListener(sectionName, listener); DatabaseManager.getInstance() .saveDataToDb(SectionDetailModel.generateFrom(detail, section)); } catch (IOException e) { Log.e(TAG, "Error while downloading section"); } } else { // try retrieving data from db SectionDetailModel detail = DatabaseManager.getInstance().getSectionDetailFromDb(section); populateSectionDetailCache(section, detail); returnSectionDetailToListener(sectionName, listener); } } else { Log.d(TAG, "Retrieving data from cache"); returnSectionDetailToListener(sectionName, listener); } }
private void populateSectionDetailCache(SectionModel section, RawViaplayModel rawModel) { mDetailSections.put(section.getName(), SectionDetailModel.generateFrom(rawModel, section)); }