Skip to content

Instantly share code, notes, and snippets.

View ktchernov's full-sized avatar

Konstantin Tchernov ktchernov

View GitHub Profile
@ktchernov
ktchernov / ip_list_with_ports.sh
Last active June 9, 2022 22:16
Bash array join
# turns a port and an IP array into a string like: "127.0.0.1:4430,10.33.0.6:4430"
function ip_list_with_ports {
local port=$1
shift 1
local ips=("$@")
for ((i=0; i<${#ips[@]}; i++)); do
echo -n "${ips[$i]}:$port"
if [ $i != $(( ${#ips[@]} - 1 )) ]; then
echo -n ","
fi
@ktchernov
ktchernov / dates.py
Last active June 7, 2022 21:43
Python dates
import datetime
# Python date and datetime in ISO 8601 format
# Example local timezone is UTC+12
isodate = datetime.datetime.now().date().isoformat()
print(isodate) # prints 2022-06-07 - Current timezone
isodateutc = datetime.datetime.utcnow().date().isoformat()
print(isodateutc) # prints 2022-06-06 - UTC timezone
@ktchernov
ktchernov / Dockerfile
Created May 30, 2022 02:30
Dockerfile users
FROM debian:11
# Add a user my-user (1001) to group my-group (gid 1001)
RUN addgroup --system --gid 1001 my-group && \
adduser --system --gid 1001 --uid 1001 --disabled-password my-user
package io.github.ktchernov
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import javax.inject.Provider
abstract class ViewModelFactory <VM : ViewModel> protected constructor(
private val provider: Provider<VM>) : ViewModelProvider.Factory {
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
val packages
= packageManager.getInstalledApplications(PackageManager.GET_META_DATA)
@ktchernov
ktchernov / update_gradle.sh
Created May 9, 2018 02:59
Update gradle version
#!/bin/sh
./gradlew wrapper --gradle-version $1 --distribution-type all
@ktchernov
ktchernov / prune_local_branches
Created May 9, 2018 02:58
Prune local branches
#!/bin/bash
#set -ex
branch_name=$(git symbolic-ref -q HEAD)
branch_name=${branch_name##refs/heads/}
branch_name=${branch_name:-HEAD}
if [[ $branch_name = "HEAD" ]]; then
echo "Cannot work with a detached head"
@ktchernov
ktchernov / delete_merged_branches.sh
Created August 21, 2017 21:45
Delete merged branches
#!/bin/sh
git branch --merged | egrep -v "(^\*|master|dev)" | xargs git branch -d
@ktchernov
ktchernov / jdk.table.xml
Created June 20, 2017 05:26
Workaround for missing Android API level 26 sources in Android Studio 3 Canary (not yet made available to download by Google). Substitutes API 25 sources.
<!-- 0. Make sure you've got API level 25 sources
1. Open ~/Library/Preferences/AndroidStudioPreview3.0/options/jdk.table.xml (macOS path)
2. Find "API 26 Platform" section
3. Replace the <sourcePath> XML node in this section with the following (again, macOS path) -->
<sourcePath>
<root type="composite">
<root type="simple" url="file://$USER_HOME$/Library/Android/sdk/sources/android-25" />
</root>
</sourcePath>
@ktchernov
ktchernov / StateAwareAppBarLayout.java
Created June 13, 2017 01:08
An extension of Android AppBarLayout, that allows to receive state changes (collapsed, expanded and idle). This is a simplified version of ControllableAppBarLayout (https://gist.github.com/skimarxall/863585dcd7abde8f4153) - only implementing a state change listener.
package io.github.ktchernov.play_aroundapp.views;
/**
* Copyright 2017 Konstantin Tchernov
*
* Adapted from: ControllableAppBarLayout by Bartosz Lipinski and
* Marcel Pintó Biescas
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.