Skip to content

Commit

Permalink
Implement SwipeRefreshLayout in Quiz
Browse files Browse the repository at this point in the history
  • Loading branch information
debugger24 committed Sep 4, 2017
1 parent 17dc442 commit 4e95644
Show file tree
Hide file tree
Showing 3 changed files with 568 additions and 538 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.TextViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
Expand Down Expand Up @@ -49,6 +50,7 @@ public class Quiz extends AppCompatActivity {
Button btnSubmit, btnRefresh;
View viewQuestions, viewNoQuiz, viewSuccess;
Toolbar toolbar;
private SwipeRefreshLayout swipeContainer;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -63,6 +65,27 @@ protected void onCreate(Bundle savedInstanceState) {
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

swipeContainer = (SwipeRefreshLayout) findViewById(R.id.swipeContainer);
swipeContainer.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
// Your code to refresh the list here.
// Make sure you call swipeContainer.setRefreshing(false)
// once the network request has completed successfully.
if (isNetworkAvailable()) {
refresh();
} else {
Toast.makeText(getApplicationContext(), "No Internet Connection", Toast.LENGTH_SHORT).show();
swipeContainer.setRefreshing(false);
}
}
});
// Configure the refreshing colors
swipeContainer.setColorSchemeResources(android.R.color.holo_blue_bright,
android.R.color.holo_green_light,
android.R.color.holo_orange_light,
android.R.color.holo_red_light);

txtQuestions[0] = (TextView) findViewById(R.id.txtQuestion0);
txtQuestions[1] = (TextView) findViewById(R.id.txtQuestion1);
txtQuestions[2] = (TextView) findViewById(R.id.txtQuestion2);
Expand Down Expand Up @@ -313,13 +336,15 @@ public void onClick(View view) {
default:
answersArray[9] = "";
}
swipeContainer.setRefreshing(true);
submitAnswer();
}
});

btnRefresh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
swipeContainer.setRefreshing(true);
refresh();
}
});
Expand Down Expand Up @@ -348,22 +373,26 @@ private void submitAnswer() {

@Override
public void onResponse(String response) {
try {
Log.v("SUB JSON RESPONSE", response);
JSONObject jsonObject = new JSONObject(response);
if (jsonObject.getString("status").equals("success")) {
Toast.makeText(getApplicationContext(), "SUCCESSFULLY SUBMITTED. Result will be announced soon", Toast.LENGTH_LONG).show();

// Hide Questions
resetViews();
viewSuccess.setVisibility(View.VISIBLE);

} else {
Toast.makeText(getApplicationContext(), "FAILED TO SUBMIT. Try Again", Toast.LENGTH_LONG).show();
if (getApplicationContext() != null) {
try {
Log.v("SUB JSON RESPONSE", response);
JSONObject jsonObject = new JSONObject(response);
if (jsonObject.getString("status").equals("success")) {
Toast.makeText(getApplicationContext(), "SUCCESSFULLY SUBMITTED. Result will be announced soon", Toast.LENGTH_LONG).show();

// Hide Questions
resetViews();
viewSuccess.setVisibility(View.VISIBLE);

} else {
Toast.makeText(getApplicationContext(), "FAILED TO SUBMIT. Try Again", Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Something went wrong. Try Again", Toast.LENGTH_LONG).show();
} finally {
swipeContainer.setRefreshing(false);
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Something went wrong. Try Again", Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
Expand All @@ -373,6 +402,7 @@ public void onErrorResponse(VolleyError volleyError) {
if (getApplicationContext() != null) {
Toast.makeText(getApplicationContext(), "FAILED TO SUBMIT. Try Again", Toast.LENGTH_LONG).show();
}
swipeContainer.setRefreshing(false);
}
}) {
@Override
Expand Down Expand Up @@ -412,21 +442,23 @@ public void onResponse(JSONObject response) {
VolleyLog.d("QUIZ RESPONSE", "Response: " + response.toString());
if (response != null) {
checkStatus(response);
swipeContainer.setRefreshing(false);
}
}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("QUIZ ERROR", "Error: " + error.getMessage());
// swipeContainer.setRefreshing(false);
swipeContainer.setRefreshing(false);
}
});

// Adding request to volley request queue
AppController.getInstance().addToRequestQueue(jsonReq);
} else {
Toast.makeText(getApplicationContext(), "No Internet Connection", Toast.LENGTH_SHORT).show();
swipeContainer.setRefreshing(false);
}

}
Expand Down
16 changes: 4 additions & 12 deletions PhaseShift2017/app/src/main/res/layout/activity_admin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,14 @@
android:id="@+id/tshirtLastUpdate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Last Update : 1 Sep 6:32 PM" />
android:layout_alignParentBottom="true" />

<TextView
android:id="@+id/tshirtCounter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="72"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Large" />

</RelativeLayout>
Expand All @@ -78,16 +76,14 @@
android:id="@+id/appLastUpdate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Last Update : 1 Sep 6:32 PM" />
android:layout_alignParentBottom="true" />

<TextView
android:id="@+id/appCounter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="645"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Large" />

</RelativeLayout>
Expand All @@ -111,16 +107,14 @@
android:id="@+id/registrationLastUpdate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Last Update : 1 Sep 6:32 PM" />
android:layout_alignParentBottom="true" />

<TextView
android:id="@+id/registrationCounter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="8765"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Large" />

</RelativeLayout>
Expand All @@ -144,16 +138,14 @@
android:id="@+id/eventsLastUpdate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Last Update : 1 Sep 6:32 PM" />
android:layout_alignParentBottom="true" />

<TextView
android:id="@+id/eventsCounter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="76"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Large" />

</RelativeLayout>
Expand Down
Loading

0 comments on commit 4e95644

Please sign in to comment.