Mobile_Application_Developm.../DailyHelper/app/src/main/java/at/fhj/swd/dailyhelper/ui/fragemnt/HomeFragment.java

142 lines
4.5 KiB
Java
Raw Normal View History

2019-05-30 17:56:39 +00:00
package at.fhj.swd.dailyhelper.ui.fragemnt;
2019-06-23 15:36:05 +00:00
import android.content.Context;
import android.content.SharedPreferences;
2019-06-24 19:28:24 +00:00
import android.content.pm.PackageManager;
import android.graphics.Color;
2019-05-30 17:56:39 +00:00
import android.os.Bundle;
2019-06-23 15:36:05 +00:00
import android.os.Handler;
import android.preference.PreferenceManager;
2019-05-30 17:56:39 +00:00
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
2019-06-24 10:51:04 +00:00
import android.support.v7.widget.CardView;
2019-05-30 17:56:39 +00:00
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
2019-06-24 19:28:24 +00:00
import android.widget.ImageView;
2019-06-23 15:36:05 +00:00
import android.widget.TextView;
2019-05-30 17:56:39 +00:00
import at.fhj.swd.dailyhelper.R;
2019-06-24 19:28:24 +00:00
import at.fhj.swd.dailyhelper.adapter.WeatherListAdapter;
2019-06-24 10:51:04 +00:00
import at.fhj.swd.dailyhelper.db.room.WeatherDB;
import at.fhj.swd.dailyhelper.model.Weather;
2019-06-24 19:28:24 +00:00
import at.fhj.swd.dailyhelper.network.DownloadImageTask;
2019-06-23 15:36:05 +00:00
import at.fhj.swd.dailyhelper.service.LocationService;
2019-05-30 17:56:39 +00:00
public class HomeFragment extends Fragment {
2019-06-23 15:36:05 +00:00
private TextView tvLongitude;
private TextView tvLatitude;
private TextView tvSpeed;
private TextView tvAltitude;
private TextView tvAccuracy;
private Context context;
private Handler handler;
2019-06-24 07:26:01 +00:00
private Runnable runnableCode;
2019-06-23 15:36:05 +00:00
private SharedPreferences preferences;
2019-06-24 10:51:04 +00:00
private View view;
2019-06-23 15:36:05 +00:00
2019-05-30 17:56:39 +00:00
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
2019-06-24 10:51:04 +00:00
view = inflater.inflate(R.layout.fragment_home, container, false);
2019-06-23 15:36:05 +00:00
context = this.getContext();
2019-06-24 19:55:01 +00:00
preferences = PreferenceManager.getDefaultSharedPreferences(this.getContext());
2019-06-23 15:36:05 +00:00
2019-06-24 19:28:24 +00:00
loadPosition();
2019-06-24 19:55:01 +00:00
loadWeather();
2019-06-24 19:28:24 +00:00
return view;
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onPause() {
handler.removeCallbacks(runnableCode);
super.onPause();
}
private void loadPosition () {
2019-06-24 19:55:01 +00:00
2019-06-23 15:36:05 +00:00
tvLongitude = view.findViewById(R.id.tv_longitude);
tvLatitude = view.findViewById(R.id.tv_latitude);
tvSpeed = view.findViewById(R.id.tv_speed);
tvAltitude = view.findViewById(R.id.tv_altitude);
tvAccuracy = view.findViewById(R.id.tv_accuracy);
// Create the Handler object (on the main thread by default)
handler = new Handler();
// Define the code block to be executed
2019-06-24 07:26:01 +00:00
runnableCode = new Runnable() {
2019-06-23 15:36:05 +00:00
@Override
public void run() {
tvLongitude.setText(context.getString(R.string.longitude) +": "+ preferences.getFloat("longitude", -1000));
tvLatitude.setText(context.getString(R.string.latitude) +": "+ preferences.getFloat("latitude", -1000));
tvSpeed.setText(context.getString(R.string.speed) +": "+ preferences.getFloat("speed", -1000));
tvAltitude.setText(context.getString(R.string.altitude) +": "+ preferences.getFloat("altitude", -1000));
tvAccuracy.setText(context.getString(R.string.accuracy) +": "+ preferences.getFloat("accuracy", -1000));
2019-06-24 19:28:24 +00:00
2019-06-23 15:36:05 +00:00
handler.postDelayed(this, 2000);
}
};
// Start the initial runnable task by posting through the handler
handler.post(runnableCode);
2019-05-30 17:56:39 +00:00
}
2019-06-24 07:26:01 +00:00
2019-06-24 19:55:01 +00:00
private void loadWeather (){
2019-06-24 10:51:04 +00:00
Weather weather;
2019-06-24 19:28:24 +00:00
CardView cvCardView;
2019-06-24 10:51:04 +00:00
TextView tvDate;
2019-06-24 19:28:24 +00:00
ImageView imageView;
2019-06-24 10:51:04 +00:00
TextView tvDescription;
TextView tvTemperature;
TextView tvHumity;
2019-06-24 19:28:24 +00:00
cvCardView = view.findViewById(R.id.incWeather);
2019-06-24 10:51:04 +00:00
tvDate = view.findViewById(R.id.tvDate);
2019-06-24 19:28:24 +00:00
imageView = view.findViewById(R.id.card_view_image);
2019-06-24 10:51:04 +00:00
tvDescription = view.findViewById(R.id.tvDescription);
tvTemperature = view.findViewById(R.id.tvTemperature);
tvHumity = view.findViewById(R.id.tvHumity);
weather = WeatherDB.getDatabase(getContext()).weatherDAO().getFirst();
if (weather != null) {
2019-06-24 19:28:24 +00:00
cvCardView.setCardElevation(0);
2019-06-24 10:51:04 +00:00
tvDate.setText(weather.getDate());
2019-06-24 19:28:24 +00:00
new DownloadImageTask(imageView).execute("https://openweathermap.org/img/w/" + weather.getIcon() + ".png");
2019-06-24 10:51:04 +00:00
tvDescription.setText(weather.getDescription());
tvTemperature.setText(context.getString(R.string.temperature) + ": " + weather.getTemp() + "°C");
tvHumity.setText(context.getString(R.string.humidity) + ": " + weather.getHumidity() + "%");
}
}
2019-06-24 19:28:24 +00:00
public void refresh(){
loadPosition();
2019-06-24 19:55:01 +00:00
loadWeather();
2019-06-24 19:28:24 +00:00
}
2019-05-30 17:56:39 +00:00
}