This commit is contained in:
Matthias Schreiner 2019-06-24 22:28:24 +03:00
parent 2f13747b8a
commit 9e064dd64e
10 changed files with 133 additions and 78 deletions

View File

@ -24,10 +24,9 @@ import java.util.List;
import at.fhj.swd.dailyhelper.R; import at.fhj.swd.dailyhelper.R;
import at.fhj.swd.dailyhelper.model.Weather; import at.fhj.swd.dailyhelper.model.Weather;
import at.fhj.swd.dailyhelper.network.DownloadImageTask;
public class WeatherListAdapter extends RecyclerView.Adapter<WeatherListAdapter.WeatherItemViewHolder> { public class WeatherListAdapter extends RecyclerView.Adapter<WeatherListAdapter.WeatherItemViewHolder> {
private ImageView imageView;
private List<Weather> weathers; private List<Weather> weathers;
@ -58,7 +57,7 @@ public class WeatherListAdapter extends RecyclerView.Adapter<WeatherListAdapter.
weatherItemViewHolder.tvDate.setText(weathers.get(i).getDate()); weatherItemViewHolder.tvDate.setText(weathers.get(i).getDate());
String icon = weathers.get(i).getIcon(); String icon = weathers.get(i).getIcon();
new DownloadImageTask(imageView).execute("https://openweathermap.org/img/w/" + icon + ".png"); new DownloadImageTask(weatherItemViewHolder.imageView).execute("https://openweathermap.org/img/w/" + icon + ".png");
weatherItemViewHolder.tvDescription.setText(weathers.get(i).getDescription()); weatherItemViewHolder.tvDescription.setText(weathers.get(i).getDescription());
@ -79,6 +78,7 @@ public class WeatherListAdapter extends RecyclerView.Adapter<WeatherListAdapter.
CardView cvWeatherItem; CardView cvWeatherItem;
TextView tvDate; TextView tvDate;
ImageView imageView;
TextView tvDescription; TextView tvDescription;
TextView tvTemperature; TextView tvTemperature;
@ -106,29 +106,4 @@ public class WeatherListAdapter extends RecyclerView.Adapter<WeatherListAdapter.
void onItemClicked(Weather weather, int position); void onItemClicked(Weather weather, int position);
} }
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap bmp = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
bmp = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return bmp;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
} }

View File

@ -0,0 +1,32 @@
package at.fhj.swd.dailyhelper.network;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ImageView;
import java.io.InputStream;
public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap bmp = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
bmp = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return bmp;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}

View File

@ -53,6 +53,7 @@ public class LocationService extends Service {
.putFloat("speed", locationResult.getLocations().get(0).getSpeed()).apply(); .putFloat("speed", locationResult.getLocations().get(0).getSpeed()).apply();
} }
} }
}; };

View File

