Android Open Source - on-the-hook Fish Activity






From Project

Back to project page on-the-hook.

License

The source code is released under:

MIT License

If you think the Android project on-the-hook 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

package com.yoandinkov.onthehook;
//  w w w . j a  v a2  s. c  o  m
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.HttpStatus;

import com.yoandinkov.onthehook.crypt.JsonConverter;
import com.yoandinkov.onthehook.db.DatabaseManager;
import com.yoandinkov.onthehook.db.models.FishDbModel;
import com.yoandinkov.onthehook.models.Fish;
import com.yoandinkov.onthehook.models.FishSpecies;
import com.yoandinkov.onthehook.models.HttpResponseWrapper;
import com.yoandinkov.onthehook.models.RequestPurpose;
import com.yoandinkov.onthehook.models.Response;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class FishActivity extends RequestActivity{

  private Button ButtonCatchFish;
  private Spinner SpinnerFishSpecies;
  private SeekBar SeekBarFishWeight;
  private TextView TextViewFishWeight;
  private int fishingId;
  private FishDbModel caugthFish;
  // button catch
  private HashMap<String, Integer> fishSpecies;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fish);
    
    int fishingId = getIntent().getExtras().getInt("fishingId");

    this.setCaughtFish(new FishDbModel());
    this.setFishingId(fishingId);
    this.setSpinnerFishSpecies((Spinner)findViewById(R.id.fishSpinnerFishSpecies));
    this.setFishSpecies(new HashMap<String, Integer>());
    this.setSeekBarFishWeight((SeekBar)findViewById(R.id.fishSeekBarFishWeight));
    this.setTextViewFishWeight((TextView)findViewById(R.id.fishTextViewFishWeight));
    this.setButtonCatchFish((Button)findViewById(R.id.fishButtonCatch));
    
    this.getSeekBarFishWeight().setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
            public void onStopTrackingTouch(SeekBar seekBar) {
            }

            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
              double kilos = 0.1 * progress;
              
              if (progress == 0) {
                kilos += 0.01;
              }
              
              FishActivity.this.getCaughtFish().setWeight(kilos);
              
                FishActivity.this.getTextViewFishWeight().setText("Kilos: " +  new DecimalFormat("#.##").format(kilos));
            }
        });
    
    this.getButtonCatchFish().setOnClickListener(this);
    
    this.getData();
  }

  @Override
  protected void handleResponse(HttpResponseWrapper response) {
    
    RequestPurpose purpose = response.getPurpose();
    Response currentResponse = response.getResponse();
    
    switch(purpose) {
    case GET_FISH_SPECIES:
      this.setUpSpinner(currentResponse);
      break;
    case CATCH_FISH:
      this.handleCaughtFish(currentResponse);
      break;
    default:
      break;
    }
  }
  
  @Override
  public void onClick(View clickedView) {
    int clickedViewId = clickedView.getId();
    
    switch(clickedViewId) {
    case R.id.fishButtonCatch :
      this.catchFish(clickedView);
    break;
    }
  }
  
  private void catchFish(View currentView) {
    
    String fishSpeciesName = this.getSpinnerFishSpecies().getSelectedItem().toString();
    
    int fishSpeciesId = this.getFishSpeciesValue(fishSpeciesName);
    
    Fish currentFish = new Fish();
    
    currentFish.setFishSpeciesId(fishSpeciesId);
    currentFish.setWeight(this.getCaughtFish().getWeight());
    
    this.getCaughtFish().setName(fishSpeciesName);
    
    String json = JsonConverter.toJson(currentFish);
    String url = String.format("%s/fishings/%d/catch", this.getApiUrl(), this.getFishingId());
    
    HttpPutRequester currentRequest = new HttpPutRequester();
    
    currentRequest.setCurrentPurpose(RequestPurpose.CATCH_FISH);
    
    currentRequest.execute(url, json);
  }
  
  private void handleCaughtFish(Response currentResponse) {
    if(currentResponse.getStatusCode() == HttpStatus.SC_OK) {
      FishDbModel fishForDb = this.getCaughtFish();
      
      DatabaseManager.init(this);
      DatabaseManager.getInstance().addFishToDb(fishForDb);
      
      Intent goToFishing = new Intent(this, FishingActivity.class);
      startActivity(goToFishing);
      finish();
    } else {
      Toast.makeText(this, currentResponse.getBody(), Toast.LENGTH_LONG).show();
    }
  }
  
  private void setUpSpinner(Response response) {
    if (response.getStatusCode() == HttpStatus.SC_OK) {
      
      List<FishSpecies> result = JsonConverter.fromJsonToFishSpecies(response.getBody());
      
      this.setData(result);
      
      ArrayList<String> fishSpeciesNames = new ArrayList<String>(this.getFishSpecies().keySet());
      
      ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, fishSpeciesNames);
        
      dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        
      this.getSpinnerFishSpecies().setAdapter(dataAdapter);
    } else {
      Toast.makeText(this, response.getBody(), Toast.LENGTH_LONG).show();
    }
  }
  
  private void setData(List<FishSpecies> fishSpecies) {
    for(int i = 0; i < fishSpecies.size(); i++) {
      this.getFishSpecies().put(fishSpecies.get(i).getName(), fishSpecies.get(i).getId());
    }
  }

  private void getData() {
    HttpGetRequester currentRequest = new HttpGetRequester();
    
    currentRequest.setCurrentPurpose(RequestPurpose.GET_FISH_SPECIES);
    
    String url = String.format("%s/species", this.getApiUrl());
    
    currentRequest.execute(url);
  }
  
  public HashMap<String, Integer> getFishSpecies() {
    return fishSpecies;
  }

  public void setFishSpecies(HashMap<String, Integer> fishSpecies) {
    this.fishSpecies = fishSpecies;
  }
  
  private int getFishSpeciesValue(String name) {
    int currentFishSpeciesId = this.getFishSpecies().get(name);
    
    return currentFishSpeciesId;
  }

  public Spinner getSpinnerFishSpecies() {
    return SpinnerFishSpecies;
  }

  public void setSpinnerFishSpecies(Spinner spinnerFishSpecies) {
    SpinnerFishSpecies = spinnerFishSpecies;
  }

  private SeekBar getSeekBarFishWeight() {
    return SeekBarFishWeight;
  }

  private void setSeekBarFishWeight(SeekBar seekBarFishWeight) {
    SeekBarFishWeight = seekBarFishWeight;
  }

  private TextView getTextViewFishWeight() {
    return TextViewFishWeight;
  }

  private void setTextViewFishWeight(TextView textViewFishWeight) {
    TextViewFishWeight = textViewFishWeight;
  }

  private Button getButtonCatchFish() {
    return ButtonCatchFish;
  }

  private void setButtonCatchFish(Button buttonCatchFish) {
    ButtonCatchFish = buttonCatchFish;
  }

  public int getFishingId() {
    return fishingId;
  }

  public void setFishingId(int fishingId) {
    this.fishingId = fishingId;
  }

  public FishDbModel getCaughtFish() {
    return caugthFish;
  }

  public void setCaughtFish(FishDbModel fishDbModel) {
    this.caugthFish = fishDbModel;
  }
  
  
}




