Skip to content

Commit

Permalink
removed EventBus where appropriate, other general cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
commonsguy committed Jan 7, 2017
1 parent 77be9c2 commit 069c1d0
Show file tree
Hide file tree
Showing 51 changed files with 474 additions and 514 deletions.
9 changes: 4 additions & 5 deletions DataBinding/Basic/app/build.gradle
@@ -1,18 +1,17 @@
apply plugin: 'com.android.application'

dependencies {
compile 'de.greenrobot:eventbus:2.4.0'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.squareup.retrofit:retrofit:1.9.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
}

android {
compileSdkVersion 24
buildToolsVersion "24.0.0"
compileSdkVersion 25
buildToolsVersion "25.0.0"

defaultConfig {
minSdkVersion 15
targetSdkVersion 23
targetSdkVersion 25
versionCode 1
versionName "1.0"
}
Expand Down
Expand Up @@ -18,22 +18,13 @@
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.StrictMode;
import de.greenrobot.event.EventBus;

public class MainActivity extends Activity {
public class MainActivity extends Activity
implements QuestionsFragment.Contract {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

/*
StrictMode.VmPolicy.Builder b=new StrictMode.VmPolicy.Builder();
b.detectCleartextNetwork().penaltyDeathOnCleartextNetwork();
StrictMode.setVmPolicy(b.build());
*/

if (getFragmentManager().findFragmentById(android.R.id.content) == null) {
getFragmentManager().beginTransaction()
.add(android.R.id.content,
Expand All @@ -42,19 +33,8 @@ protected void onCreate(Bundle savedInstanceState) {
}

@Override
public void onResume() {
super.onResume();
EventBus.getDefault().register(this);
}

@Override
public void onPause() {
EventBus.getDefault().unregister(this);
super.onPause();
}

public void onEventMainThread(QuestionClickedEvent event) {
public void onQuestion(Item question) {
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse(event.item.link)));
Uri.parse(question.link)));
}
}

This file was deleted.

Expand Up @@ -28,14 +28,18 @@
import com.commonsware.android.databind.basic.databinding.RowBinding;
import com.squareup.picasso.Picasso;
import java.util.List;
import de.greenrobot.event.EventBus;
import retrofit.Callback;
import retrofit.RestAdapter;
import retrofit.RetrofitError;
import retrofit.client.Response;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class QuestionsFragment extends ListFragment implements
Callback<SOQuestions> {
interface Contract {
void onQuestion(Item question);
}

@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Expand All @@ -45,13 +49,15 @@ public View onCreateView(LayoutInflater inflater,

setRetainInstance(true);

RestAdapter restAdapter=
new RestAdapter.Builder().setEndpoint("https://api.stackexchange.com")
.build();
Retrofit retrofit=
new Retrofit.Builder()
.baseUrl("https://api.stackexchange.com")
.addConverterFactory(GsonConverterFactory.create())
.build();
StackOverflowInterface so=
restAdapter.create(StackOverflowInterface.class);
retrofit.create(StackOverflowInterface.class);

so.questions("android", this);
so.questions("android").enqueue(this);

return(result);
}
Expand All @@ -60,20 +66,21 @@ public View onCreateView(LayoutInflater inflater,
public void onListItemClick(ListView l, View v, int position, long id) {
Item item=((ItemsAdapter)getListAdapter()).getItem(position);

EventBus.getDefault().post(new QuestionClickedEvent(item));
((Contract)getActivity()).onQuestion(item);
}

@Override
public void failure(RetrofitError exception) {
Toast.makeText(getActivity(), exception.getMessage(),
Toast.LENGTH_LONG).show();
Log.e(getClass().getSimpleName(),
"Exception from Retrofit request to StackOverflow", exception);
public void onResponse(Call<SOQuestions> call,
Response<SOQuestions> response) {
setListAdapter(new ItemsAdapter(response.body().items));
}

@Override
public void success(SOQuestions questions, Response response) {
setListAdapter(new ItemsAdapter(questions.items));
public void onFailure(Call<SOQuestions> call, Throwable t) {
Toast.makeText(getActivity(), t.getMessage(),
Toast.LENGTH_LONG).show();
Log.e(getClass().getSimpleName(),
"Exception from Retrofit request to StackOverflow", t);
}

class ItemsAdapter extends ArrayAdapter<Item> {
Expand Down
Expand Up @@ -14,11 +14,12 @@

package com.commonsware.android.databind.basic;

import retrofit.Callback;
import retrofit.http.GET;
import retrofit.http.Query;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.http.GET;
import retrofit2.http.Query;

public interface StackOverflowInterface {
@GET("/2.1/questions?order=desc&sort=creation&site=stackoverflow")
void questions(@Query("tagged") String tags, Callback<SOQuestions> cb);
Call<SOQuestions> questions(@Query("tagged") String tags);
}
2 changes: 1 addition & 1 deletion DataBinding/Basic/build.gradle
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
classpath 'com.android.tools.build:gradle:2.2.3'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
13 changes: 7 additions & 6 deletions DataBinding/Chained/app/build.gradle
@@ -1,16 +1,17 @@
apply plugin: 'com.android.application'

dependencies {
compile 'de.greenrobot:eventbus:2.4.0'
compile 'org.greenrobot:eventbus:3.0.0'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.squareup.retrofit:retrofit:1.9.0'
compile 'com.android.support:recyclerview-v7:24.1.0'
compile 'com.android.support:cardview-v7:24.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.android.support:recyclerview-v7:25.1.0'
compile 'com.android.support:cardview-v7:25.1.0'
compile 'com.android.support:support-v4:25.1.0'
}

android {
compileSdkVersion 24
buildToolsVersion "24.0.0"
compileSdkVersion 25
buildToolsVersion "25.0.0"

defaultConfig {
minSdkVersion 15
Expand Down
Expand Up @@ -18,8 +18,9 @@
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.StrictMode;
import de.greenrobot.event.EventBus;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class MainActivity extends Activity {
@Override
Expand All @@ -34,18 +35,19 @@ protected void onCreate(Bundle savedInstanceState) {
}

@Override
public void onResume() {
super.onResume();
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}

@Override
public void onPause() {
public void onStop() {
EventBus.getDefault().unregister(this);
super.onPause();
super.onStop();
}

public void onEventMainThread(QuestionClickedEvent event) {
@Subscribe(threadMode =ThreadMode.MAIN)
public void onQuestionClicked(QuestionClickedEvent event) {
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse(event.question.link)));
}
Expand Down
Expand Up @@ -21,7 +21,7 @@
import android.view.MotionEvent;
import android.view.View;
import com.commonsware.android.databind.basic.databinding.RowBinding;
import de.greenrobot.event.EventBus;
import org.greenrobot.eventbus.EventBus;

public class QuestionController extends RecyclerView.ViewHolder
implements View.OnTouchListener {
Expand Down
Expand Up @@ -32,21 +32,22 @@
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import java.util.HashMap;
import retrofit.Callback;
import retrofit.RestAdapter;
import retrofit.RetrofitError;
import retrofit.client.Response;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class QuestionsFragment extends RecyclerViewFragment {
private ArrayList<Question> questions
=new ArrayList<Question>();
private HashMap<String, Question> questionMap=
new HashMap<String, Question>();
RestAdapter restAdapter=
new RestAdapter.Builder().setEndpoint("https://api.stackexchange.com")
private ArrayList<Question> questions=new ArrayList<Question>();
private HashMap<String, Question> questionMap=new HashMap<String, Question>();
Retrofit retrofit=
new Retrofit.Builder()
.baseUrl("https://api.stackexchange.com")
.addConverterFactory(GsonConverterFactory.create())
.build();
StackOverflowInterface so=
restAdapter.create(StackOverflowInterface.class);
retrofit.create(StackOverflowInterface.class);

@BindingAdapter({"app:imageUrl", "app:placeholder", "app:error"})
public static void bindImageView(ImageView iv,
Expand Down Expand Up @@ -77,11 +78,11 @@ public void onViewCreated(View view,

setLayoutManager(new LinearLayoutManager(getActivity()));

so.questions("android", new Callback<SOQuestions>() {
so.questions("android").enqueue(new Callback<SOQuestions>() {
@Override
public void success(SOQuestions results,
Response response) {
for (Item item : results.items) {
public void onResponse(Call<SOQuestions> call,
Response<SOQuestions> response) {
for (Item item : response.body().items) {
Question question=new Question(item);

questions.add(question);
Expand All @@ -92,8 +93,8 @@ public void success(SOQuestions results,
}

@Override
public void failure(RetrofitError error) {
onError(error);
public void onFailure(Call<SOQuestions> call, Throwable t) {
onError(t);
}
});
}
Expand Down Expand Up @@ -122,11 +123,11 @@ private void updateQuestions() {

String ids=TextUtils.join(";", idList);

so.update(ids, new Callback<SOQuestions>() {
so.update(ids).enqueue(new Callback<SOQuestions>() {
@Override
public void success(SOQuestions soQuestions,
Response response) {
for (Item item : soQuestions.items) {
public void onResponse(Call<SOQuestions> call,
Response<SOQuestions> response) {
for (Item item : response.body().items) {
Question question=questionMap.get(item.id);

if (question!=null) {
Expand All @@ -136,13 +137,13 @@ public void success(SOQuestions soQuestions,
}

@Override
public void failure(RetrofitError error) {
onError(error);
public void onFailure(Call<SOQuestions> call, Throwable t) {
onError(t);
}
});
}

private void onError(RetrofitError error) {
private void onError(Throwable error) {
Toast.makeText(getActivity(), error.getMessage(),
Toast.LENGTH_LONG).show();

Expand Down
Expand Up @@ -14,15 +14,15 @@

package com.commonsware.android.databind.basic;

import retrofit.Callback;
import retrofit.http.GET;
import retrofit.http.Path;
import retrofit.http.Query;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Path;
import retrofit2.http.Query;

public interface StackOverflowInterface {
@GET("/2.1/questions?order=desc&sort=creation&site=stackoverflow")
void questions(@Query("tagged") String tags, Callback<SOQuestions> cb);
Call<SOQuestions> questions(@Query("tagged") String tags);

@GET("/2.1/questions/{ids}?site=stackoverflow")
void update(@Path("ids") String questionIds, Callback<SOQuestions> cb);
Call<SOQuestions> update(@Path("ids") String tags);
}
2 changes: 1 addition & 1 deletion DataBinding/Chained/build.gradle
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
classpath 'com.android.tools.build:gradle:2.2.3'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down

0 comments on commit 069c1d0

Please sign in to comment.