kopia lustrzana https://github.com/SP8EBC/MeteoSystem
prototyping event bus
rodzic
8629960d88
commit
e460611820
|
@ -54,6 +54,6 @@ dependencies {
|
|||
implementation 'com.squareup.okhttp3:okhttp:3.14.7'
|
||||
implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
|
||||
implementation 'com.jakewharton.threetenabp:threetenabp:1.2.1'
|
||||
|
||||
implementation 'org.greenrobot:eventbus:3.0.0'
|
||||
|
||||
}
|
|
@ -5,27 +5,28 @@ import androidx.appcompat.app.AppCompatActivity;
|
|||
import android.content.Context;
|
||||
import android.content.res.Configuration;
|
||||
import android.content.res.Resources;
|
||||
import android.media.Image;
|
||||
import android.os.Bundle;
|
||||
import android.view.Menu;
|
||||
import android.widget.ImageButton;
|
||||
|
||||
import com.jakewharton.threetenabp.AndroidThreeTen;
|
||||
|
||||
import java.io.File;
|
||||
import org.greenrobot.eventbus.EventBus;
|
||||
import org.greenrobot.eventbus.Subscribe;
|
||||
import org.greenrobot.eventbus.ThreadMode;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
import cc.pogoda.mobile.pogodacc.R;
|
||||
import cc.pogoda.mobile.pogodacc.activity.handler.MainActImageButtonAllStationsClickEvent;
|
||||
import cc.pogoda.mobile.pogodacc.activity.handler.MainActImageButtonFavouritesClickEvent;
|
||||
import cc.pogoda.mobile.pogodacc.config.AppConfiguration;
|
||||
import cc.pogoda.mobile.pogodacc.dao.AllStationsDao;
|
||||
import cc.pogoda.mobile.pogodacc.file.FavouritiesFile;
|
||||
import cc.pogoda.mobile.pogodacc.file.FileNames;
|
||||
import cc.pogoda.mobile.pogodacc.type.Favourites;
|
||||
import cc.pogoda.mobile.pogodacc.type.ParceableStationsList;
|
||||
import cc.pogoda.mobile.pogodacc.type.WeatherStation;
|
||||
import cc.pogoda.mobile.pogodacc.type.WeatherStationListEvent;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
|
||||
|
@ -35,19 +36,78 @@ public class MainActivity extends AppCompatActivity {
|
|||
|
||||
private List<WeatherStation> listOfAllStations;
|
||||
|
||||
List<WeatherStation> favs;
|
||||
|
||||
private ParceableStationsList parceableListOfAllStations;
|
||||
|
||||
private ParceableStationsList parceableListOfFavStations;
|
||||
|
||||
private MainActImageButtonFavouritesClickEvent mainActImageButtonFavouritesClickEvent = null;
|
||||
|
||||
private ImageButton imageButtonFavourites;
|
||||
|
||||
public MainActivity() {
|
||||
|
||||
//AppConfiguration.favourites = new Favourites(fileNames);
|
||||
}
|
||||
|
||||
private void recreateListOfFavs() {
|
||||
|
||||
// check if this is a first call after application start
|
||||
if (favs == null) {
|
||||
favs = new FavouritiesFile(fileNames).loadFavourites();
|
||||
}
|
||||
|
||||
// update values for the fav list with listOfAllStations
|
||||
for (WeatherStation f : favs) {
|
||||
|
||||
// find an index of updated station
|
||||
int idx = listOfAllStations.indexOf(f);
|
||||
|
||||
// get the station
|
||||
WeatherStation fromAllStations = listOfAllStations.get(idx);
|
||||
|
||||
// update all parameters
|
||||
f.setAvailableParameters(fromAllStations.getAvailableParameters());
|
||||
f.setMoreInfo(fromAllStations.getMoreInfo());
|
||||
f.setImageAlign(fromAllStations.getImageAlign());
|
||||
f.setImageUrl(fromAllStations.getImageUrl());
|
||||
f.setSponsorUrl(fromAllStations.getSponsorUrl());
|
||||
f.setMoreInfo(fromAllStations.getMoreInfo());
|
||||
}
|
||||
|
||||
parceableListOfFavStations = ParceableStationsList.createFromStdList(favs);
|
||||
|
||||
// create an event handler fired when a user click 'favourites' button
|
||||
mainActImageButtonFavouritesClickEvent = new MainActImageButtonFavouritesClickEvent(this, parceableListOfFavStations);
|
||||
|
||||
// assign on click listener
|
||||
if (imageButtonFavourites != null) {
|
||||
imageButtonFavourites.setOnClickListener(mainActImageButtonFavouritesClickEvent);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
EventBus.getDefault().unregister(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when a user goes back to the main screen
|
||||
*/
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
recreateListOfFavs();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
// register to Event bus to receive events when a station is added od removed from favourites
|
||||
EventBus.getDefault().register(this);
|
||||
|
||||
AndroidThreeTen.init(this);
|
||||
|
||||
Locale locale = new Locale("pl");
|
||||
|
@ -63,20 +123,20 @@ public class MainActivity extends AppCompatActivity {
|
|||
|
||||
fileNames = new FileNames(baseContext);
|
||||
|
||||
// download all stations from API
|
||||
listOfAllStations = new AllStationsDao().getAllStations();
|
||||
|
||||
// convert this to parceable to exchange across intents
|
||||
parceableListOfAllStations = ParceableStationsList.createFromStdList(listOfAllStations);
|
||||
|
||||
List<WeatherStation> favs = new FavouritiesFile(fileNames).loadFavourites();
|
||||
|
||||
ImageButton imageButtonAllStations = (ImageButton)findViewById(R.id.imageButtonAllStations);
|
||||
if (imageButtonAllStations != null)
|
||||
imageButtonAllStations.setOnClickListener(new MainActImageButtonAllStationsClickEvent(this, parceableListOfAllStations));
|
||||
|
||||
ImageButton imageButtonFavourites = (ImageButton)findViewById(R.id.imageButtonFavourites);
|
||||
if (imageButtonFavourites != null) {
|
||||
imageButtonFavourites.setOnClickListener(new MainActImageButtonFavouritesClickEvent(this, parceableListOfFavStations));
|
||||
}
|
||||
imageButtonFavourites = (ImageButton)findViewById(R.id.imageButtonFavourites);
|
||||
|
||||
// recreate list of favorites
|
||||
recreateListOfFavs();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -85,4 +145,22 @@ public class MainActivity extends AppCompatActivity {
|
|||
return true;
|
||||
}
|
||||
|
||||
@Subscribe(threadMode = ThreadMode.MAIN)
|
||||
public void weatherStationListHandler(WeatherStationListEvent serviceEvent) {
|
||||
System.out.println(serviceEvent.toString());
|
||||
|
||||
switch (serviceEvent.getEventReason()) {
|
||||
|
||||
case ADD:
|
||||
favs.add(serviceEvent.getStation());
|
||||
break;
|
||||
case DELETE:
|
||||
favs.remove(serviceEvent.getStation());
|
||||
break;
|
||||
}
|
||||
|
||||
recreateListOfFavs();
|
||||
//Toast.makeText(this, intentServiceResult.getResultValue(), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
}
|
|
@ -22,6 +22,8 @@ import android.widget.ListAdapter;
|
|||
import android.widget.RadioGroup;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.greenrobot.eventbus.EventBus;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
import cc.pogoda.mobile.pogodacc.R;
|
||||
|
@ -33,6 +35,7 @@ import cc.pogoda.mobile.pogodacc.activity.handler.StationDetailsActSummaryButton
|
|||
import cc.pogoda.mobile.pogodacc.activity.handler.StationDetailsActWindRoseButtonClickEvent;
|
||||
import cc.pogoda.mobile.pogodacc.config.AppConfiguration;
|
||||
import cc.pogoda.mobile.pogodacc.type.WeatherStation;
|
||||
import cc.pogoda.mobile.pogodacc.type.WeatherStationListEvent;
|
||||
|
||||
public class StationDetailsActivity extends AppCompatActivity {
|
||||
|
||||
|
@ -117,6 +120,8 @@ public class StationDetailsActivity extends AppCompatActivity {
|
|||
stationName = null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
getMenuInflater().inflate(R.menu.menu_station_details, menu);
|
||||
|
@ -132,7 +137,8 @@ public class StationDetailsActivity extends AppCompatActivity {
|
|||
if (station != null) {
|
||||
boolean result = false;
|
||||
|
||||
result = AppConfiguration.favourites.addFav(station);
|
||||
//result = AppConfiguration.favourites.addFav(station);
|
||||
EventBus.getDefault().post(new WeatherStationListEvent(station, WeatherStationListEvent.EventReason.ADD));
|
||||
|
||||
if (result) {
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
||||
|
@ -149,7 +155,8 @@ public class StationDetailsActivity extends AppCompatActivity {
|
|||
if (station != null) {
|
||||
boolean result = false;
|
||||
|
||||
result = AppConfiguration.favourites.removeFav(station);
|
||||
//result = AppConfiguration.favourites.removeFav(station);
|
||||
EventBus.getDefault().post(new WeatherStationListEvent(station, WeatherStationListEvent.EventReason.DELETE));
|
||||
|
||||
if (result) {
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
||||
|
|
|
@ -18,6 +18,7 @@ public class MainActImageButtonFavouritesClickEvent implements View.OnClickListe
|
|||
this.parent = parent;
|
||||
|
||||
intent = new Intent(this.parent, FavouritesActivity.class);
|
||||
intent.putExtra("favs", favs);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package cc.pogoda.mobile.pogodacc.file;
|
||||
|
||||
import com.google.gson.JsonParser;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
@ -10,7 +12,6 @@ import java.io.FileNotFoundException;
|
|||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
|
@ -38,6 +39,17 @@ public class FavouritiesFile {
|
|||
// create a place fo JSON content
|
||||
char buffer[] = new char[(int) file.length()];
|
||||
|
||||
try {
|
||||
streamReader.read(buffer);
|
||||
|
||||
streamReader.close();
|
||||
|
||||
JSONObject root = new JSONObject(new String(buffer));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (JSONException e) {
|
||||
}
|
||||
|
||||
} catch (FileNotFoundException e) {
|
||||
return null;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
package cc.pogoda.mobile.pogodacc.type;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
public class WeatherStationListEvent {
|
||||
|
||||
public enum EventReason {
|
||||
ADD,
|
||||
DELETE;
|
||||
}
|
||||
|
||||
WeatherStation station;
|
||||
|
||||
EventReason eventReason;
|
||||
|
||||
public WeatherStationListEvent(WeatherStation wx, EventReason reason) {
|
||||
station = wx;
|
||||
eventReason = reason;
|
||||
}
|
||||
|
||||
public WeatherStation getStation() {
|
||||
return station;
|
||||
}
|
||||
|
||||
public EventReason getEventReason() {
|
||||
return eventReason;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public String toString() {
|
||||
return "[station.systemName = " + station.systemName + "][eventReason = " + eventReason + "]";
|
||||
}
|
||||
}
|
Ładowanie…
Reference in New Issue