com.liferay.alerts.callback.FetchCommentsCallback.java Source code

Java tutorial

Introduction

Here is the source code for com.liferay.alerts.callback.FetchCommentsCallback.java

Source

/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.liferay.alerts.callback;

import android.content.Context;

import com.liferay.alerts.activity.CommentsActivity;
import com.liferay.alerts.model.Alert;
import com.liferay.alerts.model.User;
import com.liferay.alerts.util.ToastUtil;
import com.liferay.mobile.android.task.callback.typed.JSONArrayAsyncTaskCallback;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

/**
 * @author Bruno Farache
 */
public class FetchCommentsCallback extends JSONArrayAsyncTaskCallback {

    public FetchCommentsCallback(Context context) {
        _context = context.getApplicationContext();
    }

    @Override
    public void onFailure(Exception exception) {
        ToastUtil.show(_context, exception.getMessage());
    }

    @Override
    public void onSuccess(JSONArray result) {
        try {
            ArrayList<Alert> alerts = new ArrayList<>();

            for (int i = 0; i < result.length(); i++) {
                JSONObject jsonObject = result.getJSONObject(i);

                long id = jsonObject.getLong(Alert.PUSH_NOTIFICATIONS_ENTRY_ID);

                JSONObject userJSONObject = jsonObject.getJSONObject(Alert.USER);

                User user = new User(userJSONObject);

                JSONObject payload = new JSONObject(jsonObject.getString(Alert.PAYLOAD));

                alerts.add(new Alert(id, user, payload));
            }

            CommentsActivity.updateCommentsList(_context, alerts);
        } catch (Exception e) {
            ToastUtil.show(_context, e.getMessage());
        }
    }

    private Context _context;

}