Java Source Code List

com.yoandinkov.onthehook.DraughtActivity.java
com.yoandinkov.onthehook.FishActivity.java
com.yoandinkov.onthehook.FishingActivity.java
com.yoandinkov.onthehook.LoginActivity.java
com.yoandinkov.onthehook.MainMenuActivity.java
com.yoandinkov.onthehook.RegisterActivity.java
com.yoandinkov.onthehook.RequestActivity.java
com.yoandinkov.onthehook.adapters.FishAdapter.java
com.yoandinkov.onthehook.crypt.JsonConverter.java
com.yoandinkov.onthehook.crypt.SHA1Converter.java
com.yoandinkov.onthehook.db.DatabaseHelper.java
com.yoandinkov.onthehook.db.DatabaseManager.java
com.yoandinkov.onthehook.db.models.FishDbModel.java
com.yoandinkov.onthehook.models.Coordinates.java
com.yoandinkov.onthehook.models.Credentials.java
com.yoandinkov.onthehook.models.FishButton.java
com.yoandinkov.onthehook.models.FishSpecies.java
com.yoandinkov.onthehook.models.Fish.java
com.yoandinkov.onthehook.models.Fishing.java
com.yoandinkov.onthehook.models.HttpResponseWrapper.java
com.yoandinkov.onthehook.models.HttpType.java
com.yoandinkov.onthehook.models.Login.java
com.yoandinkov.onthehook.models.NewFishing.java
com.yoandinkov.onthehook.models.Registration.java
com.yoandinkov.onthehook.models.RequestPurpose.java
com.yoandinkov.onthehook.models.Response.java
com.yoandinkov.onthehook.models.WaterLocation.java