This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Dao | |
interface ProductsDao { | |
// Because this is marked suspend, Room will use it's own dispatcher | |
// to run this query in a main-safe way. | |
@Query("select * from ProductListing ORDER BY dateStocked ASC") | |
suspend fun loadProductsByDateStockedAscending(): List<ProductListing> | |
// Because this is marked suspend, Room will use it's own dispatcher | |
// to run this query in a main-safe way. | |
@Query("select * from ProductListing ORDER BY dateStocked DESC") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Solution 0: Disable the sort buttons when any sort is running | |
class ProductsViewModel(val productsRepository: ProductsRepository): ViewModel() { | |
private val _sortedProducts = MutableLiveData<List<ProductListing>>() | |
val sortedProducts: LiveData<List<ProductListing>> = _sortedProducts | |
private val _sortButtonsEnabled = MutableLiveData<Boolean>() | |
val sortButtonsEnabled: LiveData<Boolean> = _sortButtonsEnabled | |
init { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Solution #1: Cancel previous work | |
// This is a great solution for tasks like sorting and filtering that | |
// can be cancelled if a new request comes in. | |
class ProductsRepository(val productsDao: ProductsDao, val productsApi: ProductsService) { | |
var controlledRunner = ControlledRunner<List<ProductListing>>() | |
suspend fun loadSortedProducts(ascending: Boolean): List<ProductListing> { | |
// cancel the previous sorts before starting a new one |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// see the complete implementation at | |
// https://gist.github.com/objcode/7ab4e7b1df8acd88696cb0ccecad16f7 | |
suspend fun cancelPreviousThenRun(block: suspend () -> T): T { | |
// If there is an activeTask, cancel it because it's result is no longer needed | |
activeTask?.cancelAndJoin() | |
// ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Solution #2: Add a Mutex | |
// Note: This is not optimal for the specific use case of sorting | |
// or filtering but is a good pattern for network saves. | |
class ProductsRepository(val productsDao: ProductsDao, val productsApi: ProductsService) { | |
val singleRunner = SingleRunner() | |
suspend fun loadSortedProducts(ascending: Boolean): List<ProductListing> { | |
// wait for the previous sort to complete before starting a new one |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ProductsRepository(val productsDao: ProductsDao, val productsApi: ProductsService) { | |
var controlledRunner = ControlledRunner<List<ProductListing>>() | |
suspend fun fetchProductsFromBackend(): List<ProductListing> { | |
// if there's already a request running, return the result from the | |
// existing request. If not, start a new request by running the block. | |
return controlledRunner.joinPreviousOrRun { | |
val result = productsApi.getProducts() | |
productsDao.insertAll(result) | |
result |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// see the complete implementation at | |
// https://gist.github.com/objcode/7ab4e7b1df8acd88696cb0ccecad16f7#file-concurrencyhelpers-kt-L124 | |
suspend fun joinPreviousOrRun(block: suspend () -> T): T { | |
// if there is an activeTask, return it's result and don't run the block | |
activeTask?.let { | |
return it.await() | |
} | |
// ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from collections import namedtuple | |
CoC = 0.018 # canon APS-C | |
MIN_DISTANCE = 100 # must focus 300mm away | |
MAX_DISTANCE = 1 * 1000 * 1000 # must focus one KM away | |
FOCALS = [1.3 + (x/10.0) for x in xrange(1, 40 * 10)] | |
FOCUS_DISTANCE = [100 * x for x in xrange(1, 10)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context=".AwesomeActivity"> | |
<Button |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Using view binding in an Activity | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
val binding = ActivityAwesomeBinding.inflate(layoutInflater) | |
binding.title.text = "Hello" | |
binding.subtext.text = "Concise, safe code" | |
binding.button.setOnClickListener { /* ... */ } | |
setContentView(binding.root) |