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

Java tutorial

Introduction

Here is the source code for fr.eoit.activity.fragment.blueprint.RequiredBlueprintCopyInventionFragment.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.database.MatrixCursor;
import android.os.Bundle;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.SimpleCursorAdapter;
import fr.eoit.R;
import fr.eoit.activity.fragment.ItemListFragment;
import fr.eoit.activity.util.ItemListViewBinder;
import fr.eoit.activity.util.ItemListViewBinder.RedQuantityBehavior;
import fr.eoit.db.bean.Blueprint;
import fr.eoit.db.bean.ItemMaterials;
import fr.eoit.db.dto.ColumnsNames.Prices;
import fr.eoit.formula.FormulaCalculator;
import fr.piconsoft.db.util.DbUtil;

import java.lang.ref.WeakReference;

/**
 * @author picon.software
 *
 */
public class RequiredBlueprintCopyInventionFragment extends ItemListFragment<SimpleCursorAdapter> {

    protected String[] dataColumns = { Blueprint._ID, Blueprint.COLUMN_NAME_NAME,
            ItemMaterials.COLUMN_NAME_QUANTITY };
    protected int[] viewIDs = { R.id.ITEM_ICON, R.id.ITEM_NAME, R.id.ITEM_QUANTITY };

    private int numberOfChances, maxProdLimit;
    private long parentTypeId;

    private WeakReference<InventionFragment> fragmentReference;

    public RequiredBlueprintCopyInventionFragment() {
        sectionTitleId = -1;
        layoutId = R.layout.blueprint_required_blueprint_copy;
    }

    /**
     * @param maxProdLimit the maxProdLimit to set
     */
    public void setMaxProdLimit(int maxProdLimit) {
        this.maxProdLimit = maxProdLimit;
    }

    /**
     * @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 SimpleCursorAdapter adapteurInit() {
        return null;
    }

    @Override
    protected Loader<Cursor> getCursorLoader(int id, Bundle args) {
        return new CursorLoader(getActivity(),
                ContentUris.withAppendedId(Blueprint.CONTENT_ITEM_ID_URI_BASE, parentTypeId),
                new String[] { Blueprint._ID, Blueprint.COLUMN_NAME_NAME,
                        Blueprint.COLUMN_NAME_MAX_PRODUCTION_LIMIT, Blueprint.COLUMN_NAME_RESEARCH_COPY_TIME },
                null, null, null);
    }

    @Override
    protected void onLoadFinishedAdapteur(Cursor cursor, SimpleCursorAdapter adapter) {
        cursor.moveToFirst();

        long id = cursor.getLong(cursor.getColumnIndexOrThrow(Blueprint._ID));
        int maxProductionLimit = (this.maxProdLimit / 10) > 1
                ? cursor.getInt(cursor.getColumnIndexOrThrow(Blueprint.COLUMN_NAME_MAX_PRODUCTION_LIMIT))
                : 1;
        int baseCopyTime = cursor.getInt(cursor.getColumnIndexOrThrow(Blueprint.COLUMN_NAME_RESEARCH_COPY_TIME));
        double price = FormulaCalculator.calculateMaxRunsCopyCost(baseCopyTime, 1000, 4343, 1.5F, 0.5F);

        MatrixCursor blueprintCopyCursor = new MatrixCursor(new String[] { Blueprint._ID,
                Blueprint.COLUMN_NAME_NAME, ItemMaterials.COLUMN_NAME_QUANTITY, Prices.COLUMN_NAME_PRODUCE_PRICE });

        String name = String.valueOf(maxProductionLimit) + " "
                + getResources().getText(R.string.blueprint_copy_str1) + " "
                + cursor.getString(cursor.getColumnIndexOrThrow(Blueprint.COLUMN_NAME_NAME));

        DbUtil.addRowToMatrixCursor(blueprintCopyCursor, id, name, numberOfChances, price);

        refreshAdapteur(new SimpleCursorAdapter(getActivity(), R.layout.item_row_small, blueprintCopyCursor,
                dataColumns, viewIDs, SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER));

        getAdapter().setViewBinder(new ItemListViewBinder(RedQuantityBehavior.PRICE));

        if (fragmentReference.get() != null) {
            fragmentReference.get().setSingleBlueprintCopyPrice(price);
        }
    }

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