Skip to content

Instantly share code, notes, and snippets.

View Frank1234's full-sized avatar

Frank Frank1234

  • Amsterdam, Netherlands
View GitHub Profile
@Frank1234
Frank1234 / PieGraph.java
Last active October 5, 2017 08:07
PieGraph, showing progress on a circle chart.
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
/**
* Shows progress on a circle chart.
@Frank1234
Frank1234 / EspressoViewFinder.kt
Last active November 23, 2019 13:05
Helper methods for Espresso and React Native.
import android.graphics.Rect
import android.support.test.espresso.Espresso.onView
import android.support.test.espresso.PerformException
import android.support.test.espresso.UiController
import android.support.test.espresso.ViewAction
import android.support.test.espresso.matcher.ViewMatchers.*
import android.support.test.espresso.util.HumanReadables
import android.support.test.espresso.util.TreeIterables
import android.view.View
import android.view.ViewGroup
@Frank1234
Frank1234 / testUtils.js
Last active November 23, 2019 13:06
Class that can add testIDs on both Android and iOS debug builds
import {Platform} from 'react-native';
const getPlatformTestId = (id: string) =>
Platform.OS === 'ios' ? {testID: id} : {accessible: true, accessibilityLabel: id};
/**
* Adds a testID to the views on Android and iOS in their specific ways. On Android,
* this will result in a ContentDescription on Debug builds (and no changes on live builds).
*/
const setTestID = (id : string) =>
@Frank1234
Frank1234 / ExampleFragment.kt
Last active February 16, 2020 09:39
Example Fragment for ViewBindingHolder
class ExampleFragment : Fragment(), ViewBindingHolder<FragmentExampleBinding> by ViewBindingHolderImpl() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? = initBinding(FragmentExampleBinding.inflate(layoutInflater), this) {
// do your onCreateView logic here, where 'this' is the binding object.
textTitle.text = "Clean ViewBinding and Lifecycle handling"
@Frank1234
Frank1234 / ApiMainHeadersProvider.kt
Last active February 27, 2020 10:27
Provides Retrofit Headers
class ApiMainHeadersProvider {
/**
* Public headers for calls that do not need an authenticated user.
*/
fun getPublicHeaders(): PublicHeaders =
PublicHeaders().apply {
putAll(getDefaultHeaders())
}
@Frank1234
Frank1234 / ViewBindingHolder.kt
Last active December 14, 2020 11:36
Holds and manages ViewBinding inside a Fragment.
/**
* Holds and manages ViewBinding inside a Fragment.
*/
interface ViewBindingHolder<T : ViewBinding> {
val binding: T?
/**
* Saves the binding for cleanup on onDestroy, calls the specified function [onBound] with `this` value
* as its receiver and returns the bound view root.
_...
o_.-"` `\
.--. _ `'-._.-'""-; _
.' \`_\_ {_.-a"a-} _ / \
_/ .-' '. {c-._o_.){\|` |
/**
* Returns true if the navigation controller is still pointing at 'this' fragment, or false if it already navigated away.
*/
fun Fragment.mayNavigate(): Boolean {
val navController = findNavController()
val destinationIdInNavController = navController.currentDestination?.id
// add tag_navigation_destination_id to your ids.xml so that it's unique:
val destinationIdOfThisFragment = view?.getTag(R.id.tag_navigation_destination_id) ?: destinationIdInNavController
/**
* Navigates only if this is safely possible; when this Fragment is still the current destination.
*/
fun Fragment.navigateSafe(
@IdRes resId: Int,
args: Bundle? = null,
navOptions: NavOptions? = null,
navigatorExtras: Navigator.Extras? = null
) {
if (mayNavigate()) findNavController().navigate(