Skip to content

Instantly share code, notes, and snippets.

View EfeBudak's full-sized avatar
learn->work->learn->work

Efe Budak EfeBudak

learn->work->learn->work
View GitHub Profile
val bottomSheetState = rememberStandardBottomSheetState()
val scaffoldState = rememberBottomSheetScaffoldState(bottomSheetState)
BottomSheetScaffold(
modifier = Modifier.fillMaxSize(),
scaffoldState = scaffoldState,
sheetContainerColor = Color.Transparent,
sheetContent = {
// Put whatever the sheet you want in here
}
@EfeBudak
EfeBudak / ObservingTheCurrentOffset.kt
Created August 7, 2024 19:28
Collect current offset of the bottom sheet
fun SomeComposable(){
val bottomSheetState = rememberStandardBottomSheetState()
val scaffoldState = rememberBottomSheetScaffoldState(bottomSheetState)
var currentOffset by remember { mutableFloatStateOf(0f) }
runCatching {
LaunchedEffect(bottomSheetState.requireOffset()) {
snapshotFlow { bottomSheetState.requireOffset() }
.collect { currentOffset = it }
override fun onReceive(context: Context?, intent: Intent?) {
context?.let {
Log.d("InteractiveNotificationBroadcastReceiver", "onReceive")
val notification = NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("Interactive Notification Title")
.setContentText("MODIFIED text")
.setContentIntent(newPendingIntent(context))
.build()
NotificationManagerCompat.from(context)
val builder = NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("Interactive Notification Title")
.setContentText("Interactive Notification Text")
.setContentIntent(InteractiveNotificationBroadcastReceiver.newPendingIntent(this))
val soCloseInteractiveNotification = builder.build()
class InteractiveNotificationBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
Log.d("InteractiveNotificationBroadcastReceiver", "onReceive")
}
companion object {
fun newPendingIntent(context: Context): PendingIntent {
val intent = Intent(context, InteractiveNotificationBroadcastReceiver::class.java)
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
@EfeBudak
EfeBudak / Create a Simple Notification
Created August 15, 2019 07:43
Create a Simple Notification on Android
val builder = NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("Interactive Notification Title")
.setContentText("Interactive Notification Text")
val notSoInteractiveNotification = builder.build()
@EfeBudak
EfeBudak / Create a Notification Channel
Created August 15, 2019 07:40
Code piece that creates a notification channel on Android
private fun createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val importance = NotificationManager.IMPORTANCE_DEFAULT
val channel = NotificationChannel(CHANNEL_ID, CHANNEL_NAME, importance).apply {
description = CHANNEL_DESCRIPTION
}
val notificationManager: NotificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}
recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
if ((recyclerView?.layoutManager as LinearLayoutManager)
.findFirstCompletelyVisibleItemPosition() == 0) {
buttonReturnToTop.visibility = View.GONE
}
}
})
recyclerView.layoutManager = object :
LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false) {
override fun onLayoutCompleted(state: RecyclerView.State?) {
super.onLayoutCompleted(state)
if (showReturnToTopButton
&& (findLastVisibleItemPosition()
- findFirstVisibleItemPosition()
+ 1
< adapter.itemCount)) {
buttonReturnToTop.visibility = View.VISIBLE
override fun start() {
itemDBObservable = itemDao.getAllItems()
compositeDisposable.clear()
compositeDisposable += itemDBObservable
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.scan { previousList: List<Item>, newList: List<Item> ->
val newItems = newList - previousList
if (previousList.isNotEmpty() && newItems.isNotEmpty()) {
showReturnToTopButton()