如何收集activity中的两个状态流?因为我只消耗了第一个流量。
例如,viewmodel内部是这样的:
class ExampleViewModel: ViewModel(){
private val state = MutableStateFlow<HomeMainFragmentState>(HomeMainFragmentState.Init)
private val products = MutableStateFlow<List<ProductEntity>>(mutableListOf())
//to be consumed
fun getState() : StateFlow<HomeMainFragmentState> = state
fun getProducts() : StateFlow<List<ProductEntity>> = products
}
Run Code Online (Sandbox Code Playgroud)
然后在我看来是这样的:
private fun observe(){
viewLifecycleOwner.lifecycleScope.launch {
viewLifecycleOwner.lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED){
viewModel.getState().collect { state -> handleState(state) }
viewModel.getProducts().collect{ products -> handleProducts(products) }
}
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,只有第一个流被消耗,因为这种情况是“状态”,产品从未被活动/片段消耗/执行。
如何解决这个问题?我还阅读了有关组合流程的信息,这是否意味着第二个流程取决于第一个流程的运行?
我有一个可重复使用的组件,称为 DatePicker,如下所示
export interface IPDatePicker extends IProps {
control: any
label: string
placeholder: string
value?: string
errorText?: string
isRequired?: boolean
name: string
defaultValue?: any
onChangeText: (value: string) => void
minimumDate?: any
maximumDate?: any
timeOnly?: boolean
hideLabel?: boolean
labelMaxLine?: number
// setDateValue: any
}
const DatePicker: React.FC<IPDatePicker> = props => {
const todaysDate = new Date()
const [date, setDate] = useState<Date>(todaysDate)
const [isDatePickerOpen, setIsDatePickerOpen] = useState(false)
return (
<Controller
name={props?.name}
defaultValue={props?.defaultValue}
control={props?.control}
render={({field: {onChange, value}}: any) => {.....}
/>
Run Code Online (Sandbox Code Playgroud)
关键是,组件需要“控制”作为强制参数。
“控件”来自使用 …