Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RoboSpice example #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ gen
*.iml
*.ipr
*.iws

*.settings
.classpath

6 changes: 6 additions & 0 deletions cleanandroidcode/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
android:minSdkVersion="8"
android:targetSdkVersion="17"
tools:ignore="OldTargetApi" />

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>

<application
android:name=".HelloApplication"
Expand All @@ -22,6 +25,9 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<service android:name="com.octo.android.robospice.retrofit.RetrofitGsonSpiceService" android:exported="false"/>
</application>


</manifest>
8 changes: 8 additions & 0 deletions cleanandroidcode/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@
<groupId>com.squareup</groupId>
<artifactId>dagger-compiler</artifactId>
</dependency>
<dependency>
<groupId>com.squareup.retrofit</groupId>
<artifactId>retrofit</artifactId>
</dependency>
<dependency>
<groupId>com.octo.android.robospice</groupId>
<artifactId>robospice-retrofit</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
5 changes: 5 additions & 0 deletions cleanandroidcode/res/layout/hello_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@
android:layout_height="wrap_content"
class="info.piwai.cleanandroidcode.HelloFragment_" />

<fragment
android:id="@+id/hello_github_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
class="info.piwai.cleanandroidcode.GitHubFragment_" />
</LinearLayout>
17 changes: 17 additions & 0 deletions cleanandroidcode/res/layout/hello_github_fragment.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:orientation="vertical" >

<TextView
android:id="@+id/my_label"
android:layout_width="match_parent"
android:freezesText="true"
android:textColor="@android:color/black"
android:textAppearance="@android:style/TextAppearance.Medium"
android:layout_height="match_parent"
android:text="@string/fragment_text" />

</LinearLayout>
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package info.piwai.cleanandroidcode;

import info.piwai.cleanandroidcode.base.BaseFragment;
import info.piwai.cleanandroidcode.network.GitHubRetrofitSpiceRequest;
import info.piwai.cleanandroidcode.network.ListContributor;

import javax.inject.Inject;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.googlecode.androidannotations.annotations.EFragment;
import com.googlecode.androidannotations.annotations.ViewById;
import com.octo.android.robospice.persistence.DurationInMillis;
import com.octo.android.robospice.persistence.exception.SpiceException;
import com.octo.android.robospice.request.listener.RequestListener;
import com.squareup.otto.Bus;
import com.squareup.otto.Subscribe;

@EFragment(R.layout.hello_github_fragment)
public class GitHubFragment extends BaseFragment {

@Inject
Bus bus;

@ViewById(R.id.my_label)
TextView mylabel;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bus.register(this);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
spiceManager.addListenerIfPending(ListContributor.class, "square", new GitHubRequestListenerRequestListener());
return super.onCreateView(inflater, container, savedInstanceState);
}

@Subscribe
public void onUpdateTitle(UpdateTitleEvent event) {
spiceManager.execute(new GitHubRetrofitSpiceRequest(), "square", DurationInMillis.ALWAYS_RETURNED, new GitHubRequestListenerRequestListener());
mylabel.setText("Requesting contributors on github...");
}

private final class GitHubRequestListenerRequestListener implements RequestListener<ListContributor> {
@Override
public void onRequestFailure(SpiceException spiceException) {
Toast.makeText(getActivity(), "failure", Toast.LENGTH_SHORT).show();
}

@Override
public void onRequestSuccess(ListContributor result) {
mylabel.setText("Square contributors: " + result.size());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
@EActivity(R.layout.hello_activity)
public class HelloAndroidActivity extends BaseActivity {

@Subscribe
public void onUpdateTitle(UpdateTitleEvent event) {
setTitle(event.title);
}
@Subscribe
public void onUpdateTitle(UpdateTitleEvent event) {
setTitle(event.title);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
@EFragment(R.layout.hello_fragment)
public class HelloFragment extends BaseFragment {

@Inject
Bus bus;
@Inject
Bus bus;

@Click
void fragmentButtonClicked() {
bus.post(new UpdateTitleEvent("Button clicked at " + System.currentTimeMillis()));
}
@Click
void fragmentButtonClicked() {
bus.post(new UpdateTitleEvent("Button clicked at " + System.currentTimeMillis()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,29 @@
import android.os.Bundle;
import android.support.v4.app.Fragment;

import com.octo.android.robospice.SpiceManager;
import com.octo.android.robospice.retrofit.RetrofitGsonSpiceService;

public abstract class BaseFragment extends Fragment {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GraphRetriever.from(getActivity()).inject(this);
}
protected SpiceManager spiceManager = new SpiceManager(RetrofitGsonSpiceService.class);

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GraphRetriever.from(getActivity()).inject(this);
}

@Override
public void onStart() {
super.onStart();
spiceManager.start(getActivity());
}

@Override
public void onStop() {
spiceManager.shouldStop();
super.onStop();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package info.piwai.cleanandroidcode.network;

class Contributor {
String login;
int contributions;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package info.piwai.cleanandroidcode.network;

import retrofit.http.GET;
import retrofit.http.Name;

interface GitHub {
@GET("/repos/{owner}/{repo}/contributors")
ListContributor contributors(@Name("owner") String owner, @Name("repo") String repo);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package info.piwai.cleanandroidcode.network;

import retrofit.http.RestAdapter;
import roboguice.util.temp.Ln;

import com.octo.android.robospice.request.retrofit.RetrofitSpiceRequest;

public class GitHubRetrofitSpiceRequest extends RetrofitSpiceRequest<ListContributor> {

private static final String BASE_URL = "https://api.github.com";

public GitHubRetrofitSpiceRequest() {
super(ListContributor.class);
}

@Override
public ListContributor loadDataFromNetwork() {
Ln.d("Call web service " + BASE_URL);

// Create a very simple REST adapter which points the GitHub API endpoint.
RestAdapter restAdapter = new RestAdapter.Builder().setServer(BASE_URL).build();

// Create an instance of our GitHub API interface.
GitHub github = restAdapter.create(GitHub.class);

return github.contributors("square", "retrofit");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package info.piwai.cleanandroidcode.network;

import java.util.ArrayList;

/**
* Necessary for RoboSpice. All types included in results of request's type tree must be real Java
* types. As Java doesn't accept expression like <tt>List<Contributor>.class</tt>, we must create a
* full java type as a workaround.
* @author SNI
*/
public class ListContributor extends ArrayList<Contributor> {

private static final long serialVersionUID = -2518157580598657864L;

}
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
<otto.version>2.0.0-SNAPSHOT</otto.version>
<dagger.version>0.9.1</dagger.version>
<androidannotations.version>2.7</androidannotations.version>
<retrofit.version>1.0.0-SNAPSHOT</retrofit.version>
<robospice-retrofit.version>1.4.1-SNAPSHOT</robospice-retrofit.version>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -89,6 +91,16 @@
<artifactId>androidannotations-api</artifactId>
<version>${androidannotations.version}</version>
</dependency>
<dependency>
<groupId>com.squareup.retrofit</groupId>
<artifactId>retrofit</artifactId>
<version>${retrofit.version}</version>
</dependency>
<dependency>
<groupId>com.octo.android.robospice</groupId>
<artifactId>robospice-retrofit</artifactId>
<version>${robospice-retrofit.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down