Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Migrating fragment communication to the EventManager in RoboGuice

  • Loading branch information...
commit 38c565a87d1bb48252a26b245297276c8e37390b 1 parent d289824
@donnfelker authored
View
BIN  libs/android-support-v4.jar
Binary file not shown
View
BIN  libs/guice-3.0-no_aop.jar
Binary file not shown
View
BIN  libs/roboguice-2.0-RC2.jar
Binary file not shown
View
6 res/values/roboguice.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+ <string-array name="roboguice_modules">
+ <item>com.agilevent.repairshop.RepairShopModule</item>
+ </string-array>
+</resources>
View
22 src/com/agilevent/repairshop/MainActivity.java
@@ -5,23 +5,19 @@
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
+import com.google.inject.Inject;
+import roboguice.activity.RoboFragmentActivity;
+import roboguice.event.EventManager;
+import roboguice.inject.InjectFragment;
+
+public class MainActivity extends RoboFragmentActivity {
+
+ @Inject protected EventManager eventManager;
-public class MainActivity extends FragmentActivity {
-
- private VehiclesListFragment vlf;
- private VehicleDamageWaiverFragment vdf;
-
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
-
- // Get the fragments so we can work with them.
- vlf = (VehiclesListFragment)getSupportFragmentManager().findFragmentById(R.id.vehicles);
- vdf = (VehicleDamageWaiverFragment)getSupportFragmentManager().findFragmentById(R.id.damage_waiver);
-
- // Set the waiver as the listener.
- vlf.setVechicleListener(vdf);
}
@Override
@@ -37,7 +33,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
Toast.makeText(this, getString(R.string.about_content), Toast.LENGTH_LONG).show();
return true;
case R.id.reset:
- vlf.reset();
+ eventManager.fire(new ResetEvent());
return true;
default:
return super.onOptionsItemSelected(item);
View
11 src/com/agilevent/repairshop/RepairshopModule.java
@@ -0,0 +1,11 @@
+package com.agilevent.repairshop;
+
+import com.google.inject.AbstractModule;
+
+public class RepairShopModule extends AbstractModule {
+ @Override
+ protected void configure() {
+ // Configure RoboGuice here if you need to.
+ // Add bindings, etc.
+ }
+}
View
4 src/com/agilevent/repairshop/ResetEvent.java
@@ -0,0 +1,4 @@
+package com.agilevent.repairshop;
+
+public class ResetEvent {
+}
View
18 src/com/agilevent/repairshop/VehicleDamageWaiverFragment.java
@@ -15,10 +15,16 @@
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
+import com.google.inject.Inject;
+import roboguice.event.EventListener;
+import roboguice.event.EventManager;
+import roboguice.fragment.RoboFragment;
-public class VehicleDamageWaiverFragment extends Fragment implements VehicleSelected {
+public class VehicleDamageWaiverFragment extends RoboFragment {
- private SignatureView waiverView;
+ protected SignatureView waiverView;
+
+ @Inject protected EventManager eventManager;
@Override
public void onCreate(Bundle savedInstanceState) {
@@ -29,7 +35,6 @@ public void onCreate(Bundle savedInstanceState) {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
-
waiverView = (SignatureView)getView().findViewById(R.id.vehicle);
waiverView.setDrawingCacheEnabled(true);
@@ -66,6 +71,13 @@ public void onClick(DialogInterface dialog, int which) {
}
}
});
+
+ eventManager.registerObserver(VehicleSelectedEvent.class, new EventListener<VehicleSelectedEvent>() {
+ @Override
+ public void onEvent(VehicleSelectedEvent event) {
+ OnVehicleSelected(event.getVehicle());
+ }
+ });
}
@Override
View
13 src/com/agilevent/repairshop/VehicleSelectedEvent.java
@@ -0,0 +1,13 @@
+package com.agilevent.repairshop;
+
+public class VehicleSelectedEvent {
+ protected String vehicle;
+
+ public VehicleSelectedEvent(String vehicle) {
+ this.vehicle = vehicle;
+ }
+
+ public String getVehicle() {
+ return vehicle;
+ }
+}
View
26 src/com/agilevent/repairshop/VehiclesListFragment.java
@@ -7,31 +7,36 @@
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
+import com.google.inject.Inject;
+import roboguice.event.EventListener;
+import roboguice.event.EventManager;
+import roboguice.fragment.RoboListFragment;
-public class VehiclesListFragment extends ListFragment {
+public class VehiclesListFragment extends RoboListFragment {
private static final String CAR = "Car";
private static final String SUV = "SUV";
private static final String TRUCK = "Truck";
- private VehicleSelected vehicleSelected;
+
+ @Inject protected EventManager eventManager;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initializeAdapter();
-
+
}
-
+
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
-
+
// Get the vehicle that they selected (in this case its a string)
String vehicle = l.getItemAtPosition(position).toString();
- // Notify the listener (the damage waiver)
- vehicleSelected.OnVehicleSelected(vehicle);
+ // Notify the event bus.
+ eventManager.fire(new VehicleSelectedEvent(vehicle));
}
private void initializeAdapter() {
@@ -44,13 +49,8 @@ private void initializeAdapter() {
public void reset() {
initializeAdapter();
- vehicleSelected.OnVehicleSelected("");
+ eventManager.fire(new VehicleSelectedEvent(""));
Toast.makeText(getActivity(), getString(R.string.reset), Toast.LENGTH_SHORT).show();
}
- public void setVechicleListener(VehicleSelected vehicleSelected) {
- this.vehicleSelected = vehicleSelected;
- }
-
-
}
Please sign in to comment.
Something went wrong with that request. Please try again.