get currencies from tourism api instead

This commit is contained in:
Emin 2024-07-16 11:31:47 +05:00
parent 104f02b987
commit dc0523cbb3
12 changed files with 56 additions and 99 deletions

View file

@ -28,3 +28,25 @@
# R8 crypts the source line numbers in all log messages.
# https://github.com/organicmaps/organicmaps/issues/6559#issuecomment-1812039926
-dontoptimize
# For some unknown reason we couldn't find out, requests are not working properly
# when the app is shrinked and/or minified, so we keep all of these things out from R8 effects.
-keep,allowobfuscation,allowshrinking class kotlin.coroutines.Continuation
-keep,allowobfuscation,allowshrinking interface retrofit2.Call
-keep,allowobfuscation,allowshrinking class retrofit2.Response
-if interface * { @retrofit2.http.* public *** *(...); }
-keep,allowoptimization,allowshrinking,allowobfuscation class <3>
-keep class app.tourism.data.remote.** { *; }
-keep public class app.tourism.data.dto.** {
public void set*(***);
public *** get*();
public protected private *;
}
-keep public class app.tourism.domain.models.** {
public void set*(***);
public *** get*();
public protected private *;
}

View file

@ -124,6 +124,7 @@ import static android.content.pm.PackageManager.PERMISSION_GRANTED;
import static app.organicmaps.location.LocationState.FOLLOW;
import static app.organicmaps.location.LocationState.FOLLOW_AND_ROTATE;
import static app.organicmaps.location.LocationState.LOCATION_TAG;
import static app.tourism.ui.utils.ShowToastKt.showToast;
import static app.tourism.utils.MapUtilsKt.isInsideTajikistan;
public class MwmActivity extends BaseMwmFragmentActivity

View file

@ -0,0 +1,11 @@
package app.tourism.data.dto
import app.tourism.data.db.entities.CurrencyRatesEntity
import app.tourism.domain.models.profile.CurrencyRates
data class CurrencyRatesDataDto(val data: CurrencyRatesDto) {
fun toCurrencyRates() = CurrencyRates(data.usd, data.eur, data.rub)
fun toCurrencyRatesEntity() = CurrencyRatesEntity(1, data.usd, data.eur, data.rub)
}
data class CurrencyRatesDto(val usd: Double, val eur: Double, val rub: Double)

View file

@ -1,19 +0,0 @@
package app.tourism.data.dto.currency;
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;
import java.util.List;
import app.tourism.domain.models.profile.CurrencyRates;
@Root(name = "ValCurs")
public class CurrenciesList {
@Attribute(required = false, name = "Date") public String date;
@Attribute(required = false) public String name;
@ElementList(name = "Valute", inline = true)
public List<Currency> currencies;
}

View file

@ -1,16 +0,0 @@
package app.tourism.data.dto.currency;
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;
@Root(name = "Valute")
public class Currency{
@Attribute(required = false) public String ID;
@Element(name = "CharCode") public String charCode;
@Element(name = "Nominal") public Integer nominal;
@Element(name = "Name") public String name;
@Element(name = "Value") public Double value;
}

View file

@ -1,25 +1,11 @@
package app.tourism.data.remote
import app.tourism.data.dto.currency.CurrenciesList
import app.tourism.domain.models.resource.Resource
import app.tourism.utils.getCurrentDate
import app.tourism.utils.getCurrentLocale
import com.google.gson.JsonParseException
import retrofit2.HttpException
import app.tourism.data.dto.CurrencyRatesDataDto
import retrofit2.Response
import retrofit2.http.GET
import retrofit2.http.Query
import java.io.IOException
interface CurrencyApi {
@GET("en/kurs/export_xml.php")
suspend fun getCurrency(
@Query("date") date: String = getCurrentDate(),
@Query("export") export: String = "xmlout"
): Response<CurrenciesList>
companion object {
const val BASE_URL = "http://nbt.tj/"
}
@GET("currency")
suspend fun getCurrency(): Response<CurrencyRatesDataDto>
}

View file

