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

Java tutorial

Introduction

Here is the source code for com.mercandalli.android.apps.files.admin.ServerDataFragment.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.content.Context;
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.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.common.util.StringPair;
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 org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

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

public class ServerDataFragment extends BackFragment {

    private static final String TAG = "ServerDataFragment";

    private RecyclerView recyclerView;
    private AdapterModelInformation mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    List<ModelInformation> list;
    private ProgressBar circularProgressBar;
    private SwipeRefreshLayout swipeRefreshLayout;
    private View circle;

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

    @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);
        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() {
        List<StringPair> parameters = null;
        if (NetUtils.isInternetConnection(getContext())) {
            new TaskGet(getActivity(), Constants.URL_DOMAIN + Config.ROUTE_INFORMATION, new IPostExecuteListener() {
                @Override
                public void onPostExecute(JSONObject json, String body) {
                    list = new ArrayList<>();
                    list.add(new ModelInformation("Server Data", Constants.TAB_VIEW_TYPE_SECTION));
                    try {
                        if (json != null) {
                            if (json.has("result")) {
                                JSONArray array = json.getJSONArray("result");
                                for (int i = 0; i < array.length(); i++) {
                                    ModelInformation modelFile = new ModelInformation(array.getJSONObject(i));
                                    list.add(modelFile);
                                }
                            }
                        } else {
                            final Context context = getContext();
                            if (context != null) {
                                Toast.makeText(context, R.string.action_failed, Toast.LENGTH_SHORT).show();
                            }
                        }
                    } catch (JSONException e) {
                        Log.e(TAG, "ServerDataFragment: failed to convert Json", e);
                    }
                    updateAdapter();
                }
            }, parameters).execute();
        }
    }

    int i;

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

            this.mAdapter = new AdapterModelInformation(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 ModelInformation("Number", "" + i), 0);
                    recyclerView.scrollToPosition(0);
                    i++;
                }
            });

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

                }
            });

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

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