Android Open Source - MythTrack Hero Display Fragment






From Project

Back to project page MythTrack.

License

The source code is released under:

MIT License

If you think the Android project MythTrack listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
 * HeroDisplay is the main UI fragment for the HeroTracker.
 */* w  ww .j  a  v a  2s . c  o m*/
 * @author Nolan Jurgens
 */

package nolanjurgens.mythtrack.app;

// IMPORTS /////////////////////////////////////////////////////////////////////////////////////////
import android.app.ActionBar;
import android.app.Activity;
import android.app.DialogFragment;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import nolanjurgens.mythtrack.R;
import nolanjurgens.mythtrack.provider.MythTrackContract;

////////////////////////////////////////////////////////////////////////////////////////////////////
// CLASS - HeroDisplayFragment                                                                    //
////////////////////////////////////////////////////////////////////////////////////////////////////
public class HeroDisplayFragment extends Fragment
{
  // FIELDS ////////////////////////////////////////////////////////////////////////////////////////

  /** The hero loaded in the tracker.*/
  private Hero activeHero;
  /** Helper for accessing/modifying the "Heroes" table.*/
  private HeroHelper heroes;
  /** Keeps track of when the hero is fully loaded from the database.*/
  private boolean heroLoaded = false;

  // INTERFACES ////////////////////////////////////////////////////////////////////////////////////

  /** Interface for a listener for when the hero finishes loading.*/
  OnHeroLoadedListener loadedCallback;
  public interface OnHeroLoadedListener
  {
    public void onHeroLoaded();
  }

  // LISTENERS /////////////////////////////////////////////////////////////////////////////////////

  /** Normal click listener.*/
  View.OnClickListener clickListener = new View.OnClickListener()
  {
    /**
     * Handles clicks on the Hero Display fragment views.
     * @param view The view that was long clicked.
     */
    @Override
    public void onClick(View view)
    {
      // Clear the right fragment if in dual pane mode.
      if(checkForDualPaneMode())
      {
        getFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
      }

      switch(view.getId())
      {
        case R.id.herodisplay_courage:
        {
          break;
        }
        case R.id.herodisplay_gold:
        {
          setGold();
          break;
        }
        case R.id.herodisplay_movement:
        {
          break;
        }
        case R.id.herodisplay_serendipity:
        {
          setSerendipity();
          break;
        }
        case R.id.herodisplay_threat:
        {
          setThreat();
          break;
        }
        case R.id.herodisplay_vitality:
        {
          setVitality();
          break;
        }
        case R.id.herodisplay_primary:
        {
          selectPrimary();
          break;
        }
        case R.id.herodisplay_secondary:
        {
          selectSecondary();
          break;
        }
        case R.id.herodisplay_armor:
        {
          selectArmor();
          break;
        }
        case R.id.herodisplay_helm:
        {
          selectHelm();
          break;
        }
        case R.id.herodisplay_accessory:
        {
          selectAccessory();
          break;
        }
        case R.id.herodisplay_potions:
        {
          selectPotion();
          break;
        }
        default:
        {
        }
      }
    }
  };


  /** Long click listener.*/
  View.OnLongClickListener longClickListener = new View.OnLongClickListener()
  {
    /**
     * Handles long clicks on the Hero Display fragment views.
     * @param view The view that was long clicked.
     * @return True if the long click was handled.
     */
    @Override
    public boolean onLongClick(View view)
    {
      // Clear the right fragment if in dual pane mode.
      if(checkForDualPaneMode())
      {
        getFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
      }

      switch(view.getId())
      {
        case R.id.herodisplay_courage:
        {
          showCourageInfo();
          return true;
        }
        case R.id.herodisplay_gold:
        {
          showGoldInfo();
          return true;
        }
        case R.id.herodisplay_movement:
        {
          showMovementInfo();
          return true;
        }
        case R.id.herodisplay_serendipity:
        {
          showSerendipityInfo();
          return true;
        }
        case R.id.herodisplay_threat:
        {
          showThreatInfo();
          return true;
        }
        case R.id.herodisplay_vitality:
        {
          showVitalityInfo();
          return true;
        }
        case R.id.herodisplay_primary:
        {
          showEquipmentInfo();
          return true;
        }
        case R.id.herodisplay_secondary:
        {
          showEquipmentInfo();
          return true;
        }
        case R.id.herodisplay_armor:
        {
          showEquipmentInfo();
          return true;
        }
        case R.id.herodisplay_helm:
        {
          showEquipmentInfo();
          return true;
        }
        case R.id.herodisplay_accessory:
        {
          showEquipmentInfo();
          return true;
        }
        case R.id.herodisplay_potions:
        {
          showPotionInfo();
          return true;
        }
        default:
        {
        }
      }
      return false;
    }
  };