@ -2,14 +2,12 @@ package app.tourism.data.repositories
import android.content.Context
import app.tourism.data.db.Database
import app.tourism.data.dto.currency.CurrenciesList
import app.tourism.data.remote.CurrencyApi
import app.tourism.data.remote.handleGenericCall
import app.tourism.domain.models.profile.CurrencyRates
import app.tourism.domain.models.resource.Resource
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlin.Double.Companion.NaN
class CurrencyRepository(
private val api: CurrencyApi,
@ -26,24 +24,10 @@ class CurrencyRepository(
handleGenericCall(
call = { api.getCurrency() },
mapper = {
val currencyRates = getCurrencyRatesFromXml(it)
db.currencyRatesDao.updateCurrencyRates(currencyRates.toCurrencyRatesEntity())
currencyRates
db.currencyRatesDao.updateCurrencyRates(it.toCurrencyRatesEntity())
it.toCurrencyRates()
},
context
)
}
private fun getCurrencyRatesFromXml(it: CurrenciesList): CurrencyRates {
val currencies = it.currencies
fun findValueByCurrencyCode(code: String): Double {
return currencies.firstOrNull { it.charCode == code }?.value ?: NaN
}
val usd = findValueByCurrencyCode("USD")
val eur = findValueByCurrencyCode("EUR")
val rub = findValueByCurrencyCode("RUB")
return CurrencyRates(usd, eur, rub)
}
}
}

View file

@ -44,9 +44,10 @@ class PlacesRepository(
mapper = { data ->
// get data
val favorites =
if (favoritesResponse is Resource.Success) favoritesResponse.data?.data?.map {
it.toPlaceFull(true)
} else null
if (favoritesResponse is Resource.Success)
favoritesResponse.data?.data?.map {
it.toPlaceFull(true)
} else null
val reviews = mutableListOf<Review>()

View file

@ -26,7 +26,7 @@ import javax.inject.Singleton
object NetworkModule {
@Provides
@Singleton
fun provideApi(@Named(MAIN_OKHTTP_LABEL) okHttpClient: OkHttpClient): TourismApi {
fun provideApi(okHttpClient: OkHttpClient): TourismApi {
return Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
@ -37,8 +37,10 @@ object NetworkModule {
@Provides
@Singleton
@Named(MAIN_OKHTTP_LABEL)
fun provideHttpClient(@ApplicationContext context: Context, userPreferences: UserPreferences): OkHttpClient {
fun provideHttpClient(
@ApplicationContext context: Context,
userPreferences: UserPreferences
): OkHttpClient {
return OkHttpClient.Builder()
.addInterceptor(
HttpLoggingInterceptor()
@ -63,29 +65,13 @@ object NetworkModule {
}.build()
}
@Provides
@Singleton
@Named(CURRENCY_OKHTTP_LABEL)
fun provideHttpClientForCurrencyRetrofit(): OkHttpClient {
val okHttpClient = OkHttpClient.Builder()
okHttpClient.readTimeout(1, TimeUnit.MINUTES)
okHttpClient.connectTimeout(1, TimeUnit.MINUTES)
.addInterceptor(
HttpLoggingInterceptor()
.setLevel(HttpLoggingInterceptor.Level.BODY)
)
return okHttpClient.build()
}
@Provides
@Singleton
@Named(CURRENCY_RETROFIT_LABEL)
fun provideCurrencyRetrofit(@Named(CURRENCY_OKHTTP_LABEL) client: OkHttpClient): Retrofit {
fun provideCurrencyRetrofit(client: OkHttpClient): Retrofit {
return Retrofit.Builder()
.baseUrl(CurrencyApi.BASE_URL)
.addConverterFactory(SimpleXmlConverterFactory.create())
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build()
}
@ -97,6 +83,4 @@ object NetworkModule {
}
}
const val MAIN_OKHTTP_LABEL = "main okhttp"
const val CURRENCY_RETROFIT_LABEL = "currency retrofit"
const val CURRENCY_OKHTTP_LABEL = "currency okhttp"

View file

@ -21,6 +21,7 @@ import androidx.compose.material3.SwitchDefaults
import androidx.compose.material3.Text
import androidx.compose.material3.rememberModalBottomSheetState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf

View file

@ -2229,4 +2229,5 @@
<string name="review_will_be_published">Отзыв будет публикован когда будете онлайн</string>
<string name="review_was_published">Отзыв был успешно опубликован</string>
<string name="failed_to_publish_review">Не удалось публиковать отзыв</string>
<string name="plz_dont_go_out_of_tjk">Поажалуйста, не выходите за рамки Таджикистана</string>
</resources>

View file

@ -2250,7 +2250,7 @@
<string name="retry">Try again</string>
<string name="no_network">Couldn\'t reach the server, please check connection</string>
<string name="no_image">No image</string>
<string name="tjk">Tajikistan 🫠🌸☮</string>
<string name="tjk">Tajikistan</string>
<string name="clear_search_field">Clear search field</string>
<string name="top30">Top 30 places</string>
<string name="sights">Sights</string>
@ -2271,4 +2271,5 @@
<string name="review_will_be_published">Review will be published when you are online</string>
<string name="review_was_published">Review was successfully published</string>
<string name="failed_to_publish_review">Failed to publish review\n</string>
<string name="plz_dont_go_out_of_tjk">Please, don\'t go out of Tajikistan</string>
</resources>