Skip to content

Instantly share code, notes, and snippets.

@ceruleanotter
ceruleanotter / MyViewModel.kt
Created June 20, 2019 19:38
ViewModel Integrations: Coroutines 2
class MyViewModel() : ViewModel() {
fun initialize() {
viewModelScope.launch {
processBitmap()
}
}
suspend fun processBitmap() = withContext(Dispatchers.Default) {
// Do your long running work here
@ceruleanotter
ceruleanotter / CoroutineExample.kt
Created June 20, 2019 19:29
ViewModel Integrations : Coroutines Example 1
// Don't use GlobalScope - for example purposes only
GlobalScope.launch {
longRunningFunction()
anotherLongRunningFunction()
}
@ceruleanotter
ceruleanotter / main_activity.xml
Created June 20, 2019 19:27
ViewModel Integrations: Data Binding Example 4
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="viewmodel"
type="com.android.MyViewModel"/>
</data>
<TextView
android:id="@+id/name"
android:text="@{viewmodel.name}"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
@ceruleanotter
ceruleanotter / MainActivity.kt
Last active June 20, 2019 19:25
ViewModel Integrations: Data Binding Example 3
class MainActivity : AppCompatActivity() {
// This ktx requires at least androidx.activity:activity-ktx:1.0.0
private val myViewModel: MyViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//Inflate view and create binding
val binding: MainActivityBinding =
@ceruleanotter
ceruleanotter / main_activity.xml
Created June 20, 2019 19:22
ViewModel Integrations: Data Binding Example 2
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="viewmodel"
type="com.android.MyViewModel"/>
</data>
<... Rest of your layout ...>
</layout>
@ceruleanotter
ceruleanotter / MyFragment.kt
Last active June 20, 2019 22:03
ViewModel Integrations: Data Binding Example 1
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
myViewModel.name.observe(this, { newName ->
// Update the UI. In this case, a TextView.
nameTextView.text = newName
})
}
@ceruleanotter
ceruleanotter / MyFragment.java
Last active June 20, 2019 19:16
ViewModel Integrations: NavGraph with a ViewModel Example 4
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Other fragment setup code
NavController navController = NavHostFragment.findNavController(this);
ViewModelProvider viewModelProvider = new ViewModelProvider(this,
navController.getViewModelStore(R.id.checkout_graph));
@ceruleanotter
ceruleanotter / MyFragment.kt
Created June 20, 2019 19:14
ViewModel Integrations: NavGraph with a ViewModel Example 3
val viewModel: CheckoutViewModel by navGraphViewModels(R.id.checkout_graph)
@ceruleanotter
ceruleanotter / navigation_graph.xml
Created June 20, 2019 19:04
ViewModel Integrations: NavGraph with a ViewModel Example 2
<navigation app:startDestination="@id/homeFragment" ...>
<fragment android:id="@+id/homeFragment" .../>
<fragment android:id="@+id/productListFragment" .../>
<fragment android:id="@+id/productFragment" .../>
<fragment android:id="@+id/bargainFragment" .../>
<navigation
android:id="@+id/checkout_graph"
app:startDestination="@id/cartFragment">
@ceruleanotter
ceruleanotter / MyFragment.kt
Created June 20, 2019 19:00
ViewModel Integrations: NavGraph with a ViewModel Example 1
// Any fragment's onCreate or onActivityCreated
// This ktx requires at least androidx.fragment:fragment-ktx:1.1.0
val sharedViewModel: ActivityViewModel by activityViewModels()