TP-70565 | Sidharth Bamba | Category list caching (#11539)

This commit is contained in:
Sidharth Bamba
2024-06-28 14:09:15 +05:30
committed by GitHub
parent 0e8c634815
commit a1599b1f8b
3 changed files with 193 additions and 2 deletions

View File

@@ -0,0 +1,29 @@
/*
*
* * Copyright © 2024 by Navi Technologies Limited
* * All rights reserved. Strictly confidential
*
*/
package com.navi.bbps.common
import com.google.gson.GsonBuilder
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import com.navi.common.CommonLibManager
import java.lang.reflect.Type
import java.nio.charset.StandardCharsets
import javax.inject.Inject
import org.joda.time.DateTime
class LocalJsonDataSource @Inject constructor() {
private val gson =
GsonBuilder().registerTypeAdapter(DateTime::class.java, DateTimeConverterAdapter()).create()
fun <T> getDataFromJsonFile(type: Type, jsonKey: String, filePath: Int): T {
val inputStream = CommonLibManager.application.resources.openRawResource(filePath)
val dataString = String(inputStream.readBytes(), StandardCharsets.UTF_8)
val jsonElement = (JsonParser.parseString(dataString) as? JsonObject)?.get(jsonKey)
return gson.fromJson(jsonElement, type)
}
}

View File

@@ -9,10 +9,13 @@ package com.navi.bbps.feature.category
import androidx.lifecycle.viewModelScope
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.navi.base.cache.model.NaviCacheAltSourceEntity
import com.navi.base.cache.repository.NaviCacheRepository
import com.navi.base.utils.ResourceProvider
import com.navi.base.utils.isNotNull
import com.navi.bbps.R
import com.navi.bbps.common.LocalJsonDataSource
import com.navi.bbps.common.NaviBbpsScreen
import com.navi.bbps.common.model.NaviBbpsVmData
import com.navi.bbps.common.viewmodel.NaviBbpsBaseVM
@@ -26,8 +29,10 @@ import com.navi.bbps.network.di.NaviBbpsGsonBuilder
import com.navi.common.constants.DBCacheConstants
import com.navi.common.di.CoroutineDispatcherProvider
import com.navi.common.firebaseremoteconfig.FirebaseRemoteConfigHelper
import com.navi.common.network.models.isSuccess
import com.navi.common.network.models.isSuccessWithData
import com.navi.common.utils.log
import com.navi.rr.utils.ext.toJson
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject
import kotlinx.coroutines.flow.MutableStateFlow
@@ -45,6 +50,7 @@ constructor(
private val myBillsSyncJob: MyBillsSyncJob,
private val myBillsRepository: MyBillsRepository,
private val bbpsResourceProvider: ResourceProvider,
private val localJsonDataSource: LocalJsonDataSource,
@NaviBbpsGsonBuilder private val naviBbpsGson: Gson
) :
NaviBbpsBaseVM(
@@ -87,8 +93,6 @@ constructor(
private fun fetchBillCategories() {
viewModelScope.launch(dispatcherProvider.io) {
updateBillCategoriesState(BillCategoryState.Loading)
val billCategories =
naviCacheRepository.getDataOrFetchFromAltSource(
key = DBCacheConstants.NAVI_BBPS_CATEGORIES_LIST_CACHE_KEY,
@@ -120,6 +124,20 @@ constructor(
}
private suspend fun fetchCategoriesFromNetwork(): NaviCacheAltSourceEntity {
val localJsonDataSource =
localJsonDataSource.getDataFromJsonFile<BillCategoriesResponse>(
type = object : TypeToken<BillCategoriesResponse>() {}.type,
jsonKey = "billerCategoryResponse",
filePath = R.raw.fetch_bill_categories
)
if (localJsonDataSource.isNotNull()) {
updateBillCategoriesState(
BillCategoryState.Loaded(
billCategories = mapBillCategoriesResponseToEntities(localJsonDataSource)
)
)
}
val billCategoriesResponse = billCategoriesRepository.fetchBillCategories()
if (!billCategoriesResponse.isSuccessWithData()) {
notifyError(billCategoriesResponse)

View File

@@ -0,0 +1,144 @@
{
"billerCategoryResponse": {
"categoryGroups": [
{
"categories": [
{
"billerListAllHeading": "All network operators",
"billerListStateHeading": "Network operators in {state}",
"categoryId": "MOBILE-POSTPAID",
"iconUrl": "https://public-assets.prod.navi-sa.in/bbps/utility_postpaid_icon.png",
"order": 3,
"searchBoxPlaceholderText": "network operators",
"title": "Postpaid"
},
{
"billerListAllHeading": "All network operators",
"billerListStateHeading": "Network operators in {state}",
"categoryId": "MOBILE-PREPAID",
"iconUrl": "https://public-assets.prod.navi-sa.in/bbps/mobile_recharge_icon.png",
"order": 1,
"searchBoxPlaceholderText": "network operators",
"title": "Mobile recharge"
},
{
"billerListAllHeading": "All service providers",
"billerListStateHeading": "Service providers in {state}",
"categoryId": "DTH",
"iconUrl": "https://public-assets.prod.navi-sa.in/bbps/recharge_dth_icon.png",
"order": 2,
"searchBoxPlaceholderText": "service providers",
"title": "DTH"
},
{
"billerListAllHeading": "All service providers",
"billerListStateHeading": "Service providers in {state}",
"categoryId": "FASTAG",
"iconUrl": "https://public-assets.prod.navi-sa.in/bbps/recharge_fastag_icon.png",
"order": 4,
"searchBoxPlaceholderText": "service providers",
"title": "Fastag recharge"
}
],
"groupId": "Recharges",
"order": 1,
"title": "Recharges"
},
{
"categories": [
{
"billerListAllHeading": "All insurers",
"billerListStateHeading": "Insurers in {state}",
"categoryId": "INSURANCE",
"iconUrl": "https://public-assets.prod.navi-sa.in/bbps/finance_insurance_icon.png",
"order": 13,
"searchBoxPlaceholderText": "insurers",
"title": "LIC / Insurance"
},
{
"billerListAllHeading": "All lenders",
"billerListStateHeading": "Lenders in {state}",
"categoryId": "LOAN",
"iconUrl": "https://public-assets.prod.navi-sa.in/bbps/finance_loan_icon.png",
"order": 12,
"searchBoxPlaceholderText": "lenders",
"title": "Loan repayment"
},
{
"billerListAllHeading": "All billers",
"billerListStateHeading": "Billers in {state}",
"categoryId": "CREDIT-CARD",
"iconUrl": "https://public-assets.prod.navi-sa.in/bbps/finance_creditcard_icon.png",
"order": 11,
"searchBoxPlaceholderText": "billers",
"title": "Credit card"
}
],
"groupId": "Finances",
"order": 3,
"title": "Finances"
},
{
"categories": [
{
"billerListAllHeading": "All billers",
"billerListStateHeading": "Billers in {state}",
"categoryId": "WATER",
"iconUrl": "https://public-assets.prod.navi-sa.in/bbps/utility_water_icon.png",
"order": 5,
"searchBoxPlaceholderText": "billers",
"title": "Water"
},
{
"billerListAllHeading": "All network operators",
"billerListStateHeading": "Network operators in {state}",
"categoryId": "BROADBAND POSTPAID",
"iconUrl": "https://public-assets.prod.navi-sa.in/bbps/utility_broadband_icon.png",
"order": 7,
"searchBoxPlaceholderText": "network operators",
"title": "Broadband"
},
{
"billerListAllHeading": "All service providers",
"billerListStateHeading": "Service providers in {state}",
"categoryId": "GAS",
"iconUrl": "https://public-assets.prod.navi-sa.in/bbps/utility_pipedgas_icon.png",
"order": 10,
"searchBoxPlaceholderText": "service providers",
"title": "Piped gas"
},
{
"billerListAllHeading": "All operators",
"billerListStateHeading": "Operators in {state}",
"categoryId": "CABLE",
"iconUrl": "https://public-assets.prod.navi-sa.in/bbps/recharge_cable.png",
"order": 9,
"searchBoxPlaceholderText": "operators",
"title": "Cable"
},
{
"billerListAllHeading": "All billers",
"billerListStateHeading": "Billers in {state}",
"categoryId": "ELECTRICITY",
"iconUrl": "https://public-assets.prod.navi-sa.in/bbps/utility_electricity_icon.png",
"order": 6,
"searchBoxPlaceholderText": "billers",
"title": "Electricity"
},
{
"billerListAllHeading": "All billers",
"billerListStateHeading": "Billers in {state}",
"categoryId": "LPG-GAS",
"iconUrl": "https://public-assets.prod.navi-sa.in/bbps/utility_cylinder_icon.png",
"order": 8,
"searchBoxPlaceholderText": "billers",
"title": "Book a cylinder"
}
],
"groupId": "Utilities",
"order": 2,
"title": "Utilities"
}
]
}
}