@ -44,8 +44,9 @@ public class MainActivity extends AppCompatActivity
private ProgressDialog progressDialog; private ProgressDialog progressDialog;
private SharedPreferences preferences; private SharedPreferences preferences;
private WeatherFragment weatherFragment; private WeatherFragment weatherFragment;
private HomeFragment homeFragment;
private WeatherDB database; private WeatherDB database;
private NavigationView navigationView;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -56,6 +57,7 @@ public class MainActivity extends AppCompatActivity
setSupportActionBar(toolbar); setSupportActionBar(toolbar);
weatherFragment = new WeatherFragment(); weatherFragment = new WeatherFragment();
homeFragment = new HomeFragment();
progressDialog = new ProgressDialog(this); progressDialog = new ProgressDialog(this);
preferences = PreferenceManager.getDefaultSharedPreferences(this); preferences = PreferenceManager.getDefaultSharedPreferences(this);
@ -64,7 +66,7 @@ public class MainActivity extends AppCompatActivity
drawer = findViewById(R.id.drawer_layout); drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view); navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this); navigationView.setNavigationItemSelectedListener(this);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
@ -73,10 +75,13 @@ public class MainActivity extends AppCompatActivity
toggle.syncState(); toggle.syncState();
if(savedInstanceState == null) { if(savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new HomeFragment()).commit(); getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,homeFragment).commit();
navigationView.setCheckedItem(R.id.nav_home); navigationView.setCheckedItem(R.id.nav_home);
} }
startLocationService();
fetchWeather();
} }
@ -105,6 +110,8 @@ public class MainActivity extends AppCompatActivity
if(requestCode == 12435){ if(requestCode == 12435){
if(grantResults[0] == PackageManager.PERMISSION_GRANTED){ if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
startLocationService(); startLocationService();
fetchWeather();
updateFragments();
} }
} }
} }
@ -127,7 +134,7 @@ public class MainActivity extends AppCompatActivity
switch (item.getItemId()){ switch (item.getItemId()){
case R.id.nav_home: case R.id.nav_home:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new HomeFragment()).commit(); getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, homeFragment).commit();
break; break;
case R.id.nav_map: case R.id.nav_map:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new MapFragment()).commit(); getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new MapFragment()).commit();
@ -155,7 +162,7 @@ public class MainActivity extends AppCompatActivity
return true; return true;
case R.id.action_refresh: case R.id.action_refresh:
// fetchLocations(); fetchWeather();
return true; return true;
@ -168,7 +175,10 @@ public class MainActivity extends AppCompatActivity
private void updateFragments() { private void updateFragments() {
runOnUiThread(() -> { runOnUiThread(() -> {
weatherFragment.refresh(); weatherFragment.refresh();
homeFragment.refresh();
}); });
} }
private void dismissProgressDialog() { private void dismissProgressDialog() {
@ -176,14 +186,14 @@ public class MainActivity extends AppCompatActivity
} }
private void fetchWeather() { public void fetchWeather() {
HttpsGetTask httpsGetTask = new HttpsGetTask(this); HttpsGetTask httpsGetTask = new HttpsGetTask(this);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
float latitude = preferences.getFloat("latitude", -1000); float latitude = preferences.getFloat("latitude", -1000);
float longitude = preferences.getFloat("longitude", -1000); float longitude = preferences.getFloat("longitude", -1000);
if (latitude == -1000 || longitude == -1000) { if (latitude == -1000 || longitude == -1000) {
httpsGetTask.execute("https://api.openaq.org/v1/locations?country=AT&limit=200"); httpsGetTask.execute("https://api.openweathermap.org/data/2.5/forecast?q=Vienna&limit=10&appid=030cadd0e9d6bdb9ae54ff0f841258ea&lang=de&units=metric");
} else { } else {
httpsGetTask.execute("https://api.openweathermap.org/data/2.5/forecast?lat="+ latitude +"&lon=" + longitude + "&appid=030cadd0e9d6bdb9ae54ff0f841258ea&lang=de&units=metric"); httpsGetTask.execute("https://api.openweathermap.org/data/2.5/forecast?lat="+ latitude +"&lon=" + longitude + "&appid=030cadd0e9d6bdb9ae54ff0f841258ea&lang=de&units=metric");
} }
@ -217,8 +227,6 @@ public class MainActivity extends AppCompatActivity
//notify fragments //notify fragments
dismissProgressDialog(); dismissProgressDialog();
//TODO
updateFragments();
}).start(); }).start();

View File

