package com.example.aquatricity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.RetryPolicy;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
public class Leaderboard extends AppCompatActivity {
ProgressDialog loading;
ListView listview;
ListAdapter adapter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_leaderboard);
listview = (ListView) findViewById(R.id.lv_items);
getUsers();
}
private void getUsers() {
loading = ProgressDialog.show(this,"Loading","please wait",false,true);
StringRequest stringRequest = new StringRequest(Request.Method.GET, "https://script.google.com/macros/s/AKfycbzvfbIcBrm6UXpK3xphHor2azlk1dZxar28_6z8PFI7xDyNq4xnQ-m4Gs80A3Y8ZspT_Q/exec?action=getUsers",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
parseItems(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}
);
int socketTimeOut = 50000;
RetryPolicy policy = new DefaultRetryPolicy(socketTimeOut, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
stringRequest.setRetryPolicy(policy);
RequestQueue queue = Volley.newRequestQueue(this);
queue.add(stringRequest);
}
private void parseItems(String jsonResposnce) {
ArrayList<HashMap<String, String>> list = new ArrayList<>();
try {
JSONObject jobj = new JSONObject(jsonResposnce);
JSONArray jarray = jobj.getJSONArray("Users");
HashMap<String, String> user = new HashMap<>();
for (int i = 0; i < jarray.length(); i++) {
JSONObject jo = jarray.getJSONObject(i);
String displayName = jo.getString("displayName");
String email = jo.getString("emailAddress");
String pass = jo.getString("password");
String phoneNumber = jo.getString("phoneNumber");
String score = jo.getString("score");
String rank = jo.getString("rank");
String house = jo.getString("household");
user.put("displayName", displayName);
user.put("rank", rank);
user.put("score",score);
list.add(user);
}
} catch (JSONException e) {
e.printStackTrace();
}
adapter = new SimpleAdapter(this,list,R.layout.leaderboard_item,
new String[]{"displayName","rank","score"},new int[]{R.id.username,R.id.rank,R.id.score});
loading.dismiss();
}
}