android-livedata

How to observe Changes in Shared Preference using LiveData?

假如想象 提交于 2020-01-14 01:48:09
问题 Recently, I had a requirement in one of my projects to observe the changes in SharedPreferences throughout my app (I had activity, Foreground Service and BroadcastReciever which would react differently to changes in SharedPreferences). I also wanted to listen for these changes only when my corresponding AppComponents are active. As such, LiveData would be what will suit my requirement but what would be the best approach to do so? (Asking the question for documentation purpose, would love to

Refresh data from a network using LiveData

纵饮孤独 提交于 2020-01-13 10:13:53
问题 I am working on an app that queries the github api to get a list of user and i'm following the recommended android architecture component guide. Once the data is fetched from the network, I store it locally using Room DB and then display it on the UI using ViewModel that observes on the LiveData object (this works fine). However, I want to be able to have a button which when clicked would trigger a refresh action and perform a network request to get new data from the API if and only if there

Refresh data from a network using LiveData

試著忘記壹切 提交于 2020-01-13 10:12:07
问题 I am working on an app that queries the github api to get a list of user and i'm following the recommended android architecture component guide. Once the data is fetched from the network, I store it locally using Room DB and then display it on the UI using ViewModel that observes on the LiveData object (this works fine). However, I want to be able to have a button which when clicked would trigger a refresh action and perform a network request to get new data from the API if and only if there

MutableLiveData is null in JUnitTest

家住魔仙堡 提交于 2020-01-13 08:47:08
问题 I want to write a unit test. Therefore I need MutableLiveData. I started with a very basic test for setup but I cannot instantiate a MutableLiveData object. I is always null when I run the test. Do I have to mock anything? Any suggestions? @RunWith(MockitoJUnitRunner.class) public class DefaultLiveDataTest { private static final int EXPECTED = 5; private final MutableLiveData<Integer> underTest = new MutableLiveData<>(); @Test public void exampleTest() { underTest.setValue(EXPECTED); /

Convert LiveData to MutableLiveData

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-12 15:47:10
问题 Apparently, Room is not able to handle MutableLiveData and we have to stick to LiveData as it returns the following error: error: Not sure how to convert a Cursor to this method's return type I created a "custom" MutableLiveData in my DB helper this way: class ProfileRepository @Inject internal constructor(private val profileDao: ProfileDao): ProfileRepo{ override fun insertProfile(profile: Profile){ profileDao.insertProfile(profile) } val mutableLiveData by lazy { MutableProfileLiveData() }

Nullability and LiveData with Kotlin

六眼飞鱼酱① 提交于 2020-01-12 14:11:24
问题 I want to use LiveData with Kotlin and have values that should not be null. How do you deal with this? Perhaps a wrapper around LiveData? Searching for good patterns here .. As an example: class NetworkDefinitionProvider : MutableLiveData<NetworkDefinition>() { val allDefinitions = mutableListOf(RinkebyNetworkDefinition(), MainnetNetworkDefinition(), RopstenNetworkDefinition()) init { value = allDefinitions.first() } fun setCurrent(value: NetworkDefinition) { setValue(value) } } I know value

Nullability and LiveData with Kotlin

无人久伴 提交于 2020-01-12 14:08:55
问题 I want to use LiveData with Kotlin and have values that should not be null. How do you deal with this? Perhaps a wrapper around LiveData? Searching for good patterns here .. As an example: class NetworkDefinitionProvider : MutableLiveData<NetworkDefinition>() { val allDefinitions = mutableListOf(RinkebyNetworkDefinition(), MainnetNetworkDefinition(), RopstenNetworkDefinition()) init { value = allDefinitions.first() } fun setCurrent(value: NetworkDefinition) { setValue(value) } } I know value

LiveData.getValue() returns null with Room

為{幸葍}努か 提交于 2020-01-09 07:14:19
问题 Java POJO Object public class Section { @ColumnInfo(name="section_id") public int mSectionId; @ColumnInfo(name="section_name") public String mSectionName; public int getSectionId() { return mSectionId; } public void setSectionId(int mSectionId) { this.mSectionId = mSectionId; } public String getSectionName() { return mSectionName; } public void setSectionName(String mSectionName) { this.mSectionName = mSectionName; } } My Query method @Query("SELECT * FROM section") LiveData<List<Section>>

LiveData.getValue() returns null with Room

橙三吉。 提交于 2020-01-09 07:13:10
问题 Java POJO Object public class Section { @ColumnInfo(name="section_id") public int mSectionId; @ColumnInfo(name="section_name") public String mSectionName; public int getSectionId() { return mSectionId; } public void setSectionId(int mSectionId) { this.mSectionId = mSectionId; } public String getSectionName() { return mSectionName; } public void setSectionName(String mSectionName) { this.mSectionName = mSectionName; } } My Query method @Query("SELECT * FROM section") LiveData<List<Section>>

Using MediatorLiveData to merge to LiveData (Firestore) QuerySnapshot streams is producing weird results

半世苍凉 提交于 2020-01-06 07:25:47
问题 I'm assigning two Firestore Queries to LiveData objects. Each one of these queries returns to different object classes: User and Book. I need to merge both of them together and apply a transformation so I can create a LinkedHashmap that combines both sets of data (queries). In order to do this I'm using MediatorLive data and to be able to merge the two different classes I'm using QuerySnapshot as the return type. The problem I'm running into is when I output the results of the