  // ASYNCTASKS ////////////////////////////////////////////////////////////////////////////////////

  /**
   * Loads the hero from the database and populates the interface when it finishes.
   */
  private class LoadHeroTask extends AsyncTask<Long, Void, Boolean>
  {
    /**
     * Loads the hero from the database into the hero object.
     * @param heroID The ID of the hero to be loaded.
     * @return True if the hero loaded successfully.
     */
    @Override
    protected Boolean doInBackground(Long... heroID)
    {
      return heroes.loadHero(activeHero,heroID[0]);
    }


    /**
     * If the hero loaded, update the interface.
     * @param loadedSuccessfully The results of the hero load.
     */
    @Override
    protected void onPostExecute(Boolean loadedSuccessfully)
    {
      // Keep track of if the hero is loaded
      heroLoaded = loadedSuccessfully;

      // Make sure the view is loaded too, then populate it with the hero info.
      if(loadedSuccessfully && getView() != null)
      {
        refreshView();

        // Refresh the equipment info fragment if it exists.
        EquipmentInfoDialog equipmentInfoFragment = (EquipmentInfoDialog)
          getFragmentManager().findFragmentByTag("EquipmentInfoDialog");
        if(equipmentInfoFragment != null)
        {
          loadedCallback.onHeroLoaded();
        }
      }
    }
  }


  /**
   * Saves the hero to the database.
   */
  private class SaveHeroTask extends AsyncTask<Void, Void, Void>
  {
    /**
     * Saves the hero to the database.
     * @param empty Not used.
     * @return Nothing returned.
     */
    protected Void doInBackground(Void... empty)
    {
      // Save changes to the hero's stats.
      heroes.saveHeroStats(activeHero);

      return null;
    }
  }

  // METHODS ///////////////////////////////////////////////////////////////////////////////////////

  /**
   * Check that the hero has the required amount of gold.
   * @param goldAmount The amount of gold needed.
   * @return True if the hero has enough gold.
   */
  public boolean checkForAvailableGold(int goldAmount)
  {
    if(activeHero.getGold() >= goldAmount)
    {
      return true;
    }
    else
    {
      Toast.makeText(getActivity(), R.string.herodisplay_insufficient_gold,
        Toast.LENGTH_SHORT).show();
      return false;
    }
  }


  /**
   * Checks if an item deleted from the inventory was equipped and removes it if so.
   * @param inventoryID The inventory ID of the deleted item.
   * @return True if it's okay to delete the inventory item.
   */
  public boolean checkForDeletedEquipment(long inventoryID)
  {
    int previousVitalityModifier = activeHero.getVitalityModifier();

    int changeResult = 0;
    if(activeHero.getPrimaryID() == inventoryID)
    {
      changeResult = heroes.changeEquipment(activeHero, 0, MythTrackContract.Items.PRIMARY);
    }
    if(activeHero.getSecondaryID() == inventoryID)
    {
      changeResult = heroes.changeEquipment(activeHero, 0, MythTrackContract.Items.SECONDARY);
    }
    if(activeHero.getArmorID() == inventoryID)
    {
      changeResult = heroes.changeEquipment(activeHero, 0, MythTrackContract.Items.ARMOR);
    }
    if(activeHero.getHelmID() == inventoryID)
    {
      changeResult = heroes.changeEquipment(activeHero, 0, MythTrackContract.Items.HELM);
    }
    if(activeHero.getAccessoryID() == inventoryID)
    {
      changeResult = heroes.changeEquipment(activeHero, 0, MythTrackContract.Items.ACCESSORY);
    }

    if(changeResult == HeroHelper.EQUIPMENT_CHANGE_SUCCEEDED)
    {
      heroes.updateThreat(activeHero);
      heroes.updateVitality(activeHero, previousVitalityModifier);
    }
    else if(changeResult == HeroHelper.EQUIPMENT_CHANGE_WOULD_KILL)
    {
      Toast.makeText(getActivity(), R.string.herodisplay_remove_kill,
        Toast.LENGTH_SHORT).show();
      return false;
    }
    return true;
  }


