This file contains 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
import kotlinx.coroutines.* | |
import kotlin.coroutines.* | |
import kotlin.system.* | |
/** Wraps original dispatcher, so we can wrap the continuations. */ | |
class Monitor( | |
private val interceptor: ContinuationInterceptor | |
) : AbstractCoroutineContextElement(ContinuationInterceptor), ContinuationInterceptor { | |
override fun <T> interceptContinuation(continuation: Continuation<T>): Continuation<T> = | |
interceptor.interceptContinuation(MeasuredContinuation(continuation)) |
This file contains 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
/** | |
* Gates upstream flow emissions by [passThrough]. | |
* | |
* When upstream flow emits a value, either emits the value downstream if the last value emitted by [passThrough] | |
* was `true`, or accumulates it for later emission if `false`. Accumulated values are emitted once [passThrough] | |
* emits `true`. | |
*/ | |
@OptIn(FlowPreview::class) // produceIn is stable since kotlinx-coroutines 1.7.0-Beta | |
internal fun <T> Flow<T>.accumulateBy(passThrough: Flow<Boolean>) = flow<List<T>> { |
This file contains 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
@Singleton | |
class Biller @Inject constructor( | |
@ApplicationContext private val context: Context, | |
) : PurchasesUpdatedListener, BillingClientStateListener { | |
private var billingClient: BillingClient = | |
BillingClient.newBuilder(context) | |
.setListener(this) | |
.enablePendingPurchases() | |
.build() |
This file contains 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
@AndroidEntryPoint | |
class NearbyUsersActivity : AppCompatActivity() { | |
private val viewModel: NearbyUsersViewModel by viewModels() | |
override fun onCreate(savedInstanceState: Bundle?) { | |
viewModel | |
.locations | |
.onEach { /* new locations received */ } | |
.observeIn(this) |
This file contains 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
@PublishedApi | |
internal class ObserverImpl<T> ( | |
lifecycleOwner: LifecycleOwner, | |
private val flow: Flow<T>, | |
private val collector: suspend (T) -> Unit | |
) : DefaultLifecycleObserver { | |
private var job: Job? = null | |
override fun onStart(owner: LifecycleOwner) { |
This file contains 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
@AndroidEntryPoint | |
class NearbyUsersActivity : AppCompatActivity() { | |
private val viewModel: NearbyUsersViewModel by viewModels() | |
override fun onCreate(savedInstanceState: Bundle?) { | |
lifecycleScope.launchWhenStarted { | |
viewModel.locations.collect { | |
// Update views with the data. | |
} |
This file contains 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
@Singleton | |
class NearbyUsersDataSource @Inject constructor() { | |
// Ideally, those should be constructor-injected. | |
val geoFire = GeoFire(FirebaseDatabase.getInstance().getReference("geofire")) | |
val geoLocation = GeoLocation(0.0, 0.0) | |
val radius = 100.0 | |
val geoQuery = geoFire.queryAtLocation(geoLocation, radius) | |
private fun GeoQuery.asFlow() = callbackFlow { |
This file contains 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
@AndroidEntryPoint | |
class NearbyUsersActivity : AppCompatActivity() { | |
private val viewModel: NearbyUsersViewModel by viewModels() | |
override fun onCreate(savedInstanceState: Bundle?) { | |
viewModel.locations.observe(this) { state: State -> | |
// Update views with the data. | |
} | |
} |
This file contains 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 NearbyUsersViewModel @ViewModelInject constructor( | |
nearbyUsersRepository: NearbyUsersRepository | |
) : ViewModel() { | |
val locations get() = nearbyUsersRepository.locations | |
} |
NewerOlder