eventbus
This commit is contained in:
@@ -34,4 +34,5 @@ dependencies {
|
|||||||
freeCompile 'com.google.android.gms:play-services-ads:8.3.0'
|
freeCompile 'com.google.android.gms:play-services-ads:8.3.0'
|
||||||
compile project(path: ':jokesbackend', configuration: 'android-endpoints')
|
compile project(path: ':jokesbackend', configuration: 'android-endpoints')
|
||||||
compile 'com.android.support:design:23.1.0'
|
compile 'com.android.support:design:23.1.0'
|
||||||
|
compile 'de.greenrobot:eventbus:2.4.0'
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,29 +2,32 @@ package com.udacity.gradle.builditbigger;
|
|||||||
|
|
||||||
import android.test.AndroidTestCase;
|
import android.test.AndroidTestCase;
|
||||||
|
|
||||||
|
import com.google.common.eventbus.Subscribe;
|
||||||
|
|
||||||
import net.headlezz.jokesbackend.myApi.model.Joke;
|
import net.headlezz.jokesbackend.myApi.model.Joke;
|
||||||
|
|
||||||
public class JokeLoaderTaskTest extends AndroidTestCase implements JokeLoaderTask.JokeLoaderCallback {
|
public class JokeLoaderTaskTest extends AndroidTestCase {
|
||||||
|
|
||||||
SyncronizeTalker talker;
|
SyncronizeTalker talker;
|
||||||
|
|
||||||
public void testJokeLoader() {
|
public void testJokeLoader() {
|
||||||
JokeLoaderTask task = new JokeLoaderTask(this);
|
JokeLoaderTask task = new JokeLoaderTask();
|
||||||
talker = new SyncronizeTalker();
|
talker = new SyncronizeTalker();
|
||||||
|
MyBus.getInstance().register(this);
|
||||||
task.execute();
|
task.execute();
|
||||||
talker.doWait();
|
talker.doWait();
|
||||||
|
MyBus.getInstance().unregister(this);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Subscribe
|
||||||
public void onJokeLoaded(Joke joke) {
|
public void onJokeLoaded(Joke joke) {
|
||||||
assertNotNull(joke);
|
assertNotNull(joke);
|
||||||
assertTrue(joke.getJoke() != null && !joke.getJoke().isEmpty());
|
assertTrue(joke.getJoke() != null && !joke.getJoke().isEmpty());
|
||||||
talker.doNotify();
|
talker.doNotify();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Subscribe
|
||||||
public void onError() {
|
public void onError() {
|
||||||
assertTrue(false);
|
assertTrue(false);
|
||||||
talker.doNotify();
|
talker.doNotify();
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package com.udacity.gradle.builditbigger;
|
package com.udacity.gradle.builditbigger;
|
||||||
|
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
import android.support.annotation.NonNull;
|
import android.util.Log;
|
||||||
|
|
||||||
import com.google.api.client.extensions.android.http.AndroidHttp;
|
import com.google.api.client.extensions.android.http.AndroidHttp;
|
||||||
import com.google.api.client.extensions.android.json.AndroidJsonFactory;
|
import com.google.api.client.extensions.android.json.AndroidJsonFactory;
|
||||||
@@ -15,18 +15,11 @@ import java.io.IOException;
|
|||||||
|
|
||||||
public class JokeLoaderTask extends AsyncTask<Void, Void, Joke> {
|
public class JokeLoaderTask extends AsyncTask<Void, Void, Joke> {
|
||||||
|
|
||||||
|
public static final String TAG = JokeLoaderTask.class.getSimpleName();
|
||||||
static final String ROOT_URL = "http://10.0.3.2:8080/_ah/api/";
|
static final String ROOT_URL = "http://10.0.3.2:8080/_ah/api/";
|
||||||
MyApi mApi;
|
MyApi mApi;
|
||||||
|
|
||||||
JokeLoaderCallback mCallback;
|
public JokeLoaderTask() {
|
||||||
|
|
||||||
interface JokeLoaderCallback {
|
|
||||||
void onJokeLoaded(Joke joke);
|
|
||||||
void onError();
|
|
||||||
}
|
|
||||||
|
|
||||||
public JokeLoaderTask(@NonNull JokeLoaderCallback cb) {
|
|
||||||
mCallback = cb;
|
|
||||||
mApi = new MyApi.Builder(AndroidHttp.newCompatibleTransport(), new AndroidJsonFactory(), null)
|
mApi = new MyApi.Builder(AndroidHttp.newCompatibleTransport(), new AndroidJsonFactory(), null)
|
||||||
.setRootUrl(ROOT_URL)
|
.setRootUrl(ROOT_URL)
|
||||||
.setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {
|
.setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {
|
||||||
@@ -39,6 +32,7 @@ public class JokeLoaderTask extends AsyncTask<Void, Void, Joke> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Joke doInBackground(Void... params) {
|
protected Joke doInBackground(Void... params) {
|
||||||
|
Log.d(TAG, "download started");
|
||||||
try {
|
try {
|
||||||
return mApi.tellJoke().execute();
|
return mApi.tellJoke().execute();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@@ -48,13 +42,20 @@ public class JokeLoaderTask extends AsyncTask<Void, Void, Joke> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPostExecute(Joke joke) {
|
protected void onPostExecute(Joke joke) {
|
||||||
if(isCancelled())
|
if(isCancelled()) {
|
||||||
|
Log.d(TAG, "download cancelled");
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if(joke != null) {
|
if(joke != null) {
|
||||||
mCallback.onJokeLoaded(joke);
|
MyBus.getInstance().post(joke);
|
||||||
|
Log.d(TAG, "download finished");
|
||||||
} else {
|
} else {
|
||||||
mCallback.onError();
|
MyBus.getInstance().post(new JokeDownloadException());
|
||||||
|
Log.d(TAG, "download finished with error");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static class JokeDownloadException extends Exception {}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
setContentView(R.layout.activity_main);
|
setContentView(R.layout.activity_main);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCreateOptionsMenu(Menu menu) {
|
public boolean onCreateOptionsMenu(Menu menu) {
|
||||||
// Inflate the menu; this adds items to the action bar if it is present.
|
// Inflate the menu; this adds items to the action bar if it is present.
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.udacity.gradle.builditbigger;
|
package com.udacity.gradle.builditbigger;
|
||||||
|
|
||||||
|
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.support.design.widget.Snackbar;
|
import android.support.design.widget.Snackbar;
|
||||||
@@ -11,15 +12,17 @@ import android.view.ViewGroup;
|
|||||||
import net.headlezz.androidjokepresenter.JokePresenterActivity;
|
import net.headlezz.androidjokepresenter.JokePresenterActivity;
|
||||||
import net.headlezz.jokesbackend.myApi.model.Joke;
|
import net.headlezz.jokesbackend.myApi.model.Joke;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A placeholder fragment containing a simple view.
|
* A placeholder fragment containing a simple view.
|
||||||
*/
|
*/
|
||||||
public abstract class MainActivityFragment extends Fragment implements View.OnClickListener, JokeLoaderTask.JokeLoaderCallback {
|
public abstract class MainActivityFragment extends Fragment implements View.OnClickListener {
|
||||||
|
|
||||||
JokeLoaderTask mJokeLoaderTask;
|
JokeLoaderTask mJokeLoaderTask;
|
||||||
|
|
||||||
public MainActivityFragment() {
|
@Override
|
||||||
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
|
super.onCreate(savedInstanceState);
|
||||||
|
MyBus.getInstance().register(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -27,6 +30,7 @@ public abstract class MainActivityFragment extends Fragment implements View.OnCl
|
|||||||
Bundle savedInstanceState) {
|
Bundle savedInstanceState) {
|
||||||
View root = inflater.inflate(R.layout.fragment_main, container, false);
|
View root = inflater.inflate(R.layout.fragment_main, container, false);
|
||||||
root.findViewById(R.id.btShowJoke).setOnClickListener(this);
|
root.findViewById(R.id.btShowJoke).setOnClickListener(this);
|
||||||
|
|
||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,30 +39,29 @@ public abstract class MainActivityFragment extends Fragment implements View.OnCl
|
|||||||
downloadNewJoke();
|
downloadNewJoke();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* starts the async task to download and show a new joke
|
||||||
|
* cancels old downloads if theres already a running one
|
||||||
|
*/
|
||||||
private void downloadNewJoke() {
|
private void downloadNewJoke() {
|
||||||
mJokeLoaderTask = new JokeLoaderTask(this);
|
if(mJokeLoaderTask != null) {
|
||||||
|
mJokeLoaderTask.cancel(true);
|
||||||
|
}
|
||||||
|
mJokeLoaderTask = new JokeLoaderTask();
|
||||||
mJokeLoaderTask.execute();
|
mJokeLoaderTask.execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public void onEvent(Joke joke) {
|
||||||
public void onJokeLoaded(Joke joke) {
|
|
||||||
Intent i = new Intent(getContext(), JokePresenterActivity.class);
|
Intent i = new Intent(getContext(), JokePresenterActivity.class);
|
||||||
i.putExtra(JokePresenterActivity.BUNDLE_ARG_JOKE, joke.getJoke());
|
i.putExtra(JokePresenterActivity.BUNDLE_ARG_JOKE, joke.getJoke());
|
||||||
startActivity(i);
|
startActivity(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public void onEvent(JokeLoaderTask.JokeDownloadException e) {
|
||||||
public void onError() {
|
if (getActivity() != null)
|
||||||
if (getView() != null)
|
Snackbar.make(getActivity().findViewById(android.R.id.content), R.string.joke_download_error, Snackbar.LENGTH_LONG)
|
||||||
Snackbar.make(getView(), R.string.joke_download_error, Snackbar.LENGTH_LONG)
|
|
||||||
.setAction(R.string.retry, this)
|
.setAction(R.string.retry, this)
|
||||||
.show();
|
.show();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onStop() {
|
|
||||||
if(mJokeLoaderTask != null && !mJokeLoaderTask.isCancelled())
|
|
||||||
mJokeLoaderTask.cancel(true);
|
|
||||||
super.onStop();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package com.udacity.gradle.builditbigger;
|
||||||
|
|
||||||
|
import de.greenrobot.event.EventBus;
|
||||||
|
|
||||||
|
public class MyBus {
|
||||||
|
|
||||||
|
private static EventBus bus = new EventBus();
|
||||||
|
|
||||||
|
public static EventBus getInstance() {
|
||||||
|
return bus;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user