  /**
   * Determines if the activity is running in dual pane mode.
   * @return True if the activity is in dual pane mode.
   */
  public boolean checkForDualPaneMode()
  {
    // See if the dedicated HeroDisplay container is present and visible.
    View heroDisplayContainer = getActivity().findViewById(R.id.herotracker_herodisplay_container);
    return (heroDisplayContainer != null
      && heroDisplayContainer.getVisibility() == View.VISIBLE);
  }


  /**
   * Returns the active hero object.
   * @return The active hero.
   */
  public Hero getActiveHero()
  {
    if(heroLoaded)
    {
      return activeHero;
    }
    else
    {
      return null;
    }
  }


  /**
   * Load the static view layout with the dynamic hero information.
   * @param view The root view for the fragment.
   */
  private void loadView(View view)
  {
    // Load the hero's name and class.
    TextView nameView = (TextView) view.findViewById(R.id.herodisplay_name);
    String name = activeHero.getName() + " - " + activeHero.getClassName();
    nameView.setText(name);

    // Load the hero class icon.
    int classIcon = 0;
    switch(activeHero.getClassID())
    {
      case MythTrackContract.Heroes.ACOLYTE:
      {
        classIcon = R.drawable.ic_class_acolyte;
        break;
      }
      case MythTrackContract.Heroes.APPRENTICE:
      {
        classIcon = R.drawable.ic_class_apprentice;
        break;
      }
      case MythTrackContract.Heroes.ARCHER:
      {
        classIcon = R.drawable.ic_class_archer;
        break;
      }

      case MythTrackContract.Heroes.BRIGAND:
      {
        classIcon = R.drawable.ic_class_brigand;
        break;
      }
      case MythTrackContract.Heroes.DRUID:
      {
        classIcon = R.drawable.ic_class_spriggan;
        break;
      }
      case MythTrackContract.Heroes.SKALD:
      {
        classIcon = R.drawable.ic_class_skald;
        break;
      }
      case MythTrackContract.Heroes.SOLDIER:
      {
        classIcon = R.drawable.ic_class_soldier;
        break;
      }
      case MythTrackContract.Heroes.SPRIGGAN:
      {
        classIcon = R.drawable.ic_class_spriggan;
        break;
      }
      case MythTrackContract.Heroes.TRICKSTER:
      {
        classIcon = R.drawable.ic_class_trickster;
        break;
      }
    }

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
    {
      nameView.setCompoundDrawablesRelativeWithIntrinsicBounds(classIcon, 0, 0, 0);
    }
    else
    {
      nameView.setCompoundDrawablesWithIntrinsicBounds(classIcon, 0, 0, 0);
    }

    // Load the hero's movement points.
    TextView movementView = (TextView) view.findViewById(R.id.herodisplay_movement);
    int currentMovement = activeHero.getMovement() + activeHero.getMovementPointsModifier();
    String movement = Integer.toString(currentMovement);
    movementView.setText(movement);
    movementView.setOnLongClickListener(longClickListener);

    // Load the hero's courage points.
    TextView courageView = (TextView) view.findViewById(R.id.herodisplay_courage);

    // NCA bonuses apply to courage in rule set 1.0, but not in 1.1+.
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
    String rulesVersion = preferences.getString("pref_rules_version",
      getString(R.string.pref_rules_version_default));

    int currentCourage;
    if(rulesVersion.equals("1.0"))
    {
      currentCourage = activeHero.getCourage() + activeHero.getCourageModifier()
        + activeHero.getNonCombatActionModifier();
    }
    else
    {
      currentCourage = activeHero.getCourage() + activeHero.getCourageModifier();
    }


    String courage;
    if(currentCourage < 0)
    {
      courage = "0";
    }
    else
    {
      courage = Integer.toString(currentCourage);
    }
    courageView.setText(courage);
    courageView.setOnLongClickListener(longClickListener);

    // Load the hero's threat level.
    TextView threatView = (TextView) view.findViewById(R.id.herodisplay_threat);
    String threat = Integer.toString(activeHero.getThreat());
    threatView.setText(threat);
    threatView.setOnClickListener(clickListener);
    threatView.setOnLongClickListener(longClickListener);
    threatView.setClickable(true);

    // Load the hero's vitality.
    TextView vitalityView = (TextView) view.findViewById(R.id.herodisplay_vitality);
    String vitality = Integer.toString(activeHero.getVitality());
    vitalityView.setText(vitality);
    vitalityView.setOnClickListener(clickListener);
    vitalityView.setOnLongClickListener(longClickListener);
    vitalityView.setClickable(true);

    // Load the hero's amount of gold.
    TextView goldView = (TextView) view.findViewById(R.id.herodisplay_gold);
    String gold = Integer.toString(activeHero.getGold());
    goldView.setText(gold);
    goldView.setOnClickListener(clickListener);
    goldView.setOnLongClickListener(longClickListener);
    goldView.setClickable(true);

    // Load the hero's amount of serendipity.
    TextView serendipityView = (TextView) view.findViewById(R.id.herodisplay_serendipity);
    String serendipity = Integer.toString(activeHero.getSerendipity());
    serendipityView.setText(serendipity);
    serendipityView.setOnClickListener(clickListener);
    serendipityView.setOnLongClickListener(longClickListener);
    serendipityView.setClickable(true);

    // Load the hero's primary item.
    TextView primaryView = (TextView) view.findViewById(R.id.herodisplay_primary);
    String primary = activeHero.getPrimaryItem().getName();
    primaryView.setText(primary);
    primaryView.setOnClickListener(clickListener);
    primaryView.setOnLongClickListener(longClickListener);
    primaryView.setClickable(true);

    // Load the hero's secondary item.
    TextView secondaryView = (TextView) view.findViewById(R.id.herodisplay_secondary);
    String secondary = activeHero.getSecondaryItem().getName();
    secondaryView.setText(secondary);
    secondaryView.setOnClickListener(clickListener);
    secondaryView.setOnLongClickListener(longClickListener);
    secondaryView.setClickable(true);

    // Load the hero's armor item.
    TextView armorView = (TextView) view.findViewById(R.id.herodisplay_armor);
    String armor = activeHero.getArmorItem().getName();
    armorView.setText(armor);
    armorView.setOnClickListener(clickListener);
    armorView.setOnLongClickListener(longClickListener);
    armorView.setClickable(true);

    // Load the hero's helm item.
    TextView helmView = (TextView) view.findViewById(R.id.herodisplay_helm);
    String helm = activeHero.getHelmItem().getName();
    helmView.setText(helm);
    helmView.setOnClickListener(clickListener);
    helmView.setOnLongClickListener(longClickListener);
    helmView.setClickable(true);

    // Load the hero's accessory item.
    TextView accessoryView = (TextView) view.findViewById(R.id.herodisplay_accessory);
    String accessory = activeHero.getAccessoryItem().getName();
    accessoryView.setText(accessory);
    accessoryView.setOnClickListener(clickListener);
    accessoryView.setOnLongClickListener(longClickListener);
    accessoryView.setClickable(true);

    // Load the hero's potions.
    TextView potionView = (TextView) view.findViewById(R.id.herodisplay_potions);
    potionView.setOnClickListener(clickListener);
    potionView.setOnLongClickListener(longClickListener);
    potionView.setClickable(true);
  }


