fr.eoit.activity.fragment.blueprint.RequiredItemsBlueprintCopyInventionFragment.java Source code

Java tutorial

Introduction

Here is the source code for fr.eoit.activity.fragment.blueprint.RequiredItemsBlueprintCopyInventionFragment.java

Source

/**
 * Copyright (C) 2012 Picon software
 *
 * This program 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.
 *
 * This program 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.
    
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package fr.eoit.activity.fragment.blueprint;

import android.content.ContentUris;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;
import fr.eoit.EOITConst;
import fr.eoit.R;
import fr.eoit.activity.fragment.ItemMaterialListFragment;
import fr.eoit.activity.util.ItemListViewBinder;
import fr.eoit.activity.util.ItemListViewBinder.RedQuantityBehavior;
import fr.eoit.db.bean.Groups;
import fr.eoit.db.bean.Item;
import fr.eoit.db.bean.ItemMaterials;
import fr.eoit.db.dto.ColumnsNames.Prices;

import java.lang.ref.WeakReference;

/**
 * @author picon.software
 *
 */
public class RequiredItemsBlueprintCopyInventionFragment extends ItemMaterialListFragment {

    private int numberOfChances;
    private long parentTypeId;

    private WeakReference<InventionFragment> fragmentReference;

    public RequiredItemsBlueprintCopyInventionFragment() {
        dataColumns = new String[] { Item._ID, Item.COLUMN_NAME_NAME, ItemMaterials.COLUMN_NAME_QUANTITY,
                Item.COLUMN_NAME_CHOSEN_PRICE_ID, Item.COLUMN_NAME_CHOSEN_PRICE_ID };
        viewIDs = new int[] { R.id.ITEM_ICON, R.id.ITEM_NAME, R.id.ITEM_QUANTITY, R.id.WARN_ICON, R.id.ITEM_PRICE };
        sectionTitleId = -1;
        layoutId = R.layout.blueprint_required_blueprint_copy;
    }

    /**
     * @param numberOfChances the numberOfChances to set
     */
    public void setNumberOfChances(int numberOfChances) {
        this.numberOfChances = numberOfChances;
        initOrRestart();
    }

    /**
     * @param parentTypeId the parentTypeId to set
     */
    public void setParentTypeId(long parentTypeId) {
        this.parentTypeId = parentTypeId;
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
    }

    @Override
    protected Loader<Cursor> getCursorLoader(int id, Bundle args) {
        return new CursorLoader(getActivity(),
                ContentUris.withAppendedId(ItemMaterials.CONTENT_ITEM_ID_URI_BASE_PRICES, parentTypeId),
                new String[] { "i." + Item._ID + " AS " + Item._ID, ItemMaterials.COLUMN_NAME_MATERIAL_ITEM_ID,
                        Item.COLUMN_NAME_CHOSEN_PRICE_ID, Item.COLUMN_NAME_NAME, Prices.COLUMN_NAME_BUY_PRICE,
                        Prices.COLUMN_NAME_SELL_PRICE, Prices.COLUMN_NAME_PRODUCE_PRICE,
                        Prices.COLUMN_NAME_OWN_PRICE, ItemMaterials.COLUMN_NAME_QUANTITY },
                ItemMaterials.COLUMN_NAME_ACTIVITY_ID + " = 5 AND " + Groups.COLUMN_NAME_CATEGORIE_ID + " <> "
                        + EOITConst.Categories.SKILL_CATEGORIE_ID,
                null, "i." + Item._ID + " ASC");
    }

    @Override
    protected void onLoadFinishedAdapteur(Cursor cursor, SimpleCursorAdapter adapter) {
        adapter.setViewBinder(new RequiredItemListViewBinder(RedQuantityBehavior.NONE, fragmentReference));
        adapter.swapCursor(cursor);
    }

    public void setParentFragment(InventionFragment fragment) {
        fragmentReference = new WeakReference<InventionFragment>(fragment);
    }

    private class RequiredItemListViewBinder extends ItemListViewBinder {

        private WeakReference<InventionFragment> fragmentReference;

        public RequiredItemListViewBinder(RedQuantityBehavior behavior,
                WeakReference<InventionFragment> fragmentReference) {
            super(behavior);
            this.fragmentReference = fragmentReference;
        }

        @Override
        protected void addToTotalPrice(long id, double price, long quantity) {

            super.addToTotalPrice(id, price, quantity * numberOfChances);

            if (fragmentReference.get() != null && (getNumberOfElements() - 1) == getCurrentPosition()) { // we are at the last element of the cursor
                fragmentReference.get().setRequiredItemsBlueprintCopyPrice(getTotalPrice() * numberOfChances);
            }
        }

        @Override
        protected void initQuantity(long id, View view, long quantity, int ml, int groupId, double price) {
            super.initQuantity(id, view, quantity * numberOfChances, ml, groupId, price);
        }
    }
}