Skip to content

Instantly share code, notes, and snippets.

@StanGenchev
StanGenchev / NotifyingLinearLayoutManager.kt
Last active December 4, 2019 12:10
LinearLayoutManager with 'onLayoutCompleted' callback. You can use this to execute code after the layout has finished drawing on the screen. You can also use 'isLastItemCompletelyVisible' to enable/disable the nested scrolling, for a better experience with Appbars who have 'appbar_scrolling_view_behavior"' set.
class NotifyingLinearLayoutManager(context: Context?) : LinearLayoutManager(context, VERTICAL, false) {
var onLayoutCompleteCallback: OnLayoutCompleteCallback? = null
override fun onLayoutCompleted(state: RecyclerView.State?) {
super.onLayoutCompleted(state)
onLayoutCompleteCallback?.onLayoutComplete()
}
fun isLastItemCompletelyVisible() = findLastCompletelyVisibleItemPosition() == itemCount - 1
}
@StanGenchev
StanGenchev / NotifyingGridLayoutManager.kt
Last active December 4, 2019 12:09
GridLayoutManager with 'onLayoutCompleted' callback. You can use this to execute code after the layout has finished drawing on the screen. You can also use 'isLastItemCompletelyVisible' to enable/disable the nested scrolling, for a better experience with Appbars who have 'appbar_scrolling_view_behavior"' set.
class NotifyingGridLayoutManager(context: Context?) : GridLayoutManager(context, 1) {
var onLayoutCompleteCallback: OnLayoutCompleteCallback? = null
override fun onLayoutCompleted(state: RecyclerView.State?) {
super.onLayoutCompleted(state)
onLayoutCompleteCallback?.onLayoutComplete()
}
fun isLastItemCompletelyVisible() = findLastCompletelyVisibleItemPosition() == itemCount - 1
}
@StanGenchev
StanGenchev / AutoFitGrid.kt
Created December 4, 2019 12:21
AutoFitGrid/AutoFitStaggerdGrid is a RecyclerView with a Grid/StaggeredGridLayoutManager which automatically sets the number of columns based on the column width.
class AutoFitGrid : RecyclerView {
private var manager: GridLayoutManager? = null
private var columnWidth = -1
constructor(context: Context) : super(context) {
initialization(context, null)
}
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
initialization(context, attrs)
@StanGenchev
StanGenchev / EventDispatcherExample.py
Created December 15, 2019 14:26
My 'Event Dispatcher' implementation in python.
class EventDispatcher:
def __init__(self):
self.__eventhandlers = {}
def add_event(self, event, handler):
self.__eventhandlers[event] = handler
def remove_event(self, event):
del self.__eventhandlers[event]
@StanGenchev
StanGenchev / gtk-get-screen-workarea.py
Created December 22, 2019 12:01
Example showing how to get monitor resolution and/or work area in Python 3 and Gtk 3.22+.
#!/usr/bin/env python3
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk
import sys
class MyWindow(Gtk.ApplicationWindow):
def __init__(self, app):
Gtk.Window.__init__(self, title="Gtk get screen workarea", application=app)
@StanGenchev
StanGenchev / install-erlang-elixir-el8.sh
Last active April 23, 2024 14:01
Install Erlang and Elixir on CentOS 8 or Red Hat Enterprise Linux 8
#!/bin/sh
# This file can be used as an auto installer or you can mannually execute the steps yourself.
# If you use it as an installer, you must run it as root.
# Check if the script is run with sudo
if [ "$EUID" -ne 0 ]
then echo "Please run as root or with sudo"
exit
else
echo "Installing Erlang and Elixir..."
@StanGenchev
StanGenchev / install-mosquitto-el8.sh
Last active December 12, 2022 18:57
Install mosquitto MQTT on CentOS 8 or Red Hat Enterprise Linux 8
#!/bin/sh
# This file can be used as an auto installer or you can mannually execute the steps yourself.
# If you use it as an installer, you must run it as root.
# Check if the script is run with sudo
if [ "$EUID" -ne 0 ]
then echo "Please run as root or with sudo"
exit
else
echo "Installing Mosquitto..."
@StanGenchev
StanGenchev / passwordless_sudo_user_linux.md
Last active October 16, 2020 11:42
Create passwordless sudo user in Linux

Create passwordless user in Linux:

Create the user

adduser <user>

Remove password in RedHat/CentOS/Fedora Linux:

AWSTemplateFormatVersion: 2010-09-09
Transform:
- AWS::Serverless-2016-10-31
Parameters:
RootDomainName:
Description: Domain name for your website
Type: String
Mappings:
@StanGenchev
StanGenchev / get_scaled_resolution.py
Created June 21, 2020 16:44
Calculate resolution while preserving the aspect ratio with 'fit' effect in python.
def get_scaled_resolution(
self,
old_width: int = None,
old_height: int = None,
new_width: int = None,
new_height: int = None,
):
"""Returns the closest new resolution while preserving the aspect ratio.
You can specify new width, height or both.
It will automatically calculate the resolution.