Skip to content

Instantly share code, notes, and snippets.

@ronshapiro
ronshapiro / dagger-android-view.md
Last active April 24, 2024 00:19
dagger.android for views in ~10 minutes

1. You can implement View.getActivity() like this:

public interface ViewWithActivity {
  // Using android-gradle-plugin 3.0, which has the desugar step for default methods on interfaces
  default Activity getActivity() {
    // https://android.googlesource.com/platform/frameworks/support/+/03e0f3daf3c97ee95cd78b2f07bc9c1be05d43db/v7/mediarouter/src/android/support/v7/app/MediaRouteButton.java#276
    Context context = getContext();
    while (context instanceof ContextWrapper) {
      if (context instanceof Activity) {
@ronshapiro
ronshapiro / main.ts
Last active October 29, 2023 12:24
Typescript rewriting constructor properties
class F {
constructor(protected a: string) {
console.log("F.a", a);
console.log("F.this.a", this.a);
}
b() {
console.log("F.b.this.a", this.a);
}
}
@ronshapiro
ronshapiro / iOS_Android_Vocab_Cheat_Sheet.md
Last active February 1, 2023 07:54
iOS <> Android Vocab Cheat Sheet

iOS <> Android Vocab Cheat Sheet

A couple months ago while at Venmo, @chrismaddern and I discussed the lack of a bidirectional dictionary/vocab list of parallel terms in Android and iOS. It's important to know what your teammates are refering when they talk about code, even if you're not engaged with it every day. This is a beginning attempt to finally break down that knowledge gap. The goal is to allow developers of one framework to understand the basics of the other and should someone want to make the switch, this can be a resource in helping learn the new framework.

If there's a topic that you think would be interesting to discuss, tweet at me and we'll get include it in this list. I'll be updating this post with links to all future posts.

Disclaimer: I'm an Android developer and have done minimal iOS/Objective-C programming. If there is anything incorrect, reach out and I'll fix it as soon as possible. I'm assuming iOS developer are

Most Dagger errors are in the format:

src/main/java/dagger/playground/Playground.java:20: [Dagger/DuplicateBindings] dagger.playground.E is bound multiple times:
  A a();
    ^
      @Provides dagger.playground.E dagger.playground.Mod.first()
      @Provides dagger.playground.E dagger.playground.Mod.second()
  
      dagger.playground.E is injected at
@ronshapiro
ronshapiro / MonolithicActivityInjector.java
Created November 8, 2017 02:15
dagger.android without O(N) subcomponents
@Subcomponent
public abstract class MonolithicActivityInjector implements AndroidInjector<Activity> {
@Override
public final void inject(Activity activity) {
if (activity instanceof RedActivity) {
inject((RedActivity) activity);
} else if (activity instanceof BlueActivity) {
inject((BlueActivity) activity);
} else if (activity instanceof YellowActivity) {
inject((YellowActivity) activity);
@ronshapiro
ronshapiro / ReusableInjection.kt
Created October 2, 2017 14:26 — forked from ZacSweers/ReusableInjection.kt
Reusable injections
class Parent {
// Somewhere in here, a lot of external service modules are included and contribute their `Service` impls to this multibinding
@Inject lateinit var services: Map<String, Provider<Service>>
fun showChild() {
addChildController(serviceKey = "barServiceKey") // Key into the services map
}
}
@ronshapiro
ronshapiro / RxApiClient.java
Last active August 8, 2016 04:38
An API client shell that abstracts it's mechanism of executing network calls and provides a simple interface to making the calls using RxJava and Java 8.
package me.ronshapiro.rxjavatest.app;
import java.util.concurrent.Executors;
import rx.Observable;
import rx.Observable.OnSubscribe;
import rx.Scheduler;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
@ronshapiro
ronshapiro / maven-deploy.md
Last active June 1, 2016 12:29
Getting a jar on Maven Central: An Epic
@ronshapiro
ronshapiro / android-aliases.sh
Last active December 22, 2015 00:49
Some convenience aliases/functions for interfacing with adb
# easily parse Logcat output
alog () {
parser=ack
if [[ -e `which ack` ]]; then
parser=grep
fi
if (( $# == 0 )); then
adb logcat -v time
elif [[ $1 == "runtime" ]]; then
pattern='E/AndroidRuntime'
@ronshapiro
ronshapiro / semper-tmux.sh
Created December 19, 2012 01:06
A snippet to make sure you are always running a tmux session in your terminal. If you have an detached session, this will attach to it, otherwise, if there are no sessions or only ones which are attached, a new session will be started. Insert into your .bashrc or .bash_profile at the top to call every time you open a terminal session.
#############################################
# Attach tmux to the first detached session
#############################################
if [[ -z $TMUX ]]; then
TMUX_SESSIONS=`tmux list-sessions -F "#{session_name}ABC#{?session_attached,attached,not_attached}" 2>&1`
TMUX_FOUND=0
for i in $TMUX_SESSIONS
do
IS_ATTACHED=`echo $i | awk '{split($0,array,"ABC"); print array[2]}'`