converter to convert TimerJson to TimerInfo

This commit is contained in:
lbarraga 2023-04-16 23:27:05 +02:00
parent ce57425957
commit 505f5c882a

View file

@ -4,26 +4,50 @@ import be.ugent.sel.studeez.data.local.models.timer_info.*
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
class JsonToTimerConverter {
/**
* Wordt gebruikt door de ConfigurationService en door de TimerDAO.
*
* ConfigurationService: configuration wordt gefetched als een json-string,
* die wordt omgezet naar een TimerJson, die hier wordt omgezet naar de juiste TimerInfo
*
* timerDAO: Timers worden direct naar TimerJson gefetched, die hier ook worden omgezet naar
* de juiste timerInfo
*/
class ToTimerConverter {
private val timerInfoMap: Map<String, TimerFactory> = mapOf(
"endless" to TimerFactory { EndlessTimerInfo(it.name, it.description) },
"custom" to TimerFactory { CustomTimerInfo(it.name, it.description, it.studyTime) },
"break" to TimerFactory { PomodoroTimerInfo(
fun interface TimerFactory {
fun makeTimer(map: TimerJson) : TimerInfo
}
private val timerInfoMap: Map<TimerType, TimerFactory> = mapOf(
TimerType.ENDLESS to TimerFactory { EndlessTimerInfo(
it.name,
it.description,
it.id
) },
TimerType.CUSTOM to TimerFactory { CustomTimerInfo(
it.name,
it.description,
it.studyTime,
it.id
) },
TimerType.BREAK to TimerFactory { BreakTimerInfo(
it.name,
it.description,
it.studyTime,
it.breakTime,
it.repeats
it.repeats,
it.id
) }
)
private fun getTimer(timerJson: TimerJson): TimerInfo{
return timerInfoMap.getValue(timerJson.type).makeTimer(timerJson)
val type: TimerType = TimerType.valueOf(timerJson.type.uppercase())
return timerInfoMap.getValue(type).makeTimer(timerJson)
}
fun convertToTimerInfoList(a: List<TimerJson>): List<TimerInfo> {
return a.map(this::getTimer)
fun convertToTimerInfoList(timerJsonList: List<TimerJson>): List<TimerInfo> {
return timerJsonList.map(this::getTimer)
}
fun jsonToTimerJsonList(json: String): List<TimerJson> {