Skip to content

Instantly share code, notes, and snippets.

View TorkelV's full-sized avatar

Torkel Velure TorkelV

View GitHub Profile
/*
For some reason the webview does not respect its textZoom property.
It seems like it happens whenever the webviews parent is hidden and made visible again.
Setting the textZoom to a different value and then back fixes the problem..
*/
private fun WebView.fixTextZoomSetting(){
this.apply {
val fontScale = context.resources.configuration.fontScale
if(fontScale != 1f){
val newScale = (fontScale * 100f).toInt()
@TorkelV
TorkelV / WebViewDarkMode.kt
Created November 24, 2021 11:59
enable dark mode in android WebView
webView.run {
if (WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK_STRATEGY)
&& (resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES
&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
WebSettingsCompat.setForceDarkStrategy(settings, WebSettingsCompat.DARK_STRATEGY_WEB_THEME_DARKENING_ONLY)
settings.forceDark = WebSettings.FORCE_DARK_ON
}
}
@TorkelV
TorkelV / hasWebView.kt
Last active November 22, 2021 21:04
Check if WebView is available
fun hasWebView(): Boolean {
return kotlin.runCatching { CookieManager.getInstance() }.isSuccess
}
val lm = binding.recyclerView.layoutManager as LinearLayoutManager
binding.recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
val lastVisible = lm.findLastVisibleItemPosition() + 1
if (viewModel.lastVisibleItem.value < lastVisible) {
viewModel.lastVisibleItem.value = lastVisible
}
}
})
val lastVisibleItem = MutableStateFlow<Int>(0)
private val leaders: Flow<List<User>> = db.getLeaders(lastVisibleItem)
override fun getLeaders(lastVisibleItem: Flow<Int>): Flow<List<User>> =
collection("users")
.whereGreaterThan("points", 0)
.orderBy("points", Query.Direction.DESCENDING)
.paginate(lastVisibleItem)
.map { docs -> docs.map { it.toUserDto().toUser() } }
private fun Query.paginate(lastVisibleItem: Flow<Int>): Flow<List<DocumentSnapshot>> = flow {
val documents = mutableListOf<DocumentSnapshot>()
documents.addAll(
suspendCoroutine { c ->
this@paginate.limit(25).get().addOnSuccessListener { c.resume(it.documents) }
}
)
emit(documents)
lastVisibleItem.transform { lastVisible ->
if (lastVisible == documents.size && documents.size > 0) {
override fun getLeaders(lastVisibleItem: Flow<Int>): Flow<List<User>> =
flow {
val users = mutableListOf<DocumentSnapshot>()
users.addAll(
suspendCoroutine<List<DocumentSnapshot>> { c ->
collection("users")
.whereGreaterThan("points", 0)
.orderBy("points", Query.Direction.DESCENDING)
.limit(25)
.get().addOnSuccessListener {
fun Query.asFlow(): Flow<QuerySnapshot> {
return callbackFlow {
val callback = addSnapshotListener { querySnapshot, ex ->
if (ex != null) {
close(ex)
} else {
offer(querySnapshot!!)
}
}
awaitClose {
@TorkelV
TorkelV / SnapshotFlow.kt
Last active February 24, 2021 12:05
snapshot_flow
fun collection(path: String) = Firebase.firestore.collection(path)
override fun getLeaders(): Flow<List<User>> {
return collection("users")
.whereGreaterThan("points", 0)
.orderBy("points", Query.Direction.DESCENDING)
.limit(50)
.asFlow().map { snapshots ->
snapshots.map { it.toUserDto().toUser() }
}