LiveData is null always when get from Flow of preferencesDataStore - android

72 Views Asked by At

I am using from preferencesDataStore like bellow:

val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = PREFERENCES_NAME)
class DataStoreOperationsImpl(context: Context) : DataStoreOperations {
private object PreferencesKey {val custom_font = stringPreferencesKey(name = TYPE_FONT)}
private val dataStore = context.dataStore
    override fun getFonts(): Flow<CustomTypeFont> {
        return dataStore.data
            .catch { exception ->
                if (exception is IOException) {
                    emit(emptyPreferences())
                } else {
                    throw exception
                }
            }
            .map { preferences ->
                CustomTypeFont(
                    preferences[PreferencesKey.custom_font] ?: TYPE_FONT
                )
            } .onStart { emit(CustomTypeFont(TYPE_FONT)) } 
    }
}

And:

interface DataStoreOperations {fun getFonts(): Flow<CustomTypeFont>}

And:

class DataStoreRepository @Inject constructor(
    private val dataStore: DataStoreOperations
) {
fun getFontInvoked(): Flow<CustomTypeFont> {
        return dataStore.getFonts()
    }
}

And:

class GetCustomTypeFontsUseCase(
    private val dataStoreRepository: DataStoreRepository
) {
    operator fun invoke(): Flow<CustomTypeFont> {
        return dataStoreRepository.getFontInvoked()
    }
}

And:

data class DataStoreUseCases @Inject constructor(
val getCustomTypeFonts: GetCustomTypeFontsUseCase
)

And:

@HiltViewModel
class AccountViewModel @Inject constructor(
    private val app: Application,
    private val dataStoreUseCases: DataStoreUseCases
) : ViewModel() {
val customFont: LiveData<CustomTypeFont> = dataStoreUseCases.getCustomTypeFonts().asLiveData().map { it }
}

And in my xml I am using:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>

        <variable
            name="FT"
            type="com.my.amvulibrary.presentation.ui.fragments.account.viewmodel.AccountViewModel" />

    </data>
                <TextView
                android:id="@+id/txtTitleAccount"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:layout_marginRight="20dp"
                android:ellipsize="marquee"
                android:singleLine="true"
                android:text="@string/settings"
                android:textColor="#FFFFFF"
                android:textSize="20sp"
                android:textStyle="bold"
                app:fonta="@{FT.customFont}" />
</androidx.constraintlayout.widget.ConstraintLayout>

And in my fragment is:

companion object {
    @JvmStatic
    @BindingAdapter("app:fonta")
    fun setFont(textView: TextView, fontName: LiveData<CustomTypeFont>?) {
        fontName?.value?.let { font ->
            val typeface = Typeface.createFromAsset(
                textView.context.assets,
                "fonts/${font.tpFont}"
            )
            textView.typeface = typeface
        }
    }
}

But fontName in setFont is null always. What can I do?

0

There are 0 best solutions below