frankelot
05/12/2021, 1:55 PMcoroutineContext.cancel()
anywherecoroutineContext.cancel()
anywhere
My current architecture uses reusable custom viewsAdam Powell
05/12/2021, 2:07 PMJob
in the coroutineContext so trying to cancel the context won't do very much for youlouiscad
05/12/2021, 2:13 PMCoroutineScope
on your own because it can lead to easily launch in the wrong scope later on. Best to have it be a property for the cases where you need it.Adam Powell
05/12/2021, 2:16 PMViewModel.viewModelScope
extension set an awful precedent for this; its nature as an extension to a library written in the java language forced it to be public when it really shouldn't have beenlouiscad
05/12/2021, 2:19 PMprotected
, right?Adam Powell
05/12/2021, 2:22 PMonCleared
is a pretty small inconvenience in those cases where you have an AAC ViewModel that really needs to own a CoroutineScope; many don't. The existence of viewModelScope
terminates further thought about whether an operation should be scoped to the UI or to a repository.louiscad
05/12/2021, 2:23 PMViewModel
subclasses in your app, you don't have this problem though :think-smart:Adam Powell
05/12/2021, 2:25 PMstreetsofboston
05/12/2021, 2:25 PMAdam Powell
05/12/2021, 2:27 PMstreetsofboston
05/12/2021, 2:28 PMfrankelot
05/12/2021, 2:52 PMCoroutineScope
but instead use the parent Activity scopejob = (context as LifecycleOwner).lifecycleScope.launch {}
Adam Powell
05/12/2021, 2:54 PMlouiscad
05/12/2021, 2:56 PMfrankelot
05/12/2021, 2:56 PMAdam Powell
05/12/2021, 2:57 PMfrankelot
05/12/2021, 2:57 PMstreetsofboston
05/12/2021, 2:58 PMlouiscad
05/12/2021, 2:59 PMfrankelot
05/12/2021, 3:01 PMModify your Fragment/Activity that hosts the recycler-viewRight now this is a custom view that I can put anywhere, I'd like to keep it that way 🙂
streetsofboston
05/12/2021, 3:08 PMfrankelot
05/12/2021, 3:10 PMoverride fun onBindViewHolder(holder: ProfileVH, pos: Int) {
when(holder) {
MyCustomView -> //no op, no binding required
... ->
... ->
}
}
Adam Powell
05/12/2021, 3:20 PMfrankelot
05/12/2021, 3:21 PMbinding is what assigns the position😱 what do you mean by this?
streetsofboston
05/12/2021, 3:22 PMfrankelot
05/12/2021, 3:24 PMonViewAttached
subscribes to a stateflow that updates the dataAdam Powell
05/12/2021, 3:25 PMlouiscad
05/12/2021, 3:28 PMfrankelot
05/12/2021, 3:29 PMNot if it's subscribed to the wrong data because assigning the data source was skipped during bindingthis is my view
class CustomView @JvmOverloads constructor(...) : FrameLayout {
@Inject
lateinit var service : MyService;
overrideOnAttach() {
job = (context as LifecycleOwner).lifecycleScope.launch {
service.stateFlow.collect { ... }
}
}
}
louiscad
05/12/2021, 3:30 PMfrankelot
05/12/2021, 3:31 PMonBindViewHolder
louiscad
05/12/2021, 3:33 PMfrankelot
05/12/2021, 3:35 PM// on Attach
job = (context as LifecycleOwner).lifecycleScope.launch {}
// on Detach
job.cancel()
Adam Powell
05/12/2021, 3:37 PMfrankelot
05/12/2021, 3:37 PMcancel
on itAdam Powell
05/12/2021, 3:38 PMlouiscad
05/12/2021, 3:38 PMfrankelot
05/12/2021, 3:39 PMstreetsofboston
05/12/2021, 3:44 PM// top level val
private val Context?.lifecycleOwner: LifecycleOwner? get() = when (this) {
is LifecycleOwner -> this
is ContextWrapper -> this.baseContext.lifecycleOwner
else -> null
}
louiscad
05/12/2021, 3:45 PMlaunch { … }
, i.e. collecting the StateFlow
.Adam Powell
05/12/2021, 3:48 PMLifecycleOwner
of any containing fragments, for example. ViewTreeLifecycleOwner.get()
is probably more reliable but even that has its edge casesstreetsofboston
05/12/2021, 3:49 PMcontext as LifecycleOwner
🙂
I never heard of ViewTreeLifecycleOwner... interestingAdam Powell
05/12/2021, 3:50 PMfrankelot
05/12/2021, 4:04 PMlouiscad
05/12/2021, 4:07 PMfrankelot
05/12/2021, 4:12 PMyou just also need to attach the right "data provider"Ideally I'd like this not be the responsibility of the adapter. The adapter just knows about what views need to be laid out and in what order.
louiscad
05/12/2021, 4:14 PMmyanmarking
05/14/2021, 8:24 AM