architecture-samples | showcase different architectural tools and patterns | Architecture library
kandi X-RAY | architecture-samples Summary
kandi X-RAY | architecture-samples Summary
A collection of samples to discuss and showcase different architectural tools and patterns for Android apps.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of architecture-samples
architecture-samples Key Features
architecture-samples Examples and Code Snippets
Community Discussions
Trending Discussions on architecture-samples
QUESTION
The Code A is from the official sample project.
Result
is a sealed class, I'm very strange why Loading
is defined as object class.
I think the Code B is more reasonable, is it right?
Code A
...ANSWER
Answered 2022-Mar-24 at 11:20that won't work, because:
QUESTION
Freshly installing the app, the view model doesn't bind the data. Closing the app and opening it again shows the data on the screen.
Is there any problem with the pre-population of data or is the use of coroutine is not correct?
If I use Flow in place of LiveData, it collects the data on the go and works completely fine, but its a bit slow as it is emitting data in the stream.
Also, for testing, The data didn't load either LiveData/Flow.
Tried adding the EspressoIdlingResource
and IdlingResourcesForDataBinding
as given here
ANSWER
Answered 2022-Feb-26 at 16:08Your DAO returns suspend fun getAllUser(): List
, meaning it's a one time thing. So when the app starts the first time, the DB initialization is not complete, and you get an empty list because the DB is empty. Running the app the second time, the initialization is complete so you get the data.
How to fix it:
- Switch
getAllUser()
to return aFlow
:
QUESTION
I'm using the MVI pattern in a project so I wrote both State and Event classes. For the Event part inside the viewModel I'm using a private property _event of type MutableLiveData, and exposing it to the Activity as a LiveData, this way:
...ANSWER
Answered 2021-Jun-24 at 13:42Not 100% sure, but I would bet you auto-converted the SingleLiveEvent
Java code to Kotlin, and it declared it as a subclass of MutableLiveData
instead of MutableLiveData
.
QUESTION
I followed a lot of tutorials/ articles and the googles architecture sample
No matter what I try I keep getting an error saying that I need to attach Fragment to @AndroidEntryPoint annotated Activity. I've setup everything correctly but still can't get this to work properly.
...ANSWER
Answered 2021-Mar-24 at 08:05In my case I had to use
QUESTION
Im trying to make mvvm pattern with repository and servicelocator to use mock's or remote calls, it depends on flavour. What happening now,is that my liveData is not updating after i receive response from server. So for now i always have a empty list.
I use this google sample to trying make it. sample
My code below, using remote serviceLocator Appreciate your help.
...ANSWER
Answered 2020-Dec-20 at 15:46My guess is that mixing Coroutines suspending function with Retrofit and LiveData leads to some side effect here.
I do not have a single solution, but some points they can help you.
In general I would avoid mixing LiveData with suspending functions. LiveData is concept of caching data for the UI/ViewModel layer. Lower layers do not need to know anything like Android concrete stuff like LiveData. More information here
In your repository or dataSource can either use a suspending function that returns a single value or Coroutines Flow that can emit more than one value. In your ViewModel you can then map those results to your LiveData.
DataSource
In your DataSource you can use suspendCoroutine or suspendCancellableCoroutine
to connect Retrofit (or any other callback interface) with Coroutines:
QUESTION
The following code is from the project.
1: In my mind,a suspend fun should be launched in another suspend fun or viewModelScope.launch{ }
, withContext{ }
... , filterItems()
is only a normal function, I don't know why filterItems()
need to be wrapped with viewModelScope.launch{ }
in the function filterTasks()
, could you tell me ?
2: In the function filterTasks()
, viewModelScope.launch{ }
will launch in coroutines, it's asynchronous, I think return result
maybe be launched before I get the result
from viewModelScope.launch{}
, so the result
maybe null, is the code correct?
Code
...ANSWER
Answered 2020-Sep-16 at 22:26yes, you are right. but if you looked up the implementation of the launch {} such in lifecycleScope.launch {} or viewModelScope.launch {} you would find out the "block" which is "the coroutine code which will be invoked in the context of the provided scope" is cast to be suspend, so any block of code between launch {} is suspend code block. so in your example filterItems is cast to suspend under the hood and it's wrapped with viewModelScope.launch{ } to do its heavy task not in main thread.
QUESTION
The following code is from the project.
The function of tasksRepository.refreshTasks()
is to insert data from remote server to local DB, it's a time consuming operation.
In class TasksViewModel
, asksRepository.refreshTasks()
is wrapped with viewModelScope.launch{}
, it means launch and careless.
1: How can I guarantee tasksRepository.observeTasks().distinctUntilChanged().switchMap { filterTasks(it) }
to return the latest result?
2: I don't know how distinctUntilChanged()
work, will it keep listening to return the latest result in whole Lifecycle ?
3: What's happened if I use tasksRepository.observeTasks().switchMap { filterTasks(it) }
instead of tasksRepository.observeTasks().distinctUntilChanged().switchMap { filterTasks(it) }
Code
...ANSWER
Answered 2020-Sep-11 at 02:57switchMap
- The returned LiveData delegates to the most recent LiveData created by calling switchMapFunction with the most recent value set to source, without changing the reference. DocYes, it'll keep listening to return the latest result in whole Lifecycle.
distinctUntilChanged
creates a new LiveData object that does not emit a value until the source LiveData value has been changed. The value is considered changed ifequals()
yieldsfalse
.Yes you can use that too but it'll keep emitting the values even the values are the same as the last emitted value. e.g. first emitted value is ["aman","bansal"] and the second is the same ["aman","bansal"] which you don't want to emit since the values are same. So you use
distinctUntilChanged
to make sure it won't emit the same value until changed.
I hope this helped.
QUESTION
I have a callback method in my fragment which gets called from it's ViewModel. It initializes the variable in the OnCreateView()
method of the fragment, but when the ViewModel calls it to use it, its null.
I am thinking that it has something to do with maybe the VM getting recreated somehow? I just can't seem to figure it out.
I am following this answer's of how the VM drives the UI. They provide Google's sample of a callback interface being created (TasksNavigator.java), Overriding the method in the View (TasksActivity.java), and then calling that method from the VM (TasksViewModel.java) but it doesn't seem to work for me.
Fragment
...ANSWER
Answered 2020-Sep-08 at 07:04Initiate ViewModel in below method of fragment
QUESTION
ANSWER
Answered 2020-Aug-25 at 07:33Yes, it is absolutely identical because LiveData.map
is an extension function that provided from Transformations.kt
file that is a part of dependency:
QUESTION
I've got an android app setup for dependency injection using Hilt, and would like to unit test my fragments.
I'm currently creating my view model using:
...ANSWER
Answered 2020-Aug-12 at 20:39I will paste here the "danysantiago" response in a issue (https://github.com/google/dagger/issues/1972) related to your question:
Hilt ViewModel extension works by declaring modules that bind assisted factories to a map and not by binding concrete ViewModels. Therefore, what you want to do is bind the assisted factory of the concrete ViewModel using the key of the abstract ViewModel so that when HiltViewModelFactory looks up the factory based on class key it uses the assisted factory for the concrete ViewModel. This is suuuper obscure and hence why I mean not 'easily' available.
However, if you can expand on the test case your are trying to write that could help us provide some guidance, I'm not sure if you are trying to mock/fake the ViewModel itself for tests, but Hilt testing APIs should allow you to replace dependencies in the ViewModel so you can write a test with the Fragment and the ViewModel.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install architecture-samples
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page