Skip to content

Commit

Permalink
For applications, it allows presenters (LightCycles) to use views - w…
Browse files Browse the repository at this point in the history
…hich can be implemented by Fragment or Activity classes.

- Dispatcher: dispatch the Android life cycle (Activity, SupportFragment, Fragment) to LightCycles.
- Host: the activity/fragment as viewed by the light cycle. It can be a view.
- LightCycle: notified of the life cycle with the Host as a parameter.

#48
  • Loading branch information
glung committed Dec 15, 2016
1 parent a0ca7e7 commit cc8630b
Show file tree
Hide file tree
Showing 33 changed files with 406 additions and 398 deletions.
2 changes: 1 addition & 1 deletion examples/real-world/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity
android:name=".HomeActivity"
android:name=".home.HomeActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.soundcloud.lightcycle.sample.real_world;

import com.soundcloud.lightcycle.sample.real_world.home.HomeActivity;
import com.soundcloud.lightcycle.sample.real_world.license.LicenseFragment;
import dagger.Module;
import dagger.Provides;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.soundcloud.lightcycle.sample.real_world;
package com.soundcloud.lightcycle.sample.real_world.home;

import com.soundcloud.lightcycle.DefaultActivityLightCycle;

Expand All @@ -7,14 +7,14 @@

import javax.inject.Inject;

