[Nemu-Bengkel] Added Option to sort list by rating and by category

in #utopian-io5 years ago (edited)

Repository

https://github.com/sogatanco/nemu-bengkel

About Nemu-Bengkel

NEBENG (Nemu-Bengkel) is is an android application to find the nearest car repair place and motorcycle repair place. When your vehicle is damaged in transit, you don't know where it is closest to repairing it. Nebeng is the solution. Currently this application is designed for the Android operating system and will be developed for iOS operating systems.

What feature(s) did you add?

  • Option to sort by category
    There are 2 category in this system, car repair place and motorcycle repair place. I have added options menu to sort list by each category

  • Sort list by rating.
    each list has a certain rating given by the user according to user experience. here I add the option to sort data based on rating

How did you implement it/them?

All this features develop using Android Studio 3.3.2

  • Option to sort by category
    To sort list by car repair place, I send request to : /api/general/bengkel?token=1234567&kategori=1. While to sort by motorcycle repair place, I send request to /api/general/bengkel?token=1234567&kategori=2.
    here the full code to sort by motorcycle repair place :
 private void getBengkelMotor() {
        final String URL = getString(R.string.base_url) + "/api/general/bengkel?token=1234567&kategori=1";
        JsonObjectRequest getBengkel = new JsonObjectRequest(Request.Method.GET, URL, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            bengkels.clear();
                            JSONArray array = response.getJSONArray("data");
//                            Log.d("rs",array.toString());
                            ArrayList<JSONObject> rr= new ArrayList<JSONObject>();
                            for (int i = 0; i < array.length(); i++) {
                                try {
                                    rr.add(array.getJSONObject(i));
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
//                            bubble sort algorithm
                            int n=rr.size();
                            for(int a=0; a < n; a++){
                                for(int b=1; b< (n -a); b++){
                                    if(CalculationByDistance(loc, new LatLng(rr.get(b-1).getDouble("bk_lat"),rr.get(b-1).getDouble("bk_long"))) >CalculationByDistance(loc, new LatLng(rr.get(b).getDouble("bk_lat"),rr.get(b).getDouble("bk_long")))){
                                        Collections.swap(rr,b-1,b);
                                    }
                                }
                            }
// add data to adapter
                            for(int m=0; m< rr.size(); m++){
                                JSONObject data=rr.get(m);
                                LatLng lokasi=new LatLng(data.getDouble("bk_lat"),data.getDouble("bk_long"));
                                double jarak=CalculationByDistance(loc, lokasi);
                                Bengkel bengkel = new Bengkel();
                                bengkel.setNamaBengkel(data.getString("bk_namabengkel"));
                                bengkel.setRating(data.getString("total_rating"));
                                bengkel.setUrlImage(getString(R.string.base_url) + "asset/images/" + fungsi.isImgBengkelNull(data.getString("bk_foto")));
                                bengkel.setApproved(data.getInt("bk_approved"));
                                bengkel.setKategori(data.getInt("bk_kategori"));
                                bengkel.setIdbengkel(data.getInt("bk_id"));
                                bengkel.setUlasan(data.getInt("j_ulasan"));
                                bengkel.setJarak("Jarak : "+new DecimalFormat("##.##").format(jarak)+" KM");
                                bengkels.add(bengkel);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        listAdapter.notifyDataSetChanged();
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("eror", error.toString());
            }
        }){
            @Override
            protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
                try {
                    Cache.Entry cacheEntry = HttpHeaderParser.parseCacheHeaders(response);
                    if (cacheEntry == null) {
                        cacheEntry = new Cache.Entry();
                    }
                    final long cacheHitButRefreshed = 1 * 1000; // in 3 minutes cache will be hit, but also refreshed on background
                    final long cacheExpired = 24 * 60 * 60 * 1000; // in 24 hours this cache entry expires completely
                    long now = System.currentTimeMillis();
                    final long softExpire = now + cacheHitButRefreshed;
                    final long ttl = now + cacheExpired;
                    cacheEntry.data = response.data;
                    cacheEntry.softTtl = softExpire;
                    cacheEntry.ttl = ttl;
                    String headerValue;
                    headerValue = response.headers.get("Date");
                    if (headerValue != null) {
                        cacheEntry.serverDate = HttpHeaderParser.parseDateAsEpoch(headerValue);
                    }
                    headerValue = response.headers.get("Last-Modified");
                    if (headerValue != null) {
                        cacheEntry.lastModified = HttpHeaderParser.parseDateAsEpoch(headerValue);
                    }
                    cacheEntry.responseHeaders = response.headers;
                    final String jsonString = new String(response.data,
                            HttpHeaderParser.parseCharset(response.headers));
                    return Response.success(new JSONObject(jsonString), cacheEntry);
                }catch (UnsupportedEncodingException |JSONException e){
                    return Response.error(new ParseError(e));
                }
            }
            @Override
            protected void deliverResponse(JSONObject response) {
                super.deliverResponse(response);
            }
            @Override
            public void deliverError(VolleyError error) {
                super.deliverError(error);
            }
            @Override
            protected VolleyError parseNetworkError(VolleyError volleyError) {
                return super.parseNetworkError(volleyError);
            }
        };
        requestQueue.add(getBengkel);
    }

full code to sort by car repair place :

