Room list & event : decouple notice events formatting to be used within room controller

This commit is contained in:
ganfra
2019-05-21 13:20:11 +02:00
committed by Benoit Marty
parent 9f9f4c0755
commit b9d76f5047
14 changed files with 257 additions and 354 deletions

View File

@ -20,6 +20,7 @@ import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import com.squareup.moshi.Types
import im.vector.matrix.android.internal.di.MoshiProvider
import timber.log.Timber
import java.lang.reflect.ParameterizedType
typealias Content = Map<String, @JvmSuppressWildcards Any>
@ -27,11 +28,20 @@ typealias Content = Map<String, @JvmSuppressWildcards Any>
/**
* This methods is a facility method to map a json content to a model.
*/
inline fun <reified T> Content?.toModel(): T? {
inline fun <reified T> Content?.toModel(catchError: Boolean = true): T? {
return this?.let {
val moshi = MoshiProvider.providesMoshi()
val moshiAdapter = moshi.adapter(T::class.java)
return moshiAdapter.fromJsonValue(it)
return try {
moshiAdapter.fromJsonValue(it)
} catch (e: Exception) {
if (catchError) {
Timber.e(e, "To model failed : $e")
null
} else {
throw e
}
}
}
}