Skip to content

Commit

Permalink
Merge pull request #118 from suitetecsa/refactor-lib
Browse files Browse the repository at this point in the history
refactor(dataModels): refactoriza casi todos los modelos de datos
  • Loading branch information
lesclaz authored Aug 10, 2024
2 parents 9471aa9 + c7396a3 commit eb59f85
Show file tree
Hide file tree
Showing 35 changed files with 195 additions and 191 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,18 @@ import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import cu.suitetecsa.sdkandroid.R
import cu.suitetecsa.sdkandroid.presentation.balance.component.ContactsBottomSheet
import cu.suitetecsa.sdkandroid.presentation.balance.component.Spinner
import cu.suitetecsa.sdkandroid.ui.theme.SDKAndroidTheme
import io.github.suitetecsa.sdk.android.model.DataCu
import io.github.suitetecsa.sdk.android.model.MainData
import io.github.suitetecsa.sdk.android.model.SimCard
import io.github.suitetecsa.sdk.android.model.Sms
import io.github.suitetecsa.sdk.android.model.Voice
import io.github.suitetecsa.sdk.android.utils.LongUtils.asSizeString
import io.github.suitetecsa.sdk.android.utils.LongUtils.asTimeString
import cu.suitetecsa.sdkandroid.R
import cu.suitetecsa.sdkandroid.presentation.balance.component.ContactsBottomSheet
import cu.suitetecsa.sdkandroid.presentation.balance.component.Spinner
import cu.suitetecsa.sdkandroid.ui.theme.SDKAndroidTheme
import io.github.suitetecsa.sdk.android.utils.LongUtils.asRemainingDays
import io.github.suitetecsa.sdk.android.utils.asDateMillis
import io.github.suitetecsa.sdk.android.utils.isActive