class DescriptionPresenter extends DefaultActivityLightCycle<HomeActivity> {
class DescriptionPresenter extends DefaultActivityLightCycle<HomeView> {

@Inject
public DescriptionPresenter() {}

@Override
public void onCreate(HomeActivity activity, @Nullable Bundle bundle) {
activity.showDescription("LightCycle", "https://github.com/soundcloud/lightcycle");
public void onCreate(HomeView homeView, @Nullable Bundle bundle) {
homeView.showDescription("LightCycle", "https://github.com/soundcloud/lightcycle");
}

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.soundcloud.lightcycle.sample.real_world;
package com.soundcloud.lightcycle.sample.real_world.home;

import com.soundcloud.lightcycle.DefaultActivityLightCycle;
import com.soundcloud.lightcycle.sample.real_world.R;
import com.soundcloud.lightcycle.sample.real_world.utils.DateProvider;

import android.os.Bundle;
import android.support.annotation.Nullable;

import javax.inject.Inject;

class HeaderPresenter extends DefaultActivityLightCycle<HomeActivity> {
class HeaderPresenter extends DefaultActivityLightCycle<HomeView> {
private final DateProvider dateProvider;

@Inject
Expand All @@ -17,11 +18,11 @@ public HeaderPresenter(DateProvider dateProvider) {
}

@Override
public void onCreate(HomeActivity activity, @Nullable Bundle bundle) {
public void onCreate(HomeView homeView, @Nullable Bundle bundle) {
if (dateProvider.isMorning()) {
activity.sayHello(R.string.good_morning);
homeView.sayHello(R.string.good_morning);
} else {
activity.sayHello(R.string.hello);
homeView.sayHello(R.string.hello);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package com.soundcloud.lightcycle.sample.real_world;
package com.soundcloud.lightcycle.sample.real_world.home;

import com.soundcloud.lightcycle.LightCycle;
import com.soundcloud.lightcycle.LightCycleAppCompatActivity;
import com.soundcloud.lightcycle.sample.real_world.tracker.Screen;
import com.soundcloud.lightcycle.sample.real_world.R;
import com.soundcloud.lightcycle.sample.real_world.SampleApp;
import com.soundcloud.lightcycle.sample.real_world.tracker.ScreenTracker;

import android.support.annotation.StringRes;
import android.widget.TextView;

import javax.inject.Inject;

public class HomeActivity extends LightCycleAppCompatActivity<HomeActivity> implements Screen {
public class HomeActivity extends LightCycleAppCompatActivity<HomeView> implements HomeView {
@Inject @LightCycle ScreenTracker screenTracker;
@Inject @LightCycle HomePresenter headerPresenter;

Expand All @@ -20,20 +21,22 @@ public HomeActivity() {

@Override
public String getScreenName() {
return "HomeActivity";
return "HomeScreen";
}

@Override
protected void setActivityContentView() {
setContentView(R.layout.activity_home);
}

void sayHello(@StringRes int hello) {
((TextView) findViewById(R.id.hello)).setText(hello);
@Override
public void sayHello(@StringRes int message) {
((TextView) findViewById(R.id.hello)).setText(message);
}

void showDescription(String title, String description) {
@Override
public void showDescription(String title, String message) {
((TextView) findViewById(R.id.title)).setText(title);
((TextView) findViewById(R.id.description)).setText(description);
((TextView) findViewById(R.id.description)).setText(message);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.soundcloud.lightcycle.sample.real_world;
package com.soundcloud.lightcycle.sample.real_world.home;

import com.soundcloud.lightcycle.ActivityLightCycleDispatcher;
import com.soundcloud.lightcycle.LightCycle;
Expand All @@ -9,7 +9,7 @@

import javax.inject.Inject;

class HomePresenter extends ActivityLightCycleDispatcher<HomeActivity> {
class HomePresenter extends ActivityLightCycleDispatcher<HomeView> {
@LightCycle final HeaderPresenter headerPresenter;
@LightCycle final DescriptionPresenter descriptionPresenter;

Expand All @@ -20,8 +20,8 @@ class HomePresenter extends ActivityLightCycleDispatcher<HomeActivity> {
}

@Override
public void onCreate(HomeActivity activity, @Nullable Bundle bundle) {
public void onCreate(HomeView homeView, @Nullable Bundle bundle) {
LightCycles.bind(this);
super.onCreate(activity, bundle);
super.onCreate(homeView, bundle);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.soundcloud.lightcycle.sample.real_world.home;

import com.soundcloud.lightcycle.sample.real_world.tracker.Screen;

import android.support.annotation.StringRes;

interface HomeView extends Screen {

void sayHello(@StringRes int message);

void showDescription(String title, String message);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.soundcloud.lightcycle.sample.real_world;
package com.soundcloud.lightcycle.sample.real_world.license;

import com.soundcloud.lightcycle.LightCycle;
import com.soundcloud.lightcycle.LightCycleSupportFragment;
import com.soundcloud.lightcycle.sample.real_world.R;
import com.soundcloud.lightcycle.sample.real_world.SampleApp;

import android.os.Bundle;
import android.view.LayoutInflater;
Expand All @@ -11,21 +13,21 @@

import javax.inject.Inject;

public class LicenseFragment extends LightCycleSupportFragment<LicenseFragment> {
@Inject @LightCycle LicensePresenter descriptionPresenter;
public class LicenseFragment extends LightCycleSupportFragment<LicenseView> implements LicenseView {
@Inject @LightCycle LicensePresenter licensePresenter;

public LicenseFragment() {
SampleApp.getObjectGraph().inject(this);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_license, container, false);
}

void showLicense(String readme) {
((TextView) getView().findViewById(R.id.license)).setText(readme);
@Override
public void showLicense(String text) {
((TextView) getView().findViewById(R.id.license)).setText(text);
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.soundcloud.lightcycle.sample.real_world;
package com.soundcloud.lightcycle.sample.real_world.license;

import com.soundcloud.lightcycle.DefaultSupportFragmentLightCycle;

Expand All @@ -7,14 +7,14 @@

import javax.inject.Inject;

class LicensePresenter extends DefaultSupportFragmentLightCycle<LicenseFragment> {
class LicensePresenter extends DefaultSupportFragmentLightCycle<LicenseView> {

@Inject
public LicensePresenter() {
}

@Override
public void onViewCreated(LicenseFragment fragment, View view, Bundle savedInstanceState) {
fragment.showLicense("Apache License 2.0");
public void onViewCreated(LicenseView licenseView, View view, Bundle savedInstanceState) {
licenseView.showLicense("Apache License 2.0");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.soundcloud.lightcycle.sample.real_world.license;

interface LicenseView {
void showLicense(String text);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@

public interface Screen {
String getScreenName();

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.soundcloud.lightcycle.sample.real_world.tracker;

import com.soundcloud.lightcycle.DefaultActivityLightCycle;
import com.soundcloud.lightcycle.sample.real_world.HomeActivity;

import javax.inject.Inject;

public class ScreenTracker extends DefaultActivityLightCycle<HomeActivity> {
public class ScreenTracker extends DefaultActivityLightCycle<Screen> {

private final TrackingOperations operations;

Expand All @@ -15,7 +14,7 @@ public ScreenTracker(TrackingOperations operations) {
}

@Override
public void onResume(HomeActivity activity) {
operations.trackScreen(activity);
public void onResume(Screen screen) {
operations.trackScreen(screen);
}
}
2 changes: 1 addition & 1 deletion examples/real-world/src/main/res/layout/activity_home.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

<fragment
android:id="@+id/my_fragment"
android:name="com.soundcloud.lightcycle.sample.real_world.LicenseFragment"
android:name="com.soundcloud.lightcycle.sample.real_world.license.LicenseFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LicenseFragment">
tools:context=".license.LicenseFragment">

<TextView
android:id="@+id/license"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.soundcloud.lightcycle.sample.real_world;
package com.soundcloud.lightcycle.sample.real_world.home;

import static org.mockito.Mockito.verify;

import com.soundcloud.lightcycle.sample.real_world.R;
import com.soundcloud.lightcycle.sample.real_world.utils.DateProvider;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -12,7 +13,7 @@

@RunWith(MockitoJUnitRunner.class)
public class HeaderPresenterTest {
@Mock private HomeActivity activity;
@Mock private HomeView view;

@Test
public void sayHelloWhenNotMorning() {
Expand All @@ -23,9 +24,9 @@ public boolean isMorning() {
}
});

presenter.onCreate(activity, Bundle.EMPTY);
presenter.onCreate(view, Bundle.EMPTY);

verify(activity).sayHello(R.string.hello);
verify(view).sayHello(R.string.hello);
}

@Test
Expand All @@ -37,8 +38,8 @@ public boolean isMorning() {
}
});

presenter.onCreate(activity, Bundle.EMPTY);
presenter.onCreate(view, Bundle.EMPTY);

verify(activity).sayHello(R.string.good_morning);
verify(view).sayHello(R.string.good_morning);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import static org.mockito.Mockito.verify;

import com.soundcloud.lightcycle.sample.real_world.HomeActivity;
import com.soundcloud.lightcycle.sample.real_world.home.HomeActivity;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package com.soundcloud.lightcycle;

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

public interface ActivityLightCycle<T extends Activity> {
void onCreate(T activity, Bundle bundle);
void onNewIntent(T activity, Intent intent);
void onStart(T activity);
void onResume(T activity);
boolean onOptionsItemSelected(T activity, MenuItem item);
void onPause(T activity);
void onStop(T activity);
void onSaveInstanceState(T activity, Bundle bundle);
void onRestoreInstanceState(T activity, Bundle bundle);
void onDestroy(T activity);
public interface ActivityLightCycle<T> {
void onCreate(T host, Bundle bundle);
void onNewIntent(T host, Intent intent);
void onStart(T host);
void onResume(T host);
boolean onOptionsItemSelected(T host, MenuItem item);
void onPause(T host);
void onStop(T host);
void onSaveInstanceState(T host, Bundle bundle);
void onRestoreInstanceState(T host, Bundle bundle);
void onDestroy(T host);
}
Loading

0 comments on commit cc8630b

Please sign in to comment.