Android Open Source - AndroidWeatherBuoyDemo Buoy Detail Fragment






From Project

Back to project page AndroidWeatherBuoyDemo.

License

The source code is released under:

Apache License

If you think the Android project AndroidWeatherBuoyDemo 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.kevinrschultz.weatherbuoy.ui;
//  w w w  .ja  v  a2  s .  c  o m
import android.app.Fragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.kevinrschultz.weatherbuoy.R;
import com.kevinrschultz.weatherbuoy.customviews.compass.CompassView;
import com.kevinrschultz.weatherbuoy.model.Advisory;
import com.kevinrschultz.weatherbuoy.model.WaveCondition;
import com.kevinrschultz.weatherbuoy.model.WindCondition;
import com.kevinrschultz.weatherbuoy.preferences.WeatherBuoyPreferences;
import com.kevinrschultz.weatherbuoy.views.AdvisoryBannerView;
import com.kevinrschultz.weatherbuoy.views.InstrumentView;

/**
 * @author Kevin Schultz (kschultz@gilt.com)
 */
public class BuoyDetailFragment extends Fragment {

    // Models
    private Advisory advisory;
    private WaveCondition waves;
    private WindCondition wind;

    // View Model
    private BuoyDetailViewModel viewModel;

    // Views
    private AdvisoryBannerView advisoryBanner;
    private InstrumentView windSpeedView;
    private InstrumentView windDirectionView;
    private InstrumentView waveHeightView;
    private InstrumentView wavePeriodView;
    private CompassView compassView;

    public static BuoyDetailFragment makeBuoyDetailFragment() {
        return new BuoyDetailFragment();
    }

    public BuoyDetailFragment() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_buoy_detail, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        findViews();
        loadData();
    }

    private void findViews() {
        final View v = getView();
        advisoryBanner = AdvisoryBannerView.class.cast(v.findViewById(R.id.buoy_detail_banner));
        windSpeedView = InstrumentView.class.cast(v.findViewById(R.id.buoy_detail_wind_speed));
        windDirectionView = InstrumentView.class.cast(v.findViewById(R.id.buoy_detail_wind_direction));
        waveHeightView = InstrumentView.class.cast(v.findViewById(R.id.buoy_detail_wave_height));
        wavePeriodView = InstrumentView.class.cast(v.findViewById(R.id.buoy_detail_wave_period));
        compassView = CompassView.class.cast(v.findViewById(R.id.buoy_detail_compass));
    }

    private void loadData() {
        loadMockAdvisory.execute();
        loadMockInstruments.execute();
    }

    private void updateAdvisory(Advisory advisory) {
        this.advisory = advisory;
        advisoryBanner.setOptionalText(advisory.getAdvisory());
    }

    private void updateInstruments(WindCondition windCondition, WaveCondition waveCondition) {
        this.wind = windCondition;
        this.waves = waveCondition;
        this.viewModel = new BuoyDetailViewModel(wind, waves, new WeatherBuoyPreferences(getActivity()));
        viewModel.updateWindSpeed(windSpeedView);
        viewModel.updateWindDirection(windDirectionView, compassView);
        viewModel.updateWaveDirection(compassView);
        viewModel.updateWaveHeight(waveHeightView);
        viewModel.updateWavePeriod(wavePeriodView);
    }

    private AsyncTask<Void, Void, Void> loadMockAdvisory = new AsyncTask<Void, Void, Void>() {

        @Override
        protected Void doInBackground(Void... params) {
            // Simulate network call or loading from disk
            try {
                Thread.sleep(75);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            if(getActivity() != null) {
                advisory = new Advisory("SMALL CRAFT ADVISORY REMAINS IN EFFECT UNTIL 11 PM EDT THIS EVENING");
                updateAdvisory(advisory);
            }
        }
    };

    private AsyncTask<Void, Void, Void> loadMockInstruments = new AsyncTask<Void, Void, Void>() {

        @Override
        protected Void doInBackground(Void... params) {
            // Simulate network call or loading from disk
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            if(getActivity() != null) {
                WindCondition randomWind = new WindCondition(Math.random() * 25.0, (int) (Math.random() * 360.0));
                WaveCondition randomWave = new WaveCondition(Math.random() * 10.0, 6.0 + Math.random() * 8.0, (int) Math.random() * 360);
                updateInstruments(randomWind, randomWave);
            }
        }
    };

}




Java Source Code List

com.kevinrschultz.weatherbuoy.Constants.java
com.kevinrschultz.weatherbuoy.customviews.compass.CompassViewTest.java
com.kevinrschultz.weatherbuoy.customviews.compass.CompassView.java
com.kevinrschultz.weatherbuoy.customviews.compass.Compass.java
com.kevinrschultz.weatherbuoy.data.FakeBuoyListingGenerator.java
com.kevinrschultz.weatherbuoy.json.GsonSingleton.java
com.kevinrschultz.weatherbuoy.model.Advisory.java
com.kevinrschultz.weatherbuoy.model.BuoyDescription.java
com.kevinrschultz.weatherbuoy.model.Region.java
com.kevinrschultz.weatherbuoy.model.UnitSystem.java
com.kevinrschultz.weatherbuoy.model.WaveCondition.java
com.kevinrschultz.weatherbuoy.model.WindCondition.java
com.kevinrschultz.weatherbuoy.preferences.WeatherBuoyPreferences.java
com.kevinrschultz.weatherbuoy.sandbox.ActivityLaunchingListItem.java
com.kevinrschultz.weatherbuoy.sandbox.CompassViewActivity.java
com.kevinrschultz.weatherbuoy.sandbox.MainActivity.java
com.kevinrschultz.weatherbuoy.ui.BaseActivity.java
com.kevinrschultz.weatherbuoy.ui.BaseArrayAdapter.java
com.kevinrschultz.weatherbuoy.ui.BuoyDescriptionAdapter.java
com.kevinrschultz.weatherbuoy.ui.BuoyDetailActivity.java
com.kevinrschultz.weatherbuoy.ui.BuoyDetailFragment.java
com.kevinrschultz.weatherbuoy.ui.BuoyDetailViewModel.java
com.kevinrschultz.weatherbuoy.ui.BuoyListingActivity.java
com.kevinrschultz.weatherbuoy.ui.BuoyListingFragment.java
com.kevinrschultz.weatherbuoy.ui.BuoyListingPresenter.java
com.kevinrschultz.weatherbuoy.ui.BuoyListingView.java
com.kevinrschultz.weatherbuoy.ui.SettingsActivity.java
com.kevinrschultz.weatherbuoy.ui.SettingsFragment.java
com.kevinrschultz.weatherbuoy.util.UnitConverter.java
com.kevinrschultz.weatherbuoy.views.AdvisoryBannerView.java
com.kevinrschultz.weatherbuoy.views.InstrumentView.java
com.kevinrschultz.weatherbuoy.views.Instrument.java
com.kevinrschultz.weatherbuoy.views.OptionalTextView.java