小编Tai*_*han的帖子

将流量转换为实时数据的最佳方式

我正在从 Unsplash api 实现搜索,数据将根据搜索进行更新

GalleryViewModel.kt

@HiltViewModel
class GalleryViewModel @Inject constructor(
    private val fetchPhotoUseCase:FetchPhotoUseCase,
    @Assisted state: SavedStateHandle
) :ViewModel(){

    companion object{
        private const val CURRENT_QUERY = "current_query" // key
        private const val DEFAULT_QUERY = "cats"
    }

    private val currentQuery = state.getLiveData(CURRENT_QUERY, DEFAULT_QUERY)
    
    val photos = currentQuery.switchMap { queryString ->
        liveData {
            fetchPhotoUseCase.execute(queryString).collect {  
                emit(it)
            }
        }
    }

   fun searchPhotos(query: String) {
    currentQuery.value = query
   }
}
Run Code Online (Sandbox Code Playgroud)

FetchPhotoUseCase.kt

class FetchPhotoUseCase @Inject constructor(
    private val repository: UnSplashRepository
) : UseCaseWithParams<String,Flow<PagingData<UnsplashPhoto>>>(){
    override suspend fun …
Run Code Online (Sandbox Code Playgroud)

android kotlin android-livedata kotlin-coroutines kotlin-flow

6
推荐指数
1
解决办法
1万
查看次数

Background shadow in android

Is there any way in Android that applies drop shadow as shown in Figma or XD Tool ?

在此输入图像描述

I have got a figma design and I am stuck in creating this drop effect I have tried it using selector and layer-list

Is there any way to apply x , y and blur effect along with shadow color with opacity.

The above attributes only work for EditText but not working for TextInputLayout

My View Attribues in Figma are below .

在此输入图像描述

android user-experience dropshadow kotlin

3
推荐指数
1
解决办法
2091
查看次数

Android Kotlin Flow 中发出异常

我在流程内发出异常并得到以下异常。

IllegalStateException: Flow exception transparency is violated:
    Previous 'emit' call has thrown exception java.lang.NullPointerException, but then emission attempt of value 'planetbeyond.domain.api.Resource$Error@85b4d28' has been detected.
    Emissions from 'catch' blocks are prohibited in order to avoid unspecified behaviour, 'Flow.catch' operator can be used instead.
    For a more detailed explanation, please refer to Flow documentation.
       at kotlinx.coroutines.flow.internal.SafeCollector.exceptionTransparencyViolated(SafeCollector.kt:140)
       at kotlinx.coroutines.flow.internal.SafeCollector.checkContext(SafeCollector.kt:104)
       at kotlinx.coroutines.flow.internal.SafeCollector.emit(SafeCollector.kt:83)
       at kotlinx.coroutines.flow.internal.SafeCollector.emit(SafeCollector.kt:66)
       at planetbeyond.domain.use_cases.OptionSelectedCountUsecase$invoke$1.invokeSuspend(OptionSelectedCountUsecase.kt:20)
Run Code Online (Sandbox Code Playgroud)

OptionSelectedCountUsecase.kt

class OptionSelectedCountUsecase @Inject constructor(
private val repository: Repository
) {
    operator fun invoke(questionId: Int): Flow<Resource<List<OptionSelectedCountModel>>> = flow …
Run Code Online (Sandbox Code Playgroud)

rest android kotlin kotlin-flow

2
推荐指数
1
解决办法
6099
查看次数