View WebViewFontScale.kt
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
/* | |
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() |
View WebViewDarkMode.kt
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
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 | |
} | |
} |
View hasWebView.kt
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
fun hasWebView(): Boolean { | |
return kotlin.runCatching { CookieManager.getInstance() }.isSuccess | |
} |
View OnScrollListener.kt
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
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 | |
} | |
} | |
}) |
View ViewModelFlow.kt
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
val lastVisibleItem = MutableStateFlow<Int>(0) | |
private val leaders: Flow<List<User>> = db.getLeaders(lastVisibleItem) |
View LeadersFlow.kt
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
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() } } |
View GenericPaginatedFlow.kt
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
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) { |
View PaginatedFlow.kt
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
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 { |
View QueryAsFlow.kt
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
fun Query.asFlow(): Flow<QuerySnapshot> { | |
return callbackFlow { | |
val callback = addSnapshotListener { querySnapshot, ex -> | |
if (ex != null) { | |
close(ex) | |
} else { | |
offer(querySnapshot!!) | |
} | |
} | |
awaitClose { |
View SnapshotFlow.kt
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
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() } | |
} |
NewerOlder