Skip to content

Instantly share code, notes, and snippets.

View raxityo's full-sized avatar
👋

Raxit Majithiya raxityo

👋
View GitHub Profile
@raxityo
raxityo / docker-ssh.sh
Created July 13, 2017 01:24
SSH to a docker container by its container_id. Usage: docker-ssh <container_id>.
# !bin/bash
docker-ssh ()
{
if [[ $1 ]]; then
docker exec -it $1 bash
else
echo "Usage: docker-ssh <container_id>"
fi
}
@raxityo
raxityo / gobs-program.js
Created July 25, 2017 00:59
Gob's Program. As seen in Arrested Development, Season 3 Episode 11.
const rl = require('readline').createInterface({
input: process.stdin,
output: process.stdout
})
let question =
'\033c' + // Clear screen
'\n'.repeat(process.stdout.rows - 4) + // Move prompt to the bottom
'Gob\'s Program: Y/N?\n? '
rl.question(question, (answer) => {
if (answer.trim().toLowerCase() === 'y')
@raxityo
raxityo / UICollectionView+scrollToBottom.swift
Last active February 16, 2023 04:29
UICollectionView extension to programmatically scroll to last item in the CollectionView (using datasource instead of scrollToVisibleRect).
import UIKit
extension UICollectionView {
// MARK: - UICollectionView scrolling/datasource
/// Last Section of the CollectionView
var lastSection: Int {
return numberOfSections - 1
}
@raxityo
raxityo / MySocketManager.swift
Created July 19, 2018 05:22
SocketIO + RxSwift = Awesomeness 🚀
import RxCocoa
import RxSwift
import SocketIO
final class MySocketManager {
struct Relays {
// Custom events:
/// User properties updated
@raxityo
raxityo / DragAndDropViewController.swift
Created February 5, 2019 18:56
Sample ViewController to simulate simple Drag and Drop.
import UIKit
class DragAndDropViewController: UIViewController, UIGestureRecognizerDelegate {
private var sourceView: UIView!
private var dropArea: UIView!
private var objectSize = CGSize(width: 100, height: 100)
private var originalPosition = CGPoint.zero
override func loadView() {
@raxityo
raxityo / TokenGenerator.swift
Created February 11, 2019 18:13
Sample class to demonstrate usage of `try?` in Swift
class TokenGenerator {
internal struct TokenError: Error {}
private var canObtainToken = true
private var token = "TOKEN"
func obtainToken() throws -> String {
guard canObtainToken else {
throw TokenError()
@raxityo
raxityo / tryOptional.kt
Last active February 11, 2019 18:52
A Kotlin extension to introduce optional chaining to error handling.
inline fun <T> tryOptional(expression: () -> T): T? {
return try {
expression()
} catch (ex: Throwable) {
null
}
}
@raxityo
raxityo / TokenGenerator.kt
Created February 11, 2019 18:53
Sample class to demonstrate usage of `tryOptional` in Kotlin. See: https://gist.github.com/raxityo/8a109787966abc38c27b5620ce93b225
class TokenGenerator {
private var canObtainToken = true
private var token = "TOKEN"
fun obtainToken(): String {
if (!canObtainToken) {
throw Error()
}
@raxityo
raxityo / RetrofitExtensions.kt
Created April 17, 2019 22:23
Retrofit extensions to save file to Disk.
import android.content.Context
import io.reactivex.Observable
import okhttp3.ResponseBody
import okio.Okio
import retrofit2.Response
import java.io.File
private val <T> Response<T>.attachmentFileName
get() = headers()["Content-Disposition"]
?.removePrefix("attachment; filename=")
@raxityo
raxityo / FragmentManagerExtensions.kt
Last active April 30, 2019 19:15
FragmentManager extensions. A set of extensions to easily push a fragment to a container with one function.
import androidx.annotation.IdRes
import androidx.fragment.app.*
/**
* Push [Fragment] into container that gets added to back-stack and later be popped by popping the
* back-stack.
*
* Sample Usage:
* ```
* childFragmentManager.push(myFragment, R.id.childContainer)