com.mercandalli.android.apps.files.admin.ServerLogsFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.mercandalli.android.apps.files.admin.ServerLogsFragment.java

Source

/**
 * This file is part of FileSpace for Android, an app for managing your server (files, talks...).
 * <p>
 * Copyright (c) 2014-2015 FileSpace for Android contributors (http://mercandalli.com)
 * <p>
 * LICENSE:
 * <p>
 * FileSpace for Android is free software: you can redistribute it and/or modify it under the terms of the GNU General
 * Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any
 * later version.
 * <p>
 * FileSpace for Android 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 General Public License for more
 * details.
 *
 * @author Jonathan Mercandalli
 * @license http://www.gnu.org/licenses/gpl.html
 * @copyright 2014-2015 FileSpace for Android contributors (http://mercandalli.com)
 */
package com.mercandalli.android.apps.files.admin;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.mercandalli.android.apps.files.R;
import com.mercandalli.android.apps.files.common.fragment.BackFragment;
import com.mercandalli.android.apps.files.common.listener.IPostExecuteListener;
import com.mercandalli.android.apps.files.common.net.TaskGet;
import com.mercandalli.android.apps.files.main.Config;
import com.mercandalli.android.apps.files.main.Constants;
import com.mercandalli.android.apps.files.main.network.NetUtils;
import com.mercandalli.android.apps.files.user.AdapterModelUserConnection;
import com.mercandalli.android.apps.files.user.UserConnectionModel;

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

import java.util.ArrayList;
import java.util.List;

public class ServerLogsFragment extends BackFragment {

    private RecyclerView recyclerView;
    private AdapterModelUserConnection mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    private List<UserConnectionModel> list;
    private ProgressBar circularProgressBar;
    private TextView message;
    private SwipeRefreshLayout swipeRefreshLayout;
    private View circle;

    int mNewItemPosition;

    public static ServerLogsFragment newInstance() {
        return new ServerLogsFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.fragment_admin_data, container, false);
        circularProgressBar = (ProgressBar) rootView.findViewById(R.id.circularProgressBar);
        this.message = (TextView) rootView.findViewById(R.id.message);

        circle = rootView.findViewById(R.id.circle);

        recyclerView = (RecyclerView) rootView.findViewById(R.id.my_recycler_view);
        recyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(mLayoutManager);

        rootView.findViewById(R.id.circle).setVisibility(View.GONE);

        swipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipeRefreshLayout);
        swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light, android.R.color.holo_orange_light,
                android.R.color.holo_red_light);

        swipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshList();
            }
        });

        refreshList();

        return rootView;
    }

    public void refreshList() {
        if (NetUtils.isInternetConnection(getContext())) {
            new TaskGet(getActivity(), Constants.URL_DOMAIN + Config.ROUTE_USER_CONNECTION,
                    new IPostExecuteListener() {
                        @Override
                        public void onPostExecute(JSONObject json, String body) {
                            list = new ArrayList<>();

                            try {
                                if (json != null) {
                                    if (json.has("result_count_all")) {
                                        list.add(new UserConnectionModel(
                                                "Server Logs (" + json.getInt("result_count_all") + ")",
                                                Constants.TAB_VIEW_TYPE_SECTION));
                                    } else {
                                        list.add(new UserConnectionModel("Server Logs",
                                                Constants.TAB_VIEW_TYPE_SECTION));
                                    }

                                    if (json.has("result")) {
                                        JSONArray array = json.getJSONArray("result");
                                        int array_length = array.length();
                                        for (int i = 0; i < array_length; i++) {
                                            list.add(new UserConnectionModel(array.getJSONObject(i)));
                                        }
                                    }

                                } else {
                                    Toast.makeText(getContext(), R.string.action_failed, Toast.LENGTH_SHORT).show();
                                }
                            } catch (JSONException e) {
                                Log.e(getClass().getName(), "Failed to convert Json", e);
                            }
                            updateAdapter();
                        }
                    }, null).execute();
        } else {
            this.circularProgressBar.setVisibility(View.GONE);
            if (isAdded()) {
                this.message.setText(Config.isLogged() ? getString(R.string.no_internet_connection)
                        : getString(R.string.no_logged));
            }
            this.message.setVisibility(View.VISIBLE);
            this.swipeRefreshLayout.setRefreshing(false);
        }
    }

    public void updateAdapter() {
        if (this.recyclerView != null && this.list != null && this.isAdded()) {
            this.circularProgressBar.setVisibility(View.GONE);

            this.mAdapter = new AdapterModelUserConnection(list);
            this.recyclerView.setAdapter(mAdapter);
            this.recyclerView
                    .setItemAnimator(/*new SlideInFromLeftItemAnimator(mRecyclerView)*/new DefaultItemAnimator());

            if (circle.getVisibility() == View.GONE) {
                circle.setVisibility(View.VISIBLE);
                Animation animOpen = AnimationUtils.loadAnimation(getContext(), R.anim.circle_button_bottom_open);
                circle.startAnimation(animOpen);
            }

            circle.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    mAdapter.addItem(new UserConnectionModel("Number", "" + mNewItemPosition), 0);
                    recyclerView.scrollToPosition(0);
                    mNewItemPosition++;
                }
            });

            this.mAdapter.setOnItemClickListener(new AdapterModelUserConnection.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {

                }
            });

            this.swipeRefreshLayout.setRefreshing(false);
            mNewItemPosition = 0;
        }
    }

    @Override
    public boolean back() {
        return false;
    }
}