  /**
   * Called when the fragment is attached to the activity.
   * @param activity The host activity.
   */
  @Override
  public void onAttach(Activity activity)
  {
    super.onAttach(activity);

    // Make sure the host activity implements the interfaces.
    try
    {
      loadedCallback = (OnHeroLoadedListener) activity;
    }
    catch(ClassCastException exception)
    {
      throw new ClassCastException(activity.toString() + " must implement onHeroLoaded");
    }
  }


  /**
   * Called when the activity is launched.
   * @param savedInstanceState Saved instance information.
   */
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);

    // Initialize the hero table helper.
    Context context = getActivity();
    heroes = new HeroHelper(context);

    // Load up the selected hero.
    activeHero = new Hero(context);
    new LoadHeroTask().execute(getArguments().getLong("hero_id"));
  }


  /**
   * Called when the fragment is about to be drawn for the first time.
   * @param inflater The layout inflater.
   * @param container The fragment container from the activity layout.
   * @param savedInstanceState Saved instance information.
   * @return The root view of the fragment.
   */
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState)
  {
    View view = inflater.inflate(R.layout.fragment_hero_display, container, false);

    // Refresh the view. If the hero is loading the first time this won't trigger. If the hero is
    // already loaded, such as when returning to the fragment, it will.
    if(heroLoaded)
    {
      loadView(view);
    }

    // Set the subtitle
    ActionBar actionBar = getActivity().getActionBar();
    if(actionBar != null)
    {
      getActivity().getActionBar().setSubtitle(R.string.herodisplay_subtitle);
    }

    return view;
  }


  /**
   * Add items to the action bar.
   * @param menu The menu that will be populated.
   * @param inflater The menu inflater.
   */
  @Override
  public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
  {
    // Inflate the resource to create the menu.
    inflater.inflate(R.menu.hero_display, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }


  /**
   * Called when the active hero has their equipment changed.
   * @param inventoryID The inventory ID of the item being equipped.
   * @param itemType The type of item being equipped.
   */
  public void onItemEquipped(long inventoryID, int itemType)
  {
    int previousVitalityModifier = activeHero.getVitalityModifier();
    int changeResult = heroes.changeEquipment(activeHero, inventoryID, itemType);
    if(changeResult == HeroHelper.EQUIPMENT_CHANGE_SUCCEEDED)
    {
      heroes.updateThreat(activeHero);
      heroes.updateVitality(activeHero, previousVitalityModifier);
    }
    else if(changeResult == HeroHelper.EQUIPMENT_CHANGE_WOULD_KILL)
    {
      Toast.makeText(getActivity(), R.string.herodisplay_change_kill,
        Toast.LENGTH_SHORT).show();
    }
  }


  /**
   * Remove gold from a hero's coffers when an item is purchased.
   * @param itemCost The amount of gold to remove.
   */
  public void onItemPurchased(int itemCost)
  {
    activeHero.setGold(activeHero.getGold() - itemCost);
  }


  /**
   * Add gold to a hero's coffers when an item is sold.
   * @param goldValue The amount of gold to add.
   */
  public void onItemSold(int goldValue)
  {
    activeHero.setGold(activeHero.getGold() + goldValue);
  }


  /**
   * Handle a selected action button.
   * @param item The item that was selected.
   * @return Returns true to signal item was processed.
   */
  @Override
  public boolean onOptionsItemSelected(MenuItem item)
  {
    switch(item.getItemId())
    {
      default:
      {
        return super.onOptionsItemSelected(item);
      }
    }
  }


  /**
   * Called when the fragment is paused.
   */
  @Override
  public void onPause()
  {
    super.onPause();

    // Save changes to the hero's stats.
    new SaveHeroTask().execute();
  }


  /**
   * Use the given potion on the hero.
   * @param potionType The potion ID of the potion used.
   * @return True if the potion was used.
   */
  public boolean onPotionUsed(int potionType, String potionName)
  {
    switch(potionType)
    {
      case MythTrackContract.Items.ANTI_VENOM_POTION:
      {
        // TODO - Add cure logic.
        Toast.makeText(getActivity(), R.string.herodisplay_used_anti_venom,
          Toast.LENGTH_LONG).show();
        return true;
      }
      case MythTrackContract.Items.FOCUS_POTION:
      {
        String potionMessage = getString(R.string.herodisplay_used_potion);
        Toast.makeText(getActivity(), String.format(potionMessage, potionName),
          Toast.LENGTH_LONG).show();
        return true;
      }
      case MythTrackContract.Items.LUCKY_POTION:
      {
        String potionMessage = getString(R.string.herodisplay_used_potion);
        Toast.makeText(getActivity(), String.format(potionMessage, potionName),
          Toast.LENGTH_LONG).show();
        return true;
      }
      case MythTrackContract.Items.MAJOR_VITALITY_POTION:
      {
        // TODO - Add heal logic.
        String potionMessage = getString(R.string.herodisplay_used_potion);
        Toast.makeText(getActivity(), String.format(potionMessage, potionName),
          Toast.LENGTH_LONG).show();
        return true;
      }
      case MythTrackContract.Items.SHADOW_POTION:
      {
        String potionMessage = getString(R.string.herodisplay_used_potion);
        Toast.makeText(getActivity(), String.format(potionMessage, potionName),
          Toast.LENGTH_LONG).show();
        return true;
      }
      case MythTrackContract.Items.VITALITY_POTION:
      {

        int currentVitality = activeHero.getVitality();
        int maxVitality = activeHero.getBaseVitality() + activeHero.getVitalityModifier();

        // Don't use if hero is at max health already.
        if(currentVitality == maxVitality)
        {
          String maxVitalityMessage = getString(R.string.herodisplay_potion_na_vitality);
          Toast.makeText(getActivity(), String.format(maxVitalityMessage, potionName),
            Toast.LENGTH_LONG).show();
          return false;
        }

        // Heal the hero by 4, not exceeding max vitality.
        int healedVitality;

        if((maxVitality - currentVitality) < 4)
        {
          healedVitality = maxVitality - currentVitality;
        }
        else
        {
          healedVitality = 4;
        }
        activeHero.setVitality(currentVitality + healedVitality);

        String healedMessage = getString(R.string.herodisplay_used_vitality);
        Toast.makeText(getActivity(), String.format(healedMessage, potionName, healedVitality),
          Toast.LENGTH_LONG).show();
        return true;
      }
      default:
      {
        return true;
      }
    }
  }


  /**
   * Update the statistic in the database and interface.
   * @param stat The stat that needs to be updated.
   * @param value The new value to set.
   */
  public void onStatSet(int stat, int value)
  {
    // Make sure we can get a handle to the fragment view.
    if(getView() == null)
    {
      return;
    }

    // Choose which stat to set based on the passed parameter.
    switch(stat)
    {
      case StatPickerDialog.STAT_GOLD:
      {
        // Update the hero.
        activeHero.setGold(value);
        // Update the interface.
        TextView statView = (TextView) getView().findViewById(R.id.herodisplay_gold);
        String statText = Integer.toString(value);
        statView.setText(statText);
        break;
      }
      case StatPickerDialog.STAT_SERENDIPITY:
      {
        // Update the hero.
        activeHero.setSerendipity(value);
        // Update the interface.
        TextView statView = (TextView) getView().findViewById(R.id.herodisplay_serendipity);
        String statText = Integer.toString(value);
        statView.setText(statText);
        break;
      }
      case StatPickerDialog.STAT_THREAT:
      {
        // Update the hero.
        activeHero.setThreat(value);
        // Update the interface.
        TextView statView = (TextView) getView().findViewById(R.id.herodisplay_threat);
        String statText = Integer.toString(value);
        statView.setText(statText);
        break;
      }
      case StatPickerDialog.STAT_VITALITY:
      {
        // Update the hero.
        activeHero.setVitality(value);
        // Update the interface.
        TextView statView = (TextView) getView().findViewById(R.id.herodisplay_vitality);
        String statText = Integer.toString(value);
        statView.setText(statText);
        break;
      }
    }
  }


  /**
   * Refresh the loaded static view layout with the updated hero information.
   */
  public void refreshView()
  {
    View view = getView();
    if(view != null)
    {
      loadView(view);
    }
  }


  /**
   * Trigger a new fragment to select the accessory.
   */
  public void selectAccessory()
  {
    long heroID = getArguments().getLong("hero_id");
    InventoryList accessoryList = new InventoryList();
    Bundle bundle = new Bundle();
    bundle.putInt("type", MythTrackContract.Items.ACCESSORY);
    bundle.putLong("hero_id", heroID);
    accessoryList.setArguments(bundle);

    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.replace(R.id.herotracker_fragment_container, accessoryList, "AccessoryList");
    transaction.addToBackStack(null);
    transaction.commit();
  }

  /**
   * Trigger a new fragment to select the armor.
   */
  public void selectArmor()
  {
    long heroID = getArguments().getLong("hero_id");
    InventoryList armorList = new InventoryList();
    Bundle bundle = new Bundle();
    bundle.putInt("type", MythTrackContract.Items.ARMOR);
    bundle.putLong("hero_id", heroID);
    armorList.setArguments(bundle);

    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.replace(R.id.herotracker_fragment_container, armorList, "ArmorList");
    transaction.addToBackStack(null);
    transaction.commit();
  }

  /**
   * Trigger a new fragment to select the helm.
   */
  public void selectHelm()
  {
    long heroID = getArguments().getLong("hero_id");
    InventoryList helmList = new InventoryList();
    Bundle bundle = new Bundle();
    bundle.putInt("type", MythTrackContract.Items.HELM);
    bundle.putLong("hero_id", heroID);
    helmList.setArguments(bundle);

    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.replace(R.id.herotracker_fragment_container, helmList, "HelmList");
    transaction.addToBackStack(null);
    transaction.commit();
  }

  /**
   * Trigger a new fragment to select a potion.
   */
  public void selectPotion()
  {
    long heroID = getArguments().getLong("hero_id");
    InventoryList potionList = new InventoryList();
    Bundle bundle = new Bundle();
    bundle.putInt("type", MythTrackContract.Items.POTION);
    bundle.putLong("hero_id", heroID);
    potionList.setArguments(bundle);

    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.replace(R.id.herotracker_fragment_container, potionList, "PotionList");
    transaction.addToBackStack(null);
    transaction.commit();
  }

  /**
   * Trigger a new fragment to select the primary.
   */
  public void selectPrimary()
  {
    long heroID = getArguments().getLong("hero_id");
    InventoryList primaryList = new InventoryList();
    Bundle bundle = new Bundle();
    bundle.putInt("type", MythTrackContract.Items.PRIMARY);
    bundle.putLong("hero_id", heroID);
    primaryList.setArguments(bundle);

    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.replace(R.id.herotracker_fragment_container, primaryList, "PrimaryList");
    transaction.addToBackStack(null);
    transaction.commit();
  }

  /**
   * Trigger a new fragment to select the secondary.
   */
  public void selectSecondary()
  {
    long heroID = getArguments().getLong("hero_id");
    InventoryList secondaryList = new InventoryList();
    Bundle bundle = new Bundle();
    bundle.putInt("type", MythTrackContract.Items.SECONDARY);
    bundle.putLong("hero_id", heroID);
    secondaryList.setArguments(bundle);

    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.replace(R.id.herotracker_fragment_container, secondaryList, "SecondaryList");
    transaction.addToBackStack(null);
    transaction.commit();
  }


  /**
   * Allow the user to spend or receive gold.
   */
  public void setGold()
  {
    Bundle arguments = new Bundle();
    arguments.putInt("stat", StatPickerDialog.STAT_GOLD);
    arguments.putInt("value", activeHero.getGold());
    DialogFragment goldPickerDialog = new StatPickerDialog();
    goldPickerDialog.setArguments(arguments);
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    goldPickerDialog.show(transaction, "GoldPickerDialog");
  }


  /**
   * Allow the user to spend or receive serendipity.
   */
  public void setSerendipity()
  {
    Bundle arguments = new Bundle();
    arguments.putInt("stat", StatPickerDialog.STAT_SERENDIPITY);
    arguments.putInt("value", activeHero.getSerendipity());
    DialogFragment serendipityPickerDialog = new StatPickerDialog();
    serendipityPickerDialog.setArguments(arguments);
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    serendipityPickerDialog.show(transaction, "SerendipityPickerDialog");
  }


  /**
   * Allow the user to change the threat level value.
   */
  public void setThreat()
  {
    Bundle arguments = new Bundle();
    arguments.putInt("stat", StatPickerDialog.STAT_THREAT);
    arguments.putInt("value", activeHero.getThreat());
    arguments.putInt("min_threat", activeHero.getMinThreat());
    arguments.putInt("max_threat", activeHero.getMaxThreat());
    DialogFragment threatPickerDialog = new StatPickerDialog();
    threatPickerDialog.setArguments(arguments);
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    threatPickerDialog.show(transaction, "ThreatPickerDialog");
  }


  /**
   * Allow the user to change the vitality value.
   */
  public void setVitality()
  {
    Bundle arguments = new Bundle();
    arguments.putInt("stat", StatPickerDialog.STAT_VITALITY);
    arguments.putInt("value", activeHero.getVitality());
    arguments.putInt("max_vitality", activeHero.getBaseVitality()
      + activeHero.getVitalityModifier());
    DialogFragment vitalityPickerDialog = new StatPickerDialog();
    vitalityPickerDialog.setArguments(arguments);
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    vitalityPickerDialog.show(transaction, "VitalityPickerDialog");
  }

  
  /**
   * Show a dialog with information about courage.
   */
  public void showCourageInfo()
  {
    Bundle arguments = new Bundle();
    arguments.putInt("stat", StatInfoDialog.STAT_COURAGE);

    // NCA bonuses apply to courage in rule set 1.0, but not in 1.1+.
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
    String rulesVersion = preferences.getString("pref_rules_version",
      getString(R.string.pref_rules_version_default));
    if(rulesVersion.equals("1.0"))
    {
      arguments.putInt("courage", activeHero.getCourage() + activeHero.getCourageModifier()
        + activeHero.getNonCombatActionModifier());
    }
    else
    {
      arguments.putInt("courage", activeHero.getCourage() + activeHero.getCourageModifier());
    }

    DialogFragment courageInfoDialog = new StatInfoDialog();
    courageInfoDialog.setArguments(arguments);
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    courageInfoDialog.show(transaction, "CourageInfoDialog");
  }


  /**
   * Show a dialog with information about equipped items.
   */
  public void showEquipmentInfo()
  {
    DialogFragment equipmentInfoDialog = new EquipmentInfoDialog();
    FragmentTransaction transaction = getFragmentManager().beginTransaction();

    // Display in right pane or as dialog.
    if(checkForDualPaneMode())
    {
      transaction.replace(R.id.herotracker_fragment_container, equipmentInfoDialog,
        "EquipmentInfoDialog");
      transaction.commit();
    }
    else
    {
      equipmentInfoDialog.show(transaction, "EquipmentInfoDialog");
    }
  }


  /**
   * Show a dialog with information about gold.
   */
  public void showGoldInfo()
  {
    Bundle arguments = new Bundle();
    arguments.putInt("stat", StatInfoDialog.STAT_GOLD);
    DialogFragment goldInfoDialog = new StatInfoDialog();
    goldInfoDialog.setArguments(arguments);
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    goldInfoDialog.show(transaction, "GoldInfoDialog");
  }


  /**
   * Show a dialog with information about movement.
   */
  public void showMovementInfo()
  {
    Bundle arguments = new Bundle();
    arguments.putInt("stat", StatInfoDialog.STAT_MOVEMENT);
    arguments.putInt("movement", activeHero.getMovement() + activeHero.getMovementPointsModifier());
    arguments.putInt("cautious_movement", activeHero.getCautiousMovementModifier());
    DialogFragment movementInfoDialog = new StatInfoDialog();
    movementInfoDialog.setArguments(arguments);
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    movementInfoDialog.show(transaction, "MovementInfoDialog");
  }


  /**
   * Show a dialog with information about potions.
   */
  public void showPotionInfo()
  {
    Bundle arguments = new Bundle();
    arguments.putInt("stat", StatInfoDialog.STAT_POTIONS);
    DialogFragment potionInfoDialog = new StatInfoDialog();
    potionInfoDialog.setArguments(arguments);
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    potionInfoDialog.show(transaction, "PotionInfoDialog");
  }


  /**
   * Show a dialog with information about serendipity.
   */
  public void showSerendipityInfo()
  {
    Bundle arguments = new Bundle();
    arguments.putInt("stat", StatInfoDialog.STAT_SERENDIPITY);
    DialogFragment serendipityInfoDialog = new StatInfoDialog();
    serendipityInfoDialog.setArguments(arguments);
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    serendipityInfoDialog.show(transaction, "SerendipityInfoDialog");
  }


  /**
   * Show a dialog with information about threat.
   */
  public void showThreatInfo()
  {
    Bundle arguments = new Bundle();
    arguments.putInt("stat", StatInfoDialog.STAT_THREAT);
    arguments.putInt("threat", activeHero.getThreat());
    arguments.putInt("class_id", activeHero.getClassID());
    DialogFragment threatInfoDialog = new StatInfoDialog();
    threatInfoDialog.setArguments(arguments);
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    threatInfoDialog.show(transaction, "ThreatInfoDialog");
  }


  /**
   * Show a dialog with information about vitality.
   */
  public void showVitalityInfo()
  {
    Bundle arguments = new Bundle();
    arguments.putInt("stat", StatInfoDialog.STAT_VITALITY);
    arguments.putInt("vitality", activeHero.getVitality());
    arguments.putInt("max_vitality", activeHero.getBaseVitality() + activeHero.getVitalityModifier());
    DialogFragment vitalityInfoDialog = new StatInfoDialog();
    vitalityInfoDialog.setArguments(arguments);
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    vitalityInfoDialog.show(transaction, "VitalityInfoDialog");
  }


}




