Skip to content
This repository has been archived by the owner on Apr 22, 2022. It is now read-only.

Commit

Permalink
Fix: When type Parent->Child, enable Dispatcher<Child> to bind LightC…
Browse files Browse the repository at this point in the history
…ycle<Parent>.

The processor internaly lift the LightCycle<Parent> to  LightCycle<Child>.
  • Loading branch information
glung committed Mar 10, 2016
1 parent 21d2b27 commit 4a93722
Show file tree
Hide file tree
Showing 12 changed files with 491 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.soundcloud.lightcycle.sample.real_world;

import com.soundcloud.lightcycle.DefaultActivityLightCycle;

import android.app.Activity;

import javax.inject.Inject;
import javax.inject.Singleton;

@Singleton
class AppForegroundStateProvider extends DefaultActivityLightCycle<Activity> {

private boolean isForeground;

@Inject
public AppForegroundStateProvider() {
isForeground = false;
}

@Override
public void onResume(Activity activity) {
isForeground = true;
}

@Override
public void onPause(Activity activity) {
isForeground = false;
}

public boolean isForeground() {
return isForeground;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

public class SampleActivity extends MyBaseActivity {
@Inject @LightCycle ScreenTracker screenTracker;
@Inject @LightCycle AppForegroundStateProvider foregroundStateProvider;

@Override
public String getScreenName() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package com.soundcloud.lightcycle;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;

import java.lang.reflect.Method;

Expand Down Expand Up @@ -43,4 +48,200 @@ private static Method findBinderForClass(Class<?> cls)
}
return lightCycleInjectionMethod;
}

public static <Source extends Activity, Target extends Source> ActivityLightCycle<Target> lift(final ActivityLightCycle<Source> lightCycle) {
return new ActivityLightCycle<Target>() {

@Override
public void onCreate(Target activity, Bundle bundle) {
lightCycle.onCreate(activity, bundle);
}

@Override
public void onNewIntent(Target activity, Intent intent) {
lightCycle.onNewIntent(activity, intent);
}

@Override
public void onStart(Target activity) {
lightCycle.onStart(activity);
}

@Override
public void onResume(Target activity) {
lightCycle.onResume(activity);
}

@Override
public boolean onOptionsItemSelected(Target activity, MenuItem item) {
return lightCycle.onOptionsItemSelected(activity, item);
}

@Override
public void onPause(Target activity) {
lightCycle.onPause(activity);
}

@Override
public void onStop(Target activity) {
lightCycle.onStop(activity);
}

@Override
public void onSaveInstanceState(Target activity, Bundle bundle) {
lightCycle.onSaveInstanceState(activity, bundle);
}

@Override
public void onRestoreInstanceState(Target activity, Bundle bundle) {
lightCycle.onRestoreInstanceState(activity, bundle);
}

@Override
public void onDestroy(Target activity) {
lightCycle.onDestroy(activity);
}
};
}

public static <Source extends android.app.Fragment, Target extends Source> FragmentLightCycle<Target> lift(final FragmentLightCycle<Source> lightCycle) {
return new FragmentLightCycle<Target>() {

@Override
public void onAttach(Target fragment, Activity activity) {
lightCycle.onAttach(fragment, activity);
}

@Override
public void onCreate(Target fragment, Bundle bundle) {
lightCycle.onCreate(fragment, bundle);
}

@Override
public void onViewCreated(Target fragment, View view, Bundle savedInstanceState) {
lightCycle.onViewCreated(fragment, view, savedInstanceState);
}

@Override
public void onStart(Target fragment) {
lightCycle.onStart(fragment);
}

@Override
public void onResume(Target fragment) {
lightCycle.onResume(fragment);
}

@Override
public boolean onOptionsItemSelected(Target fragment, MenuItem item) {
return lightCycle.onOptionsItemSelected(fragment, item);
}

@Override
public void onPause(Target fragment) {
lightCycle.onPause(fragment);
}

@Override
public void onStop(Target fragment) {
lightCycle.onStop(fragment);
}

@Override
public void onSaveInstanceState(Target fragment, Bundle bundle) {
lightCycle.onSaveInstanceState(fragment, bundle);
}

@Override
public void onRestoreInstanceState(Target fragment, Bundle bundle) {
lightCycle.onRestoreInstanceState(fragment, bundle);
}

@Override
public void onDestroyView(Target fragment) {
lightCycle.onDestroyView(fragment);
}

@Override
public void onDestroy(Target fragment) {
lightCycle.onDestroy(fragment);
}

@Override
public void onDetach(Target fragment) {
lightCycle.onDetach(fragment);
}
};
}


public static <Source extends android.support.v4.app.Fragment, Target extends Source> SupportFragmentLightCycle<Target> lift(final SupportFragmentLightCycle<Source> lightCycle) {
return new SupportFragmentLightCycle<Target>() {

@Override
public void onAttach(Target fragment, Activity activity) {
lightCycle.onAttach(fragment, activity);
}

@Override
public void onCreate(Target fragment, Bundle bundle) {
lightCycle.onCreate(fragment, bundle);
}

@Override
public void onViewCreated(Target fragment, View view, Bundle savedInstanceState) {
lightCycle.onViewCreated(fragment, view, savedInstanceState);
}

@Override
public void onStart(Target fragment) {
lightCycle.onStart(fragment);
}

@Override
public void onResume(Target fragment) {
lightCycle.onResume(fragment);
}

@Override
public boolean onOptionsItemSelected(Target fragment, MenuItem item) {
return lightCycle.onOptionsItemSelected(fragment, item);
}

@Override
public void onPause(Target fragment) {
lightCycle.onPause(fragment);
}

@Override
public void onStop(Target fragment) {
lightCycle.onStop(fragment);
}

@Override
public void onSaveInstanceState(Target fragment, Bundle bundle) {
lightCycle.onSaveInstanceState(fragment, bundle);
}

@Override
public void onRestoreInstanceState(Target fragment, Bundle bundle) {
lightCycle.onRestoreInstanceState(fragment, bundle);
}

@Override
public void onDestroyView(Target fragment) {
lightCycle.onDestroyView(fragment);
}

@Override
public void onDestroy(Target fragment) {
lightCycle.onDestroy(fragment);
}

@Override
public void onDetach(Target fragment) {
lightCycle.onDetach(fragment);
}
};
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.soundcloud.lightcycle;

// TODO : rename ?
public interface LightCycleDispatcher<LightCycle> {

void bind(LightCycle lightCycle);
Expand Down
Loading

0 comments on commit 4a93722

Please sign in to comment.