Skip to content

Instantly share code, notes, and snippets.

View molidev8's full-sized avatar

Miguel Olivera molidev8

View GitHub Profile
@molidev8
molidev8 / periodicWorker.kt
Created August 4, 2022 08:58
A periodic WorkManager worker
class BackupWorkerManager(context: Context) {
private var manager: WorkManager = WorkManager.getInstance(context)
fun launchWorker(interval: Long) =
manager.enqueue(
PeriodicWorkRequestBuilder<BackupWorker>(interval, TimeUnit.DAYS)
.setConstraints(
Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
@molidev8
molidev8 / workflow.yaml
Created July 29, 2022 08:39
A YAML workflow to run unit test and sign an AAB for Android
name: Build, Test and Compile
on:
push:
branches: [ master ]
jobs:
Unit-Test:
runs-on: ubuntu-latest
# needs: Build
@molidev8
molidev8 / attachingCallback.kt
Last active June 19, 2022 14:13
Adding the callback to the RecyclerView
ItemTouchHelper(
OurSimpleCallbackImpl()
).attachToRecyclerView(binding.recyclerView)
@molidev8
molidev8 / animation.kt
Last active June 19, 2022 14:10
A swipe animation over an item in RecyclerView
private val trashIcon = ContextCompat.getDrawable(context, R.drawable.ic_baseline_delete_24)
private val circleColor = ContextCompat.getColor(context, R.color.deleteRed)
private val circlePaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { color = circleColor }
private val reverseSurfaceColor = ContextCompat.getColor(context, R.color.primaryTextColor)
private val CIRCLE_ACCELERATION = 6f
override fun onChildDraw(
c: Canvas,
recyclerView: RecyclerView,
@molidev8
molidev8 / notify.kt
Last active June 13, 2022 17:22
A function to show an Android notification
fun createNotification(
context: Context,
title: String,
content: String,
) {
val builder = NotificationCompat.Builder(context, CHANNEL_ID)
val notification = builder.setContentTitle(title).setContentText(content)
.setSmallIcon(R.drawable.ic_launcher_foreground).build()
NotificationManagerCompat.from(context).notify(id, notification)
}
@molidev8
molidev8 / channel.kt
Last active June 13, 2022 17:16
A function to create a notification channel
fun createChannel(
context: Context,
channelId = CHANNEL_ID,
@StringRes channelName: Int,
@StringRes channelDescription: Int,
importanceLevel: Int = NotificationManager.IMPORTANCE_HIGH
) {
val channel = NotificationChannel(
context.getString(channelId),
context.getString(channelName),
@molidev8
molidev8 / mainActivity.kt
Created June 11, 2022 17:07
A foreground service intialization
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
startService(Intent(this, TimerService::class.java))
bindService(
Intent(this, TimerService::class.java),
timerServiceConnection,
Context.BIND_AUTO_CREATE
)
setContent {
@molidev8
molidev8 / serviceConnection.kt
Created June 11, 2022 17:02
A ServiceConnection implementation
private lateinit var timerService: TimerService
private var isTimerServiceBound: Boolean = false
private val timerServiceConnection = object : ServiceConnection {
override fun onServiceConnected(name: ComponentName?, binder: IBinder?) {
timerService = (binder as TimerService.TimerBinder).service
isTimerServiceBound = true
}
override fun onServiceDisconnected(p0: ComponentName?) {
@molidev8
molidev8 / timerService.kt
Created June 11, 2022 16:49
A barebones foreground service
class TimerService : Service() {
private val binder: Binder = TimerBinder()
override fun onBind(p0: Intent?): IBinder = binder
inner class TimerBinder : Binder() {
val service: TimerService
get() = this@TimerService
}
@molidev8
molidev8 / receiver.kt
Created June 9, 2022 14:18
A callback to receive information with Android Nearby
private inner class DataReceivedCallback : PayloadCallback() {
private val incomingFilePayloads = SimpleArrayMap<Long, Payload>()
private val completedFilePayloads = SimpleArrayMap<Long, Payload>()
private var filePayloadFilename: String = ""
override fun onPayloadReceived(endPointId: String, payload: Payload) {
when (payload.type) {
Payload.Type.BYTES -> {
val gson = Gson()
val recipe =