From a417ce1f7d14a377d8a4e0719947cd68328d5918 Mon Sep 17 00:00:00 2001 From: Moez Bhatti Date: Sun, 6 Dec 2015 13:27:09 -0500 Subject: [PATCH] Pull the changes from firebase and sort them --- .../com/moez/QKSMS/common/DialogHelper.java | 40 ++++++++++++++++++- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/QKSMS/src/main/java/com/moez/QKSMS/common/DialogHelper.java b/QKSMS/src/main/java/com/moez/QKSMS/common/DialogHelper.java index c36f1e1b..65867cec 100644 --- a/QKSMS/src/main/java/com/moez/QKSMS/common/DialogHelper.java +++ b/QKSMS/src/main/java/com/moez/QKSMS/common/DialogHelper.java @@ -2,6 +2,8 @@ package com.moez.QKSMS.common; import android.util.Log; import com.android.volley.toolbox.StringRequest; +import com.google.gson.Gson; +import com.google.gson.annotations.SerializedName; import com.moez.QKSMS.R; import com.moez.QKSMS.data.Conversation; import com.moez.QKSMS.transaction.SmsHelper; @@ -10,7 +12,11 @@ import com.moez.QKSMS.ui.base.QKActivity; import com.moez.QKSMS.ui.dialog.DefaultSmsHelper; import com.moez.QKSMS.ui.dialog.QKDialog; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Arrays; +import java.util.Comparator; import java.util.HashSet; import java.util.Set; @@ -68,10 +74,27 @@ public class DialogHelper { String url = "https://qksms-changelog.firebaseio.com/changes.json"; StringRequest request = new StringRequest(url, response -> { - Log.d(TAG, "onComplete: " + response); + Gson gson = new Gson(); + Change[] changes = gson.fromJson(response, Change[].class); + + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); + SimpleDateFormat formatWithRevision = new SimpleDateFormat("yyyy-MM-dd-'r'H"); // For multiple updates in a day + for (Change change : changes) { + try { + if (change.date.length() > 11) { + change.dateLong = formatWithRevision.parse(change.date).getTime(); + } else { + change.dateLong = format.parse(change.date).getTime(); + } + } catch (ParseException e) { + e.printStackTrace(); + } + } + + Arrays.sort(changes, (lhs, rhs) -> Long.valueOf(rhs.dateLong).compareTo(lhs.dateLong)); + context.hideProgressDialog(); }, error -> { - Log.d(TAG, "onError: " + error.getMessage()); context.hideProgressDialog(); context.makeToast(R.string.toast_changelog_error); }); @@ -79,4 +102,17 @@ public class DialogHelper { context.getRequestQueue().add(request); } + private class Change implements Comparator { + @SerializedName("changes") ArrayList changes; + @SerializedName("release_date") String date; + @SerializedName("version_name") String version; + + long dateLong = 0; + + @Override + public int compare(Long lhs, Long rhs) { + return lhs.compareTo(rhs); + } + } + }