private void getBengkelMobil() {
        final String URL = getString(R.string.base_url) + "/api/general/bengkel?token=1234567&kategori=2";
        JsonObjectRequest getBengkel = new JsonObjectRequest(Request.Method.GET, URL, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            bengkels.clear();
                            JSONArray array = response.getJSONArray("data");
//                            Log.d("rs",array.toString());
                            ArrayList<JSONObject> rr= new ArrayList<JSONObject>();
                            for (int i = 0; i < array.length(); i++) {
                                try {
                                    rr.add(array.getJSONObject(i));
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
//                            bubble sort algorithm
                            int n=rr.size();
                            for(int a=0; a < n; a++){
                                for(int b=1; b< (n -a); b++){
                                    if(CalculationByDistance(loc, new LatLng(rr.get(b-1).getDouble("bk_lat"),rr.get(b-1).getDouble("bk_long"))) >CalculationByDistance(loc, new LatLng(rr.get(b).getDouble("bk_lat"),rr.get(b).getDouble("bk_long")))){
                                        Collections.swap(rr,b-1,b);
                                    }
                                }
                            }
// add data to adapter
                            for(int m=0; m< rr.size(); m++){
                                JSONObject data=rr.get(m);
                                LatLng lokasi=new LatLng(data.getDouble("bk_lat"),data.getDouble("bk_long"));
                                double jarak=CalculationByDistance(loc, lokasi);
                                Bengkel bengkel = new Bengkel();
                                bengkel.setNamaBengkel(data.getString("bk_namabengkel"));
                                bengkel.setRating(data.getString("total_rating"));
                                bengkel.setUrlImage(getString(R.string.base_url) + "asset/images/" + fungsi.isImgBengkelNull(data.getString("bk_foto")));
                                bengkel.setApproved(data.getInt("bk_approved"));
                                bengkel.setKategori(data.getInt("bk_kategori"));
                                bengkel.setIdbengkel(data.getInt("bk_id"));
                                bengkel.setUlasan(data.getInt("j_ulasan"));
                                bengkel.setJarak("Jarak : "+new DecimalFormat("##.##").format(jarak)+" KM");
                                bengkels.add(bengkel);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        listAdapter.notifyDataSetChanged();
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("eror", error.toString());
            }
        }){
            @Override
            protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
                try {
                    Cache.Entry cacheEntry = HttpHeaderParser.parseCacheHeaders(response);
                    if (cacheEntry == null) {
                        cacheEntry = new Cache.Entry();
                    }
                    final long cacheHitButRefreshed = 1 * 1000; // in 3 minutes cache will be hit, but also refreshed on background
                    final long cacheExpired = 24 * 60 * 60 * 1000; // in 24 hours this cache entry expires completely
                    long now = System.currentTimeMillis();
                    final long softExpire = now + cacheHitButRefreshed;
                    final long ttl = now + cacheExpired;
                    cacheEntry.data = response.data;
                    cacheEntry.softTtl = softExpire;
                    cacheEntry.ttl = ttl;
                    String headerValue;
                    headerValue = response.headers.get("Date");
                    if (headerValue != null) {
                        cacheEntry.serverDate = HttpHeaderParser.parseDateAsEpoch(headerValue);
                    }
                    headerValue = response.headers.get("Last-Modified");
                    if (headerValue != null) {
                        cacheEntry.lastModified = HttpHeaderParser.parseDateAsEpoch(headerValue);
                    }
                    cacheEntry.responseHeaders = response.headers;
                    final String jsonString = new String(response.data,
                            HttpHeaderParser.parseCharset(response.headers));
                    return Response.success(new JSONObject(jsonString), cacheEntry);
                }catch (UnsupportedEncodingException |JSONException e){
                    return Response.error(new ParseError(e));
                }
            }
            @Override
            protected void deliverResponse(JSONObject response) {
                super.deliverResponse(response);
            }
            @Override
            public void deliverError(VolleyError error) {
                super.deliverError(error);
            }
            @Override
            protected VolleyError parseNetworkError(VolleyError volleyError) {
                return super.parseNetworkError(volleyError);
            }
        };
        requestQueue.add(getBengkel);
    }
  • Sort list by rating.
    to get an average number of ratings, the number of ratings divided by the number of users who gave the rating. then I do the sorting with the highest number of ratings at the top
 JSONArray array = response.getJSONArray("data");
//                            Log.d("rs",array.toString());
                            ArrayList<JSONObject> rr= new ArrayList<JSONObject>();
                            for (int i = 0; i < array.length(); i++) {
                                try {
                                    rr.add(array.getJSONObject(i));
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
//                            bubble sort algorithm
                            int n=rr.size();
                            for(int a=0; a < n; a++){
                                for(int b=1; b< (n -a); b++){
                                    float jrating;
                                    if( rr.get(b-1).getString("total_rating").equals("null")){
                                        jrating=0;
                                    }else{
                                        jrating=Float.valueOf(rr.get(b-1).getString("total_rating")) / rr.get(b-1).getInt("j_ulasan");
                                    }
                                    float jrating2;
                                    if( rr.get(b).getString("total_rating").equals("null")){
                                        jrating2=0;
                                    }else{
                                        jrating2=Float.valueOf(rr.get(b).getString("total_rating")) / rr.get(b).getInt("j_ulasan");
                                    }
                                    if(jrating<jrating2){
                                        Collections.swap(rr,b-1,b);
                                    }
                                }
                            }

Commits on Github

https://github.com/sogatanco/nemu-bengkel/compare/master@%7B08-09-19%7D...master

Github Account

https://github.com/sogatanco