@Composable
fun BalanceRoute(
Expand Down Expand Up @@ -165,7 +166,7 @@ fun BalanceInfo(
) {
Text(text = "Tarifa por consumo")
Switch(
checked = state.data?.usageBasedPricing ?: true,
checked = state.data?.consumptionRate ?: true,
onCheckedChange = onTurnUsageBasedPricing,
enabled = !state.loading
)
Expand All @@ -188,44 +189,45 @@ fun PlansSection(state: BalanceState) {
) {
state.data?.let { data ->
val dataCount = if (data.data != null && data.dataLte != null) {
"${data.data!!.asSizeString} + ${data.dataLte!!.asSizeString} LTE"
"${data.data} + ${data.dataLte} LTE"
} else if (data.data != null) {
data.data!!.asSizeString
data.data!!
} else {
"${data.dataLte!!.asSizeString} LTE"
"${data.dataLte} LTE"
}
DataPlan(
planTitle = "Datos",
dataCount = dataCount,
dataExpire = data.remainingDays?.let { "$it días" }
dataExpire = data.expires?.takeIf { it.isActive }?.asDateMillis?.asRemainingDays?.let { "$it días" }
)
}
state.voice?.let { voice ->
DataPlan(
planTitle = "Voz",
dataCount = voice.seconds.asTimeString,
dataExpire = voice.remainingDays?.let { "$it días" }
dataCount = voice.data,
dataExpire = voice.expires.takeIf { it.isActive }?.asDateMillis?.asRemainingDays?.let { "$it días" }
)
}
state.sms?.let { sms ->
DataPlan(
planTitle = "SMS",
dataCount = "${sms.sms} SMS",
dataExpire = sms.remainingDays?.let { "$it días" }
dataCount = sms.data,
dataExpire = sms.expires.takeIf { it.isActive }?.asDateMillis?.asRemainingDays?.let { "$it días" }
)
}
state.dailyData?.let { dailyData ->
DataPlan(
planTitle = "Bolsa diaria",
dataCount = dailyData.data.asSizeString,
dataExpire = dailyData.remainingHours?.let { "$it horas" }
dataCount = dailyData.data,
dataExpire = dailyData.expires.takeIf { it.isActive }?.asDateMillis?.asRemainingDays
?.let { "$it horas" }
)
}
state.mailData?.let { mailData ->
DataPlan(
planTitle = "Bolsa correo",
dataCount = mailData.data.asSizeString,
dataExpire = mailData.remainingDays?.let { "$it días" }
dataCount = mailData.data,
dataExpire = mailData.expires.takeIf { it.isActive }?.asDateMillis?.asRemainingDays?.let { "$it días" }
)
}
}
Expand All @@ -242,38 +244,37 @@ fun BonusSection(state: BalanceState) {
state.bonusCredit?.let { bonusCredit ->
DataPlan(
planTitle = "Saldo",
dataCount = "$%.2f CUP".format(bonusCredit.balance),
dataExpire = "${bonusCredit.remainingDays} dias"
dataCount = "$%.2f CUP".format(bonusCredit.data),
dataExpire = "${bonusCredit.expires.asDateMillis.asRemainingDays} dias"
)
}
state.bonusData?.let { bonusData ->
val dataCount =
if (bonusData.data != null && bonusData.dataLte != null) {
"${bonusData.data!!.asSizeString} + " +
"${bonusData.dataLte!!.asSizeString} LTE"
"${bonusData.data!!} + ${bonusData.dataLte!!} LTE"
} else if (bonusData.data != null) {
bonusData.data!!.asSizeString
bonusData.data!!
} else {
"${bonusData.dataLte!!.asSizeString} LTE"
"${bonusData.dataLte!!} LTE"
}
DataPlan(
planTitle = "Datos",
dataCount = dataCount,
dataExpire = "${bonusData.remainingDays} dias"
dataExpire = "${bonusData.expires.asDateMillis.asRemainingDays} dias"
)
}
state.dataCu?.let { bonusDataCU ->
DataPlan(
planTitle = "Datos CU",
dataCount = bonusDataCU.data.asSizeString,
dataExpire = "${bonusDataCU.remainingDays} dias"
dataCount = bonusDataCU.data,
dataExpire = "${bonusDataCU.expires.asDateMillis.asRemainingDays} dias"
)
}
state.bonusUnlimitedData?.let { bonusUnlimitedData ->
DataPlan(
planTitle = "Datos Ilimitados",
dataCount = "12:00 a.m -> 7:00 a.m",
dataExpire = "${bonusUnlimitedData.remainingDays} dias"
dataExpire = "${bonusUnlimitedData.expires.asDateMillis.asRemainingDays} dias"
)
}
}
Expand All @@ -300,10 +301,10 @@ private fun BalanceInfoPreviewDark() {
balance = 123.45f,
activeUntil = "10/10/2022",
mainBalanceDueDate = "10/10/2022",
data = MainData(false, 8345369725, 29376382496, 25),
dataCu = DataCu(236975200, 25),
voice = Voice(586314L, 25),
sms = Sms(50, 25)
data = MainData(false, "3.5 BG", "4.5 GB", "30/8/2024"),
dataCu = DataCu("300 MB", "30/8/2024"),
voice = Voice("1:00:00", "30/8/2024"),
sms = Sms("50 SMS", "30/8/2024")
)
) {}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import io.github.suitetecsa.sdk.android.model.MainData
import io.github.suitetecsa.sdk.android.model.SimCard
import io.github.suitetecsa.sdk.android.model.Sms
import io.github.suitetecsa.sdk.android.model.Voice
import io.github.suitetecsa.sdk.android.utils.LongUtils.asDateString
import io.github.suitetecsa.sdk.android.utils.smartFetchBalance
import io.github.suitetecsa.sdk.android.utils.ussdFetch
import kotlinx.coroutines.flow.SharingStarted
Expand Down Expand Up @@ -162,7 +161,7 @@ class BalancesViewModel @Inject constructor(
(response as DataBalance).usageBasedPricing,
response.data,
response.dataLte,
response.remainingDays
response.expires
),
mailData = response.mailData,
dailyData = response.dailyData
Expand All @@ -172,25 +171,25 @@ class BalancesViewModel @Inject constructor(
MESSAGES_BALANCE -> {
_state.value = _state.value.copy(
sms = Sms(
(response as MessagesBalance).sms,
response.remainingDays
(response as MessagesBalance).data,
response.expires
)
)
}

PRINCIPAL_BALANCE -> {
_state.value = _state.value.copy(
balance = (response as PrincipalBalance).balance.toFloat(),
activeUntil = response.activeUntil.asDateString,
mainBalanceDueDate = response.dueDate.asDateString,
activeUntil = response.blockDate,
mainBalanceDueDate = response.deletionDate,
)
}

VOICE_BALANCE -> {
_state.value = _state.value.copy(
voice = Voice(
(response as VoiceBalance).seconds,
response.remainingDays
(response as VoiceBalance).data,
response.expires
)
)
}
Expand Down Expand Up @@ -247,7 +246,7 @@ class BalancesViewModel @Inject constructor(
isActive,
data.data,
data.dataLte,
data.remainingDays
data.expires
)
)
} ?: run {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ fun ContactImage(
Image(
modifier = modifier.size(64.dp).clip(RoundedCornerShape(Percent)),
bitmap = it,
contentDescription = "Contact Photo")
contentDescription = "Contact Photo"
)
} ?: run {
Image(
modifier = modifier.clip(RoundedCornerShape(Percent)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,5 @@ fun Contact(
@Preview(showBackground = true)
@Composable
private fun ContactPreview() {
Contact(contact = Contact("PortalUsuario", "phoneNumber", null)){}
Contact(contact = Contact("PortalUsuario", "phoneNumber", null)) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,4 @@ fun CustomLinearProgressBar(
trackColor = Color.Transparent
)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ private fun PrettyCardPreviewDark() {
SDKAndroidTheme {
Surface(
Modifier
.background(MaterialTheme.colorScheme.background)) {
.background(MaterialTheme.colorScheme.background)
) {
PrettyCard(
modifier = Modifier.padding(16.dp),
isLoading = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue

@Composable
fun <T> Spinner(
Expand Down
2 changes: 1 addition & 1 deletion sdk-android/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ mavenPublishing {
)
)

coordinates("io.github.suitetecsa.sdk", "android", "1.0.0-alpha03")
coordinates("io.github.suitetecsa.sdk", "android", "1.0.0-alpha04")
pom {
name.set(project.name)
description.set("A tool designed to interact with ETECSA services from android app.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ internal class UssdRequestSenderImpl(private val delayMillis: Long) : UssdReques
ussdRequest,
PrincipalBalance(
balance.balance,
balance.activeUntil,
balance.dueDate,
balance.lockDate,
balance.deletionDate,
ArrayList(requestsTypes)
)
)
Expand All @@ -94,10 +94,10 @@ internal class UssdRequestSenderImpl(private val delayMillis: Long) : UssdReques
callback.onSuccess(
ussdRequest,
DataBalance(
mainData.usageBasedPricing,
mainData.consumptionRate,
mainData.data,
mainData.dataLte,
mainData.remainingDays,
mainData.expires,
parseDailyData(response),
parseMailData(response)
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
package io.github.suitetecsa.sdk.android.balance.exception

class UssdRequestException : Exception {
constructor(message: String?) : super(message)
}
class UssdRequestException(message: String?) : Exception(message)
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ import io.github.suitetecsa.sdk.android.model.BonusUnlimitedData
import io.github.suitetecsa.sdk.android.model.DataCu
import io.github.suitetecsa.sdk.android.model.Sms
import io.github.suitetecsa.sdk.android.model.Voice
import io.github.suitetecsa.sdk.android.utils.LongUtils.toRemainingDays
import io.github.suitetecsa.sdk.android.utils.StringUtils
import io.github.suitetecsa.sdk.android.utils.StringUtils.toBytes
import io.github.suitetecsa.sdk.android.utils.StringUtils.toDateMillis

object BonusBalanceParser {
/**
Expand Down Expand Up @@ -48,69 +44,46 @@ val CharSequence.asBonusBalance: BonusBalance

@RequiresApi(Build.VERSION_CODES.O)
private fun CharSequence.extractCredit() =
"""\$(?<volume>([\d.]+))\s+vence\s+(?<dueDate>(\d{2}-\d{2}-\d{2}))\.""".toRegex().find(this)?.let {
BonusCredit(
it.groups["volume"]!!.value.toDouble(),
toRemainingDays(toDateMillis(it.groups["dueDate"]!!.value))
)
}
"""\$(?<data>([\d.]+))\s+vence\s+(?<expires>(\d{2}-\d{2}-\d{2}))\.""".toRegex().find(this)
?.let { BonusCredit(it.groups["data"]!!.value, it.groups["expires"]!!.value) }

@RequiresApi(Build.VERSION_CODES.O)
private fun CharSequence.extractData(): BonusData? {
val dataCountPattern = """(\d+(\.\d+)?)(\s)*([GMK])?B"""
val dataCountGroup = """(?<volume>$dataCountPattern)"""
val dataCountLtePattern = """(?<volumeLte>$dataCountPattern)\s+LTE"""
val dataDueDatePattern = """(?<dueDate>(\d{2}-\d{2}-\d{2}))"""
val dataCountGroup = """(?<data>$dataCountPattern)"""
val dataCountLtePattern = """(?<dataLte>$dataCountPattern)\s+LTE"""
val dataDueDatePattern = """(?<expires>(\d{2}-\d{2}-\d{2}))"""
val fullDataCountPattern =
"""($dataCountGroup)?(\s+)?(\+)?(\s+)?($dataCountLtePattern)?\s+vence\s+$dataDueDatePattern(\.)?"""
val unlimitedDataPattern = """ilimitados\s+vence\s+(?<unlimitedData>(\d{2}-\d{2}-\d{2}))\."""
val dataRegex =
"""Datos:\s+($unlimitedDataPattern)?(\s+)?($fullDataCountPattern)?""".toRegex()

return dataRegex.find(this)?.let {
val volume = it.groups["volume"]?.value
val volumeLte = it.groups["volumeLte"]?.value
val dueDate = it.groups["dueDate"]!!.value
BonusData(
volume?.let { dv -> toBytes(dv) },
volumeLte?.let { dlv -> toBytes(dlv) },
toRemainingDays(toDateMillis(dueDate))
)
BonusData(it.groups["data"]?.value, it.groups["dataLte"]?.value, it.groups["expires"]!!.value)
}
}

@RequiresApi(Build.VERSION_CODES.O)
private fun CharSequence.extractUnlimitedData() =
"""ilimitados\s+vence\s+(?<dueDate>(\d{2}-\d{2}-\d{2}))\."""
.toRegex().find(this)?.let {
it.groups["dueDate"]?.value?.let { ud ->
BonusUnlimitedData(toRemainingDays(toDateMillis(ud)))
}
}
"""ilimitados\s+vence\s+(?<expires>(\d{2}-\d{2}-\d{2}))\."""
.toRegex().find(this)?.let { BonusUnlimitedData(it.groups["expires"]?.value!!) }

@RequiresApi(Build.VERSION_CODES.O)
private fun CharSequence.extractDataCu() =
"""Datos\.cu\s+(?<volume>(\d+(\.\d+)?)(\s)*([GMK])?B)?\s+vence\s+(?<dueDate>(\d{2}-\d{2}-\d{2}))\."""
"""Datos\.cu\s+(?<data>(\d+(\.\d+)?)(\s)*([GMK])?B)?\s+vence\s+(?<expires>(\d{2}-\d{2}-\d{2}))\."""
.toRegex().find(this)?.let {
DataCu(
toBytes(it.groups["volume"]!!.value),
toRemainingDays(toDateMillis(it.groups["dueDate"]!!.value))
)
DataCu(it.groups["data"]!!.value, it.groups["expires"]!!.value)
}

@RequiresApi(Build.VERSION_CODES.O)
private fun CharSequence.extractVoice() =
"""Voz:\s+(?<volume>(\d{2}:\d{2}:\d{2}))\s+vence\s+(?<dueDate>(\d{2}-\d{2}-\d{2}))\."""
"""Voz:\s+(?<data>(\d{2}:\d{2}:\d{2}))\s+vence\s+(?<expires>(\d{2}-\d{2}-\d{2}))\."""
.toRegex().find(this)?.let {
Voice(
StringUtils.toSeconds(it.groups["volume"]!!.value),
toRemainingDays(toDateMillis(it.groups["dueDate"]!!.value))
)
Voice(it.groups["data"]!!.value, it.groups["expires"]!!.value)
}

@RequiresApi(Build.VERSION_CODES.O)
private fun CharSequence.extractSms() =
"""SMS:\s+(?<volume>(\d+))\s+vence\s+(?<dueDate>(\d{2}-\d{2}-\d{2}))\."""
.toRegex().find(this)?.let {
Sms(it.groups["volume"]!!.value.toLong(), toRemainingDays(toDateMillis(it.groups["dueDate"]!!.value)))
}
"""SMS:\s+(?<data>(\d+))\s+vence\s+(?<expires>(\d{2}-\d{2}-\d{2}))\.""".toRegex().find(this)
?.let { Sms(it.groups["data"]!!.value, it.groups["expires"]!!.value) }
Loading

0 comments on commit eb59f85

Please sign in to comment.