Search in sources :

Example 1 with ChangeModel

use of com.moez.QKSMS.model.ChangeModel in project qksms by moezbhatti.

the class DialogHelper method showChangelog.

public static void showChangelog(QKActivity context) {
    context.showProgressDialog();
    String url = "https://qksms-changelog.firebaseio.com/changes.json";
    StringRequest request = new StringRequest(url, response -> {
        Gson gson = new Gson();
        ChangeModel[] changes = gson.fromJson(response, ChangeModel[].class);
        SimpleDateFormat dateParser = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat dateRevisionParser = new SimpleDateFormat("yyyy-MM-dd-'r'H");
        SimpleDateFormat dateFormatter = new SimpleDateFormat("MMMM d, yyyy");
        for (ChangeModel change : changes) {
            try {
                Date date;
                if (change.getDate().length() > 11) {
                    date = dateRevisionParser.parse(change.getDate());
                } else {
                    date = dateParser.parse(change.getDate());
                }
                change.setDate(dateFormatter.format(date));
                change.setDateLong(date.getTime());
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        Arrays.sort(changes, (lhs, rhs) -> Long.valueOf(rhs.getDateLong()).compareTo(lhs.getDateLong()));
        boolean currentVersionReached = false;
        ArrayList<String> versions = new ArrayList<>();
        ArrayList<String> dates = new ArrayList<>();
        ArrayList<String> changelists = new ArrayList<>();
        for (ChangeModel change : changes) {
            if (change.getVersion().equals(BuildConfig.VERSION_NAME)) {
                currentVersionReached = true;
            }
            if (currentVersionReached) {
                versions.add(change.getVersion());
                dates.add(change.getDate());
                String changelist = "";
                for (int i = 0; i < change.getChanges().size(); i++) {
                    String changeItem = change.getChanges().get(i);
                    changelist += " • ";
                    changelist += changeItem;
                    if (i < change.getChanges().size() - 1) {
                        changelist += "\n";
                    }
                }
                changelists.add(changelist);
            }
        }
        context.hideProgressDialog();
        new QKDialog().setContext(context).setTitle(R.string.title_changelog).setTripleLineItems(versions.toArray(new String[versions.size()]), dates.toArray(new String[versions.size()]), changelists.toArray(new String[versions.size()]), null).show();
    }, error -> {
        context.hideProgressDialog();
        context.makeToast(R.string.toast_changelog_error);
    });
    context.getRequestQueue().add(request);
}
Also used : StringRequest(com.android.volley.toolbox.StringRequest) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) Date(java.util.Date) QKDialog(com.moez.QKSMS.ui.dialog.QKDialog) ChangeModel(com.moez.QKSMS.model.ChangeModel) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

StringRequest (com.android.volley.toolbox.StringRequest)1 Gson (com.google.gson.Gson)1 ChangeModel (com.moez.QKSMS.model.ChangeModel)1 QKDialog (com.moez.QKSMS.ui.dialog.QKDialog)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1