Skip to content

Instantly share code, notes, and snippets.

View n0m0r3pa1n's full-sized avatar

Georgi Mirchev n0m0r3pa1n

View GitHub Profile
@n0m0r3pa1n
n0m0r3pa1n / ListAdapterStrategy.kt
Created July 7, 2022 08:52
ListAdapterStrategy for Android mobile and TV
class MobileListAdapterStrategy<T : Any, VH : RecyclerView.ViewHolder>(
override val adapter: RecyclerView.Adapter<VH>,
private val diffCallback: DiffUtil.ItemCallback<T>
) : ListAdapterStrategy<T, VH> {
private val listAdapter = object : ListAdapter<T, VH>(diffCallback) {
fun getItemWithPosition(position: Int) = getItem(position)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
return this.onCreateViewHolder(parent, viewType)
}
@n0m0r3pa1n
n0m0r3pa1n / OkHttpWebSocketClient.kt
Last active April 23, 2024 06:30
OkHttpWebSocketClient
import io.reactivex.subjects.PublishSubject
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.launch
import kotlinx.coroutines.newSingleThreadContext
import kotlinx.coroutines.suspendCancellableCoroutine
import android.app.Dialog
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.DatePicker
import androidx.appcompat.app.AlertDialog
import androidx.core.os.bundleOf
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.activityViewModels
import dagger.hilt.android.AndroidEntryPoint
@n0m0r3pa1n
n0m0r3pa1n / RoundedCornersDrawable.xml
Created July 5, 2021 09:06
Rounded corners drawable for Android
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:topLeftRadius="24dp"
android:topRightRadius="24dp" />
<stroke
android:width="1dp"
android:color="@color/divider_dark"/>
</shape>
@n0m0r3pa1n
n0m0r3pa1n / BottomSheetExtensions.kt
Last active October 2, 2023 01:53
Bottom sheet dialog with expanding and fullscreen display
fun Fragment.showBottomSheetDialog(
@LayoutRes layout: Int,
@IdRes textViewToSet: Int? = null,
textToSet: String? = null,
fullScreen: Boolean = true,
expand: Boolean = true
) {
val dialog = BottomSheetDialog(context!!)
dialog.setOnShowListener {
val bottomSheet: FrameLayout = dialog.findViewById(com.google.android.material.R.id.design_bottom_sheet) ?: return@setOnShowListener
@n0m0r3pa1n
n0m0r3pa1n / SampleCastExoplayer.kt
Created June 11, 2021 11:33
Sample player that supports both cast & simple exo player
import com.google.android.exoplayer2.*
import com.google.android.exoplayer2.audio.AudioAttributes
import com.google.android.exoplayer2.audio.AudioListener
import com.google.android.exoplayer2.ext.cast.CastPlayer
import com.google.android.exoplayer2.ext.cast.SessionAvailabilityListener
import com.google.android.exoplayer2.ext.okhttp.OkHttpDataSource
import com.google.android.exoplayer2.source.DefaultMediaSourceFactory
import com.google.android.exoplayer2.source.ProgressiveMediaSource
import com.google.android.exoplayer2.upstream.FileDataSource
import okhttp3.OkHttpClient

Effective Java Notes

Creating and Destroying Objects

Item 1: Consider static factory methods instead of constructors

  • One advantage of static factory methods is that, unlike constructors, they have names
  • A second advantage of static factory methods is that, unlike constructors, they are not required to create a new object each time they’re invoked
  • A third advantage of static factory methods is that, unlike constructors, they can return an object of any subtype of their return type
  • The main disadvantage of providing only static factory methods is that classes without public or protected constructors cannot be subclassed
@n0m0r3pa1n
n0m0r3pa1n / dev-toolbox-2016.md
Created April 1, 2017 06:44
Android Developer Toolbox - Notes

Gradle

  • Create different build variants which contain different code of your app
    • Internal - for the company. Embed a lot of tools to locate issues easily.
    • Production - only for customers

U+2020 application

Redux Rules

  1. Immutable state - it should not be changeable instead is replaced. Everything that changes in your app including the date and the UI state is stored in a single object tree.
  2. State is read only - to make a change you need to dispatch an action. An action is a plain javascript object. State = data, change = action

Pure vs Impure functions

  1. Pure 2. Depend only on their input params 3. Some value for some input elements
//This method is supposed to tell the sockets server
//to post an refresh event with data to a selected client id
export function* postRefreshEvent(clientId, data) {
var connection = require('socket.io-emitter')({ host: '127.0.0.1', port: 8081 });
connection.emit('refresh', clientId, data);
return {statusCode: OK}
}
// Websockets