@ -2,6 +2,8 @@ package at.fhj.swd.dailyhelper.ui.fragemnt;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
@ -12,11 +14,14 @@ import android.support.v7.widget.CardView;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import at.fhj.swd.dailyhelper.R; import at.fhj.swd.dailyhelper.R;
import at.fhj.swd.dailyhelper.adapter.WeatherListAdapter;
import at.fhj.swd.dailyhelper.db.room.WeatherDB; import at.fhj.swd.dailyhelper.db.room.WeatherDB;
import at.fhj.swd.dailyhelper.model.Weather; import at.fhj.swd.dailyhelper.model.Weather;
import at.fhj.swd.dailyhelper.network.DownloadImageTask;
import at.fhj.swd.dailyhelper.service.LocationService; import at.fhj.swd.dailyhelper.service.LocationService;
public class HomeFragment extends Fragment { public class HomeFragment extends Fragment {
@ -40,6 +45,27 @@ public class HomeFragment extends Fragment {
view = inflater.inflate(R.layout.fragment_home, container, false); view = inflater.inflate(R.layout.fragment_home, container, false);
context = this.getContext(); context = this.getContext();
loadPosition();
loadWether();
return view;
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onPause() {
handler.removeCallbacks(runnableCode);
super.onPause();
}
private void loadPosition () {
preferences = PreferenceManager.getDefaultSharedPreferences(this.getContext()); preferences = PreferenceManager.getDefaultSharedPreferences(this.getContext());
tvLongitude = view.findViewById(R.id.tv_longitude); tvLongitude = view.findViewById(R.id.tv_longitude);
@ -62,40 +88,28 @@ public class HomeFragment extends Fragment {
tvAltitude.setText(context.getString(R.string.altitude) +": "+ preferences.getFloat("altitude", -1000)); tvAltitude.setText(context.getString(R.string.altitude) +": "+ preferences.getFloat("altitude", -1000));
tvAccuracy.setText(context.getString(R.string.accuracy) +": "+ preferences.getFloat("accuracy", -1000)); tvAccuracy.setText(context.getString(R.string.accuracy) +": "+ preferences.getFloat("accuracy", -1000));
handler.postDelayed(this, 2000); handler.postDelayed(this, 2000);
} }
}; };
// Start the initial runnable task by posting through the handler // Start the initial runnable task by posting through the handler
handler.post(runnableCode); handler.post(runnableCode);
getWeather();
return view;
} }
@Override private void loadWether (){
public void onResume() {
super.onResume();
}
@Override
public void onPause() {
handler.removeCallbacks(runnableCode);
super.onPause();
}
private void getWeather (){
Weather weather; Weather weather;
CardView cvCardView;
TextView tvDate; TextView tvDate;
ImageView imageView;
TextView tvDescription; TextView tvDescription;
TextView tvTemperature; TextView tvTemperature;
TextView tvHumity; TextView tvHumity;
cvCardView = view.findViewById(R.id.incWeather);
tvDate = view.findViewById(R.id.tvDate); tvDate = view.findViewById(R.id.tvDate);
imageView = view.findViewById(R.id.card_view_image);
tvDescription = view.findViewById(R.id.tvDescription); tvDescription = view.findViewById(R.id.tvDescription);
tvTemperature = view.findViewById(R.id.tvTemperature); tvTemperature = view.findViewById(R.id.tvTemperature);
tvHumity = view.findViewById(R.id.tvHumity); tvHumity = view.findViewById(R.id.tvHumity);
@ -103,11 +117,24 @@ public class HomeFragment extends Fragment {
weather = WeatherDB.getDatabase(getContext()).weatherDAO().getFirst(); weather = WeatherDB.getDatabase(getContext()).weatherDAO().getFirst();
if (weather != null) { if (weather != null) {
cvCardView.setCardElevation(0);
tvDate.setText(weather.getDate()); tvDate.setText(weather.getDate());
new DownloadImageTask(imageView).execute("https://openweathermap.org/img/w/" + weather.getIcon() + ".png");
tvDescription.setText(weather.getDescription()); tvDescription.setText(weather.getDescription());
tvTemperature.setText(context.getString(R.string.temperature) + ": " + weather.getTemp() + "°C"); tvTemperature.setText(context.getString(R.string.temperature) + ": " + weather.getTemp() + "°C");
tvHumity.setText(context.getString(R.string.humidity) + ": " + weather.getHumidity() + "%"); tvHumity.setText(context.getString(R.string.humidity) + ": " + weather.getHumidity() + "%");
} }
} }
public void refresh(){
loadPosition();
loadWether();
}
} }

View File

@ -44,7 +44,9 @@ public class WeatherFragment extends Fragment implements WeatherListAdapter.Item
private void fetchLoctions() { private void fetchLoctions() {
WeatherListAdapter adapter = new WeatherListAdapter(WeatherDB.getDatabase(getContext()).weatherDAO().getAll(), this); WeatherListAdapter adapter = new WeatherListAdapter(WeatherDB.getDatabase(getContext()).weatherDAO().getAll(), this);
weatherList.setAdapter(adapter); if (weatherList != null) {
weatherList.setAdapter(adapter);
}
} }

View File

@ -1,7 +1,9 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" <shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"> android:shape="rectangle">
<solid android:color="@android:color/white" />
<stroke <stroke
android:width="5dip" android:width="5dip"
android:color="@android:color/white" /> android:color="@android:color/darker_gray" />
</shape> </shape>

View File

@ -2,8 +2,7 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical" android:orientation="vertical">
android:background="@android:color/holo_blue_light">
@ -75,7 +74,7 @@
<TextView <TextView
android:id="@+id/tv_wheather" android:id="@+id/tv_wheather"
android:text="Wetter" android:text="@string/actual_weather"
android:padding="10dp" android:padding="10dp"
android:textSize="25sp" android:textSize="25sp"
android:layout_width="wrap_content" android:layout_width="wrap_content"
@ -84,7 +83,8 @@
<include android:id="@+id/incWeather" <include android:id="@+id/incWeather"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
layout="@layout/weather_list_item"/> layout="@layout/weather_list_item"
/>
</LinearLayout> </LinearLayout>

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/cvWeatherItem" android:id="@+id/cvWeatherItem"
android:foreground="?attr/selectableItemBackground" android:foreground="?attr/selectableItemBackground"
android:layout_margin="5dp" android:layout_margin="5dp"
@ -11,29 +12,37 @@
<LinearLayout <LinearLayout
android:orientation="vertical" android:orientation="vertical"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content"> android:layout_height="wrap_content"
android:padding="5dp">
<LinearLayout
<TextView
android:id="@+id/tvDate"
android:text="Date: "
android:padding="10dp"
android:textSize="25sp"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" /> android:layout_height="match_parent"
android:gravity="center_horizontal"
>
<ImageView <ImageView
android:id="@+id/card_view_image" android:id="@+id/card_view_image"
android:layout_width="100dp" android:layout_width="70dp"
android:layout_height="100dp" android:layout_height="70dp"
android:scaleType="fitXY" /> android:scaleType="fitXY" />
<TextView
android:id="@+id/tvDate"
android:text="Date: "
android:padding="15dp"
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"/>
</LinearLayout>
<TextView <TextView
android:id="@+id/tvDescription" android:id="@+id/tvDescription"
android:text="Description:" android:text="Description:"
android:padding="5dp"
android:textSize="15sp" android:textSize="15sp"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" /> android:layout_height="wrap_content" />
@ -42,7 +51,6 @@
<TextView <TextView
android:id="@+id/tvTemperature" android:id="@+id/tvTemperature"
android:text="Temp:" android:text="Temp:"
android:padding="4dp"
android:textSize="15sp" android:textSize="15sp"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" /> android:layout_height="wrap_content" />
@ -50,7 +58,6 @@
<TextView <TextView
android:id="@+id/tvHumity" android:id="@+id/tvHumity"
android:text="Hum:" android:text="Hum:"
android:padding="4dp"
android:textSize="15sp" android:textSize="15sp"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" /> android:layout_height="wrap_content" />

View File

@ -24,5 +24,6 @@
<string name="position">Position:</string> <string name="position">Position:</string>
<string name="temperature">Temp:</string> <string name="temperature">Temp:</string>
<string name="humidity">Feuchtigkeit:</string> <string name="humidity">Feuchtigkeit:</string>
<string name="actual_weather">Aktuelles Wetter:</string>
</resources> </resources>