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.Gson
import com.google.gson.reflect.TypeToken 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( fun interface TimerFactory {
"endless" to TimerFactory { EndlessTimerInfo(it.name, it.description) }, fun makeTimer(map: TimerJson) : TimerInfo
"custom" to TimerFactory { CustomTimerInfo(it.name, it.description, it.studyTime) }, }
"break" to TimerFactory { PomodoroTimerInfo(
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.name,
it.description, it.description,
it.studyTime, it.studyTime,
it.breakTime, it.breakTime,
it.repeats it.repeats,
it.id
) } ) }
) )
private fun getTimer(timerJson: TimerJson): TimerInfo{ 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> { fun convertToTimerInfoList(timerJsonList: List<TimerJson>): List<TimerInfo> {
return a.map(this::getTimer) return timerJsonList.map(this::getTimer)
} }
fun jsonToTimerJsonList(json: String): List<TimerJson> { fun jsonToTimerJsonList(json: String): List<TimerJson> {