Skip to content

Instantly share code, notes, and snippets.

View JoseAlcerreca's full-sized avatar

Jose Alcérreca JoseAlcerreca

View GitHub Profile
class SimpleIdlingResource(private val resourceName: String) : IdlingResource {
private val isIdle = AtomicBoolean(true)
// written from main thread, read from any thread.
@Volatile private var resourceCallback: IdlingResource.ResourceCallback? = null
override fun getName(): String = resourceName
override fun isIdleNow(): Boolean = isIdle.get()
// Expose resource IDs instead
public class MyViewModel extends ViewModel {
public final MutableLiveData<Int> statusLabel = new MutableLiveData<>();
public SampleViewModel() {
...
statusLabel.setValue(R.string.labelString);
}
}
// Don't do this
public class MyViewModel extends AndroidViewModel {
public final MutableLiveData<String> statusLabel = new MutableLiveData<>();
public SampleViewModel(Application context) {
super(context);
statusLabel.setValue(context.getString(R.string.labelString));
}
}
val binding = DataBindingUtil.setContentView<TheGeneratedBinding>(
this,
R.layout.activity_data_binding
)
binding.lifecycleOwner = this // Use viewLifecycleOwner for fragments
binding.name = myLiveData // or myViewModel
val binding = DataBindingUtil.setContentView<TheGeneratedBinding>(
this,
R.layout.activity_data_binding
)
binding.name = myLiveData // or myViewModel
class MyViewModel : ViewModel() {
private val _name = MutableLiveData<String>().apply { value = "Ada" }
val name: LiveData<String> = _name // Expose the immutable version of the LiveData
}
class MyViewModel : ViewModel() {
val name = ObservableField<String>("Ada")
}
<data>
<import type="android.arch.lifecycle.LiveData" />
<variable
name="name"
type="LiveData&lt;String>" />
</data>
<TextView
android:text="@{name}"
android:layout_width="wrap_content"
<data>
<import type="android.databinding.ObservableField"/>
<variable
name="name"
type="ObservableField&lt;String>" />
</data>
<TextView
android:text="@{name}"
android:layout_width="wrap_content"
fun getDataForUser(newUser: String?): LiveData<UserDataResult> {
if (newUser == null) {
return MutableLiveData<UserDataResult>().apply { value = null }
}
return userOnlineDataSource.getOnlineTime(newUser)
.combineAndCompute(userCheckinsDataSource.getCheckins(newUser)) { a, b ->
UserDataSuccess(a, b)
}
}