epmobile.com.android.myapplication.view.OrderListFragment.java Source code

Java tutorial

Introduction

Here is the source code for epmobile.com.android.myapplication.view.OrderListFragment.java

Source

/*
 * Copyright (C) 2013 Andreas Stuetz <andreas.stuetz@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package epmobile.com.android.myapplication.view;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.support.v4.widget.SwipeRefreshLayout;
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.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ProgressBar;

import java.util.List;

import epmobile.com.android.myapplication.R;
import epmobile.com.android.myapplication.activity.OrderDetailActivity;
import epmobile.com.android.myapplication.activity.OrderListsActivity;
import epmobile.com.android.myapplication.adapter.OrderAdapter;
import epmobile.com.android.myapplication.control.Application;
import epmobile.com.android.myapplication.loader.AppListLoader;
import epmobile.com.android.myapplication.model.Order;
import epmobile.com.android.myapplication.utils.BundleConstants;
import epmobile.com.android.myapplication.utils.StringUtils;

public class OrderListFragment extends Fragment implements LoaderManager.LoaderCallbacks<List<Order>> {

    public static final String ORDER_STATUS = "order_status";
    private static final String LOG_TAG = Application.APP_NAME + StringUtils.DASH
            + OrderListFragment.class.getSimpleName();

    private int position;

    private Activity activity;
    private RecyclerView recyclerView;
    RecyclerView.LayoutManager mLayoutManager;
    private SwipeRefreshLayout swipeLayoutOrders;
    private SwipeRefreshLayout swipeLayoutData0;
    private int LOADER_ID = 1;
    OrderAdapter dAdapter;
    ProgressBar progressBar;

    private int deliveryStatus;
    private listReceiver receiver;
    private boolean firstTimeShowing;

    public static OrderListFragment newInstance(int orderStatus) {
        OrderListFragment f = new OrderListFragment();
        Bundle b = new Bundle();
        b.putInt(ORDER_STATUS, orderStatus);
        f.setArguments(b);
        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        firstTimeShowing = true;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        activity = getActivity();
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT);
        params.gravity = Gravity.CENTER;
        FrameLayout fl = new FrameLayout(getActivity());
        fl.setLayoutParams(params);
        View rootView = inflater.inflate(R.layout.view_order_list, container, false);
        Bundle args = getArguments();
        deliveryStatus = args.getInt(ORDER_STATUS);
        switch (deliveryStatus) {
        case Order.IN_PROGRESS:
            LOADER_ID = Order.IN_PROGRESS;
            break;
        case Order.CANCELED:
            LOADER_ID = Order.CANCELED;
            break;
        case Order.COMPLETED:
            LOADER_ID = Order.COMPLETED;
            break;
        }

        progressBar = (ProgressBar) rootView.findViewById(R.id.progressBar);
        recyclerView = (RecyclerView) rootView.findViewById(R.id.listOrders);
        recyclerView.addItemDecoration(new DividerItemDecorator(getActivity(), DividerItemDecorator.VERTICAL_LIST));
        recyclerView.setHasFixedSize(true);

        swipeLayoutOrders = (SwipeRefreshLayout) rootView.findViewById(R.id.swipeLayoutOrders);
        swipeLayoutData0 = (SwipeRefreshLayout) rootView.findViewById(R.id.swipeLayoutData0);

        SwipeRefreshLayout.OnRefreshListener listener = new SwipeRefreshLayout.OnRefreshListener() {

            @Override
            public void onRefresh() {
                refreshList();
            }
        };

        swipeLayoutOrders.setOnRefreshListener(listener);
        swipeLayoutData0.setOnRefreshListener(listener);

        dAdapter = new OrderAdapter(null, R.layout.row_order_list);
        recyclerView.setAdapter(dAdapter);
        mLayoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.addOnItemTouchListener(
                new RecyclerItemClickListener(getActivity(), new RecyclerItemClickListener.OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        Order order = dAdapter.getItemAtPosition(position);
                        Intent intent = new Intent(getActivity(), OrderDetailActivity.class);
                        intent.putExtra(BundleConstants.ORDER, order);
                        startActivity(intent);
                    }
                }));
        getLoaderManager().initLoader(LOADER_ID, null, this);
        return rootView;
    }

    private void refreshList() {
        swipeLayoutOrders.setRefreshing(true);
        swipeLayoutData0.setRefreshing(true);
        Application.sendBroadcast(getActivity().getApplicationContext(), Application.Broadcast.ACTION_ORDER_SYNC);
    }

    @Override
    public Loader<List<Order>> onCreateLoader(int id, Bundle args) {
        if (Application.DEBUG)
            Log.i(LOG_TAG, "+++ onCreateLoader() called! +++");
        return new AppListLoader(getActivity().getApplicationContext(), deliveryStatus);
    }

    @Override
    public void onLoadFinished(Loader<List<Order>> loader, List<Order> data) {
        if (Application.DEBUG)
            Log.i(LOG_TAG, "+++ onLoadFinished() called! +++");
        dAdapter.setOrderList(data);
        progressBar.setVisibility(View.GONE);
        if (data == null || data.size() == 0) {
            swipeLayoutOrders.setVisibility(View.GONE);
            swipeLayoutData0.setVisibility(View.VISIBLE);
            if (data != null && firstTimeShowing) {
                refreshList();
                firstTimeShowing = false;
            }

        } else {
            swipeLayoutOrders.setVisibility(View.VISIBLE);
            swipeLayoutData0.setVisibility(View.GONE);
        }

        recyclerView.setVisibility(View.VISIBLE);
        swipeLayoutOrders.setRefreshing(false);
        swipeLayoutData0.setRefreshing(false);
    }

    @Override
    public void onLoaderReset(Loader<List<Order>> loader) {
        if (Application.DEBUG)
            Log.i(LOG_TAG, "+++ onLoadReset() called! +++");
        dAdapter.setOrderList(null);
    }

    @Override
    public void onResume() {
        super.onResume();
        receiver = new listReceiver();
        getActivity().registerReceiver(receiver, new IntentFilter(Application.Broadcast.ACTION_ORDER_SYNC_ERROR));
        getActivity().registerReceiver(receiver,
                new IntentFilter(Application.Broadcast.ACTION_ORDER_SYNC_NEW_IN_PROGRESS));
        getActivity().registerReceiver(receiver,
                new IntentFilter(Application.Broadcast.ACTION_ORDER_SYNC_NEW_CANCELLED));
        getActivity().registerReceiver(receiver,
                new IntentFilter(Application.Broadcast.ACTION_ORDER_SYNC_NEW_COMPLETED));
    }

    @Override
    public void onPause() {
        super.onPause();
        getActivity().unregisterReceiver(receiver);
    }

    private void showToastNewOrders(int orderStatusBroadCast) {
        if (((OrderListsActivity) activity).getCurrentTabSelected() != LOADER_ID) {
            return;
        }
        int stringResource;
        switch (orderStatusBroadCast) {
        case Order.IN_PROGRESS:
            stringResource = R.string.new_orders_in_progress;
            break;
        case Order.CANCELED:
            stringResource = R.string.new_orders_cancelled;
            break;
        case Order.COMPLETED:
            stringResource = R.string.new_orders_completed;
            break;
        default:
            stringResource = R.string.new_orders_completed;
        }
        StringUtils.showMessage(getActivity(), stringResource);
    }

    private class listReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(Application.Broadcast.ACTION_ORDER_SYNC_NEW_CANCELLED)) {
                showToastNewOrders(Order.CANCELED);
            } else if (intent.getAction().equals(Application.Broadcast.ACTION_ORDER_SYNC_NEW_IN_PROGRESS)) {
                showToastNewOrders(Order.IN_PROGRESS);
            } else if (intent.getAction().equals(Application.Broadcast.ACTION_ORDER_SYNC_NEW_COMPLETED)) {
                showToastNewOrders(Order.COMPLETED);
            } else {
                progressBar.setVisibility(View.GONE);
                if (dAdapter.getItemCount() == 0) {
                    swipeLayoutOrders.setVisibility(View.GONE);
                    swipeLayoutData0.setVisibility(View.VISIBLE);
                } else {
                    swipeLayoutOrders.setVisibility(View.VISIBLE);
                    swipeLayoutData0.setVisibility(View.GONE);
                    recyclerView.setVisibility(View.VISIBLE);
                }
                swipeLayoutOrders.setRefreshing(false);
                swipeLayoutData0.setRefreshing(false);
                StringUtils.showMessage(activity, R.string.error_syncing_orders);

            }
        }
    }

}