TokenBundleRepository

Repository of a bundle of information, related to auth tokens.

Samples

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProvider.AndroidViewModelFactory.Companion.APPLICATION_KEY
import androidx.lifecycle.viewModelScope
import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory
import com.google.android.horologist.auth.data.tokenshare.TokenBundleRepository
import com.google.android.horologist.auth.data.tokenshare.impl.TokenBundleRepositoryImpl
import com.google.android.horologist.auth.sample.SampleApplication
import com.google.android.horologist.auth.sample.shared.TOKEN_BUNDLE_CUSTOM_KEY
import com.google.android.horologist.auth.sample.shared.TokenBundleSerializer
import com.google.android.horologist.auth.sample.shared.model.TokenBundleProto.TokenBundle
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.runningReduce
import kotlinx.coroutines.flow.stateIn
fun main() { 
   //sampleStart 
   class TokenShareCustomKeyViewModel(
    tokenBundleRepository: TokenBundleRepository<TokenBundle?>,
) : ViewModel() {

    public val uiState: StateFlow<List<TokenBundle?>> =
        tokenBundleRepository.flow
            .map { listOf(it) }
            .runningReduce { accumulator, value -> accumulator + value }
            .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), emptyList())

    companion object {
        public val Factory: ViewModelProvider.Factory = viewModelFactory {
            initializer {
                val application = this[APPLICATION_KEY]!!

                TokenShareCustomKeyViewModel(
                    TokenBundleRepositoryImpl.create(
                        registry = (application as SampleApplication).registry,
                        serializer = TokenBundleSerializer,
                        key = TOKEN_BUNDLE_CUSTOM_KEY,
                    ),
                )
            }
        }
    }
} 
   //sampleEnd
}
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProvider.AndroidViewModelFactory.Companion.APPLICATION_KEY
import androidx.lifecycle.viewModelScope
import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory
import com.google.android.horologist.auth.data.tokenshare.TokenBundleRepository
import com.google.android.horologist.auth.data.tokenshare.impl.TokenBundleRepositoryImpl
import com.google.android.horologist.auth.sample.SampleApplication
import com.google.android.horologist.auth.sample.shared.TokenBundleSerializer
import com.google.android.horologist.auth.sample.shared.model.TokenBundleProto.TokenBundle
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.runningReduce
import kotlinx.coroutines.flow.stateIn
fun main() { 
   //sampleStart 
   class TokenShareDefaultKeyViewModel(
    tokenBundleRepository: TokenBundleRepository<TokenBundle?>,
) : ViewModel() {

    public val uiState: StateFlow<List<TokenBundle?>> =
        tokenBundleRepository.flow
            .map { listOf(it) }
            .runningReduce { accumulator, value -> accumulator + value }
            .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), emptyList())

    companion object {
        public val Factory: ViewModelProvider.Factory = viewModelFactory {
            initializer {
                val application = this[APPLICATION_KEY]!!

                TokenShareDefaultKeyViewModel(
                    TokenBundleRepositoryImpl.create(
                        registry = (application as SampleApplication).registry,
                        serializer = TokenBundleSerializer,
                    ),
                )
            }
        }
    }
} 
   //sampleEnd
}

Inheritors

Properties

Link copied to clipboard
abstract val flow: Flow<T>