Skip to content

Instantly share code, notes, and snippets.

View stephanenicolas's full-sized avatar

Stéphane Nicolas stephanenicolas

View GitHub Profile
@stephanenicolas
stephanenicolas / TP.java
Last active April 25, 2019 17:38
TP for Kotlin & java
import toothpick.Scope
import toothpick.Toothpick
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty
annotation class ActivityScope
annotation class FreeScope
//-------------------------
// case: Entry point
@stephanenicolas
stephanenicolas / HeadLineFragment.java
Last active September 28, 2017 06:28
Invoke the listener method reference on the current activity
public class HeadLineFragment extends ListFragment {
@Override
public void onListItemClick(ListView l, View v, int p, long i) {
// Send the event to the host activity
// we should probably check if the activity is finishing|destroyed|rotating
mCallback.onArticleSelected(getActivity(), p);
}
}
@stephanenicolas
stephanenicolas / HeadLineFragment.java
Last active September 27, 2017 00:02
setHeadlineListener uses Generics to trigger target type inference
public class HeadLineFragment extends Fragment {
public <T> void setHeadlineListener(HeadlineListener<T> listener) {
...
}
}
@stephanenicolas
stephanenicolas / MainActivity.java
Created September 26, 2017 23:39
Non-Static methods hidden parameters
public static class MainActivity extends Activity {
public void onArticleSelected(int position) {
}
public static void onArticleSelected(MainActivity activity, int position) {
}
}
@stephanenicolas
stephanenicolas / RxChain.java
Created September 26, 2017 23:35
Using a non static method reference in a Rx chain
just(0)
.map(this::onArticleSelected)
.subscribe(...);
@stephanenicolas
stephanenicolas / MainActivity.java
Created September 26, 2017 23:23
The Lambda Way: setting the listener
public static class MainActivity extends Activity {
public void bindFragment(HeadlinesFragment fragment) {
fragment.setHeadlineListener(MainActivity::onArticleSelected);
}
public void onArticleSelected(int position) {
...
}
}
@stephanenicolas
stephanenicolas / HeadlinesFragment.java
Last active September 27, 2017 00:01
Lambda Way: HeadlinesFragment
public class HeadlinesFragment extends ListFragment {
HeadlineListener mCallback;
public <T> void setHeadlineListener(HeadlineListener<T> listener) {
this.mCallBack = listener;
}
@Override
public void onListItemClick(ListView l, View v, int p, long i) {
// Send the event to the host activity
@stephanenicolas
stephanenicolas / HeadlineListener.java
Created September 26, 2017 23:20
Lambda Way: HeadlineListener
public interface HeadlineListener<T extends Activity> extends Serializable {
public void onArticleSelected(T activity, int position);
}
@stephanenicolas
stephanenicolas / MainActivity.java
Created September 26, 2017 23:20
Lambda way: MainActivity
public static class MainActivity extends Activity {
public void onArticleSelected(int position) {
}
}
@stephanenicolas
stephanenicolas / HeadlinesFragment.java
Created September 26, 2017 23:12
Good Old Way: Fragment
public class HeadlinesFragment extends ListFragment {
HeadlineListener mCallback;
// Container Activity must implement this interface
public interface HeadlineListener {
public void onArticleSelected(int position);
}
@Override
public void onAttach(Activity activity) {