Skip to content

Commit

Permalink
Refactoring and add a callback for EndpointsAsyncTask
Browse files Browse the repository at this point in the history
  • Loading branch information
Frédéric Letellier committed Sep 1, 2016
1 parent 0482541 commit c266103
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 22 deletions.
Original file line number Original file line Diff line number Diff line change
@@ -1,19 +1,23 @@
package com.udacity.gradle.builditbigger; package com.udacity.gradle.builditbigger;


import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.Button;


import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView; import com.google.android.gms.ads.AdView;
import com.joke.JokeActivity;




/** /**
* A placeholder fragment containing a simple view. * A placeholder fragment containing a simple view.
*/ */
public class MainActivityFragment extends Fragment { public class MainActivityFragment extends Fragment implements OnTaskCompleted{
Button mJokeButton;


public MainActivityFragment() { public MainActivityFragment() {
} }
Expand All @@ -23,6 +27,8 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_main, container, false); View root = inflater.inflate(R.layout.fragment_main, container, false);


mJokeButton = (Button) root.findViewById(R.id.btn_joke);

AdView mAdView = (AdView) root.findViewById(R.id.adView); AdView mAdView = (AdView) root.findViewById(R.id.adView);
// Create an ad request. Check logcat output for the hashed device ID to // Create an ad request. Check logcat output for the hashed device ID to
// get test ads on a physical device. e.g. // get test ads on a physical device. e.g.
Expand All @@ -31,6 +37,27 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR) .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build(); .build();
mAdView.loadAd(adRequest); mAdView.loadAd(adRequest);

mJokeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadData();
}
});

return root; return root;
} }

public void loadData() {
EndpointsAsyncTask endpointsAsyncTask = new EndpointsAsyncTask(this);
endpointsAsyncTask.execute();
}

@Override
public void onTaskCompleted(String result) {
Intent intent = new Intent(getActivity(), JokeActivity.class);

intent.putExtra(JokeActivity.JOKE_KEY, result);
startActivity(intent);
}
} }
2 changes: 1 addition & 1 deletion app/src/free/res/layout/fragment_main.xml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
android:text="@string/instructions" /> android:text="@string/instructions" />


<Button <Button
android:id="@+id/btn_joke"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_below="@+id/instructions_text_view" android:layout_below="@+id/instructions_text_view"
android:onClick="launchJokeActivity"
android:text="@string/button_text" android:text="@string/button_text"


/> />
Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -1,12 +1,9 @@
package com.udacity.gradle.builditbigger; package com.udacity.gradle.builditbigger;


import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask; import android.os.AsyncTask;


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;
import com.joke.JokeActivity;
import com.joke.endpoint.backend.myApi.MyApi; import com.joke.endpoint.backend.myApi.MyApi;


import java.io.IOException; import java.io.IOException;
Expand All @@ -17,10 +14,10 @@
*/ */
public class EndpointsAsyncTask extends AsyncTask<Void, Void, String> { public class EndpointsAsyncTask extends AsyncTask<Void, Void, String> {
private static MyApi myApiService = null; private static MyApi myApiService = null;
private final Context mContext; private OnTaskCompleted mListener;


public EndpointsAsyncTask (Context context){ public EndpointsAsyncTask (OnTaskCompleted listener) {
mContext = context; this.mListener = listener;
} }


@Override @Override
Expand All @@ -42,9 +39,6 @@ protected String doInBackground(Void... params){
@Override @Override
protected void onPostExecute(String result) { protected void onPostExecute(String result) {


Intent intent = new Intent(mContext, JokeActivity.class); mListener.onTaskCompleted(result);

intent.putExtra(JokeActivity.JOKE_KEY, result);
mContext.startActivity(intent);
} }
} }
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View;




public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
Expand Down Expand Up @@ -38,10 +37,4 @@ public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} }


public void launchJokeActivity(View view) {
EndpointsAsyncTask endpointsAsyncTask = new EndpointsAsyncTask(this);
endpointsAsyncTask.execute();
}


} }
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.udacity.gradle.builditbigger;

/**
* udacity-builditbigger
* Created on 01/09/2016 by Espace de travail.
*/
public interface OnTaskCompleted {
void onTaskCompleted(String result);
}
Original file line number Original file line Diff line number Diff line change
@@ -1,17 +1,21 @@
package com.udacity.gradle.builditbigger; package com.udacity.gradle.builditbigger;


import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.Button;


import com.joke.JokeActivity;




/** /**
* A placeholder fragment containing a simple view. * A placeholder fragment containing a simple view.
*/ */
public class MainActivityFragment extends Fragment { public class MainActivityFragment extends Fragment implements OnTaskCompleted{
Button mJokeButton;


public MainActivityFragment() { public MainActivityFragment() {
} }
Expand All @@ -20,6 +24,30 @@ public MainActivityFragment() {
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {


return inflater.inflate(R.layout.fragment_main, container, false); View root = inflater.inflate(R.layout.fragment_main, container, false);

mJokeButton = (Button) root.findViewById(R.id.btn_joke);

mJokeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadData();
}
});

return root;
}

public void loadData() {
EndpointsAsyncTask endpointsAsyncTask = new EndpointsAsyncTask(this);
endpointsAsyncTask.execute();
}

@Override
public void onTaskCompleted(String result) {
Intent intent = new Intent(getActivity(), JokeActivity.class);

intent.putExtra(JokeActivity.JOKE_KEY, result);
startActivity(intent);
} }
} }
2 changes: 1 addition & 1 deletion app/src/paid/res/layout/fragment_main.xml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
android:text="@string/instructions" /> android:text="@string/instructions" />


<Button <Button
android:id="@+id/btn_joke"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_below="@+id/instructions_text_view" android:layout_below="@+id/instructions_text_view"
android:onClick="launchJokeActivity"
android:text="@string/button_text" android:text="@string/button_text"


/> />
Expand Down

0 comments on commit c266103

Please sign in to comment.