Java Source Code List

nolanjurgens.mythtrack.app.BuyItemDialog.java
nolanjurgens.mythtrack.app.CreateHeroDialog.java
nolanjurgens.mythtrack.app.DeleteHeroDialog.java
nolanjurgens.mythtrack.app.EquipmentInfoDialog.java
nolanjurgens.mythtrack.app.HeroDisplayFragment.java
nolanjurgens.mythtrack.app.HeroHelper.java
nolanjurgens.mythtrack.app.HeroList.java
nolanjurgens.mythtrack.app.HeroTrackerActivity.java
nolanjurgens.mythtrack.app.Hero.java
nolanjurgens.mythtrack.app.InventoryList.java
nolanjurgens.mythtrack.app.ItemHelper.java
nolanjurgens.mythtrack.app.ItemList.java
nolanjurgens.mythtrack.app.Item.java
nolanjurgens.mythtrack.app.MainActivity.java
nolanjurgens.mythtrack.app.RemoveItemDialog.java
nolanjurgens.mythtrack.app.SettingsActivity.java
nolanjurgens.mythtrack.app.SettingsFragment.java
nolanjurgens.mythtrack.app.StatInfoDialog.java
nolanjurgens.mythtrack.app.StatPickerDialog.java
nolanjurgens.mythtrack.app.TitleHelper.java
nolanjurgens.mythtrack.app.Title.java
nolanjurgens.mythtrack.provider.MythTrackBackup.java
nolanjurgens.mythtrack.provider.MythTrackContract.java
nolanjurgens.mythtrack.provider.MythTrackDatabase.java