kopia lustrzana https://github.com/SP8EBC/MeteoSystem
saving restoring favourites list
rodzic
e460611820
commit
2f1652a9fb
|
@ -11,11 +11,14 @@ import androidx.recyclerview.widget.RecyclerView;
|
||||||
import cc.pogoda.mobile.pogodacc.R;
|
import cc.pogoda.mobile.pogodacc.R;
|
||||||
import cc.pogoda.mobile.pogodacc.adapter.WeatherStationRecyclerViewAdapter;
|
import cc.pogoda.mobile.pogodacc.adapter.WeatherStationRecyclerViewAdapter;
|
||||||
import cc.pogoda.mobile.pogodacc.config.AppConfiguration;
|
import cc.pogoda.mobile.pogodacc.config.AppConfiguration;
|
||||||
|
import cc.pogoda.mobile.pogodacc.type.ParceableStationsList;
|
||||||
|
|
||||||
public class FavouritesActivity extends AppCompatActivity {
|
public class FavouritesActivity extends AppCompatActivity {
|
||||||
|
|
||||||
RecyclerView recyclerViewFavourites;
|
RecyclerView recyclerViewFavourites;
|
||||||
|
|
||||||
|
ParceableStationsList favourites;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCreateOptionsMenu(Menu menu) {
|
public boolean onCreateOptionsMenu(Menu menu) {
|
||||||
|
|
||||||
|
@ -28,7 +31,9 @@ public class FavouritesActivity extends AppCompatActivity {
|
||||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
if (AppConfiguration.favourites == null || AppConfiguration.favourites.getFavourites().size() == 0) {
|
favourites = getIntent().getParcelableExtra("favs");
|
||||||
|
|
||||||
|
if (favourites == null || favourites.getList().size() == 0) {
|
||||||
setContentView(R.layout.activity_favourites_empty);
|
setContentView(R.layout.activity_favourites_empty);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -39,7 +44,7 @@ public class FavouritesActivity extends AppCompatActivity {
|
||||||
if (recyclerViewFavourites != null) {
|
if (recyclerViewFavourites != null) {
|
||||||
WeatherStationRecyclerViewAdapter adapter = null;
|
WeatherStationRecyclerViewAdapter adapter = null;
|
||||||
|
|
||||||
adapter = new WeatherStationRecyclerViewAdapter(AppConfiguration.favourites.getFavourites(), this);
|
adapter = new WeatherStationRecyclerViewAdapter(favourites.getList(), this);
|
||||||
|
|
||||||
recyclerViewFavourites.setAdapter(adapter);
|
recyclerViewFavourites.setAdapter(adapter);
|
||||||
|
|
||||||
|
|
|
@ -15,6 +15,8 @@ import org.greenrobot.eventbus.EventBus;
|
||||||
import org.greenrobot.eventbus.Subscribe;
|
import org.greenrobot.eventbus.Subscribe;
|
||||||
import org.greenrobot.eventbus.ThreadMode;
|
import org.greenrobot.eventbus.ThreadMode;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
|
@ -34,6 +36,8 @@ public class MainActivity extends AppCompatActivity {
|
||||||
|
|
||||||
private FileNames fileNames;
|
private FileNames fileNames;
|
||||||
|
|
||||||
|
private FavouritiesFile favouritiesFile;
|
||||||
|
|
||||||
private List<WeatherStation> listOfAllStations;
|
private List<WeatherStation> listOfAllStations;
|
||||||
|
|
||||||
List<WeatherStation> favs;
|
List<WeatherStation> favs;
|
||||||
|
@ -54,25 +58,46 @@ public class MainActivity extends AppCompatActivity {
|
||||||
|
|
||||||
// check if this is a first call after application start
|
// check if this is a first call after application start
|
||||||
if (favs == null) {
|
if (favs == null) {
|
||||||
favs = new FavouritiesFile(fileNames).loadFavourites();
|
favs = favouritiesFile.loadFavourites();
|
||||||
}
|
}
|
||||||
|
|
||||||
// update values for the fav list with listOfAllStations
|
// if favs is still null it means that favourites file doesn't even exists
|
||||||
for (WeatherStation f : favs) {
|
// so and user hasn't added any station to it yet
|
||||||
|
if (favs == null) {
|
||||||
|
favs = new ArrayList<>();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// update values for the fav list with listOfAllStations
|
||||||
|
//for (WeatherStation f : favs) {
|
||||||
|
for (int i = 0; i < favs.size(); i++) {
|
||||||
|
|
||||||
// find an index of updated station
|
//
|
||||||
int idx = listOfAllStations.indexOf(f);
|
WeatherStation fromFavs = favs.get(i);
|
||||||
|
|
||||||
// get the station
|
// find an index of updated station
|
||||||
WeatherStation fromAllStations = listOfAllStations.get(idx);
|
int idx = listOfAllStations.indexOf(fromFavs);
|
||||||
|
|
||||||
// update all parameters
|
// get the station
|
||||||
f.setAvailableParameters(fromAllStations.getAvailableParameters());
|
WeatherStation fromAllStations = listOfAllStations.get(idx);
|
||||||
f.setMoreInfo(fromAllStations.getMoreInfo());
|
|
||||||
f.setImageAlign(fromAllStations.getImageAlign());
|
// update all parameters
|
||||||
f.setImageUrl(fromAllStations.getImageUrl());
|
fromFavs.setAvailableParameters(fromAllStations.getAvailableParameters());
|
||||||
f.setSponsorUrl(fromAllStations.getSponsorUrl());
|
fromFavs.setMoreInfo(fromAllStations.getMoreInfo());
|
||||||
f.setMoreInfo(fromAllStations.getMoreInfo());
|
fromFavs.setImageAlign(fromAllStations.getImageAlign());
|
||||||
|
fromFavs.setImageUrl(fromAllStations.getImageUrl());
|
||||||
|
fromFavs.setSponsorUrl(fromAllStations.getSponsorUrl());
|
||||||
|
fromFavs.setMoreInfo(fromAllStations.getMoreInfo());
|
||||||
|
fromFavs.setLon(fromAllStations.getLon());
|
||||||
|
fromFavs.setLat(fromAllStations.getLat());
|
||||||
|
fromFavs.setDisplayedName(fromAllStations.getDisplayedName());
|
||||||
|
fromFavs.setDisplayedLocation(fromAllStations.getDisplayedLocation());
|
||||||
|
|
||||||
|
// there is no need to delete and put object on the list once again
|
||||||
|
// as a list does not make a copy of the object. It (ArrayList) keeps
|
||||||
|
// only a reference to an object
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
parceableListOfFavStations = ParceableStationsList.createFromStdList(favs);
|
parceableListOfFavStations = ParceableStationsList.createFromStdList(favs);
|
||||||
|
@ -123,6 +148,8 @@ public class MainActivity extends AppCompatActivity {
|
||||||
|
|
||||||
fileNames = new FileNames(baseContext);
|
fileNames = new FileNames(baseContext);
|
||||||
|
|
||||||
|
favouritiesFile = new FavouritiesFile(fileNames);
|
||||||
|
|
||||||
// download all stations from API
|
// download all stations from API
|
||||||
listOfAllStations = new AllStationsDao().getAllStations();
|
listOfAllStations = new AllStationsDao().getAllStations();
|
||||||
|
|
||||||
|
@ -152,13 +179,28 @@ public class MainActivity extends AppCompatActivity {
|
||||||
switch (serviceEvent.getEventReason()) {
|
switch (serviceEvent.getEventReason()) {
|
||||||
|
|
||||||
case ADD:
|
case ADD:
|
||||||
|
// check of list consist this station
|
||||||
|
if (favs.contains(serviceEvent.getStation())) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// add favourites to list
|
||||||
favs.add(serviceEvent.getStation());
|
favs.add(serviceEvent.getStation());
|
||||||
|
|
||||||
|
try {
|
||||||
|
// save the list into JSON file
|
||||||
|
favouritiesFile.persistFavourities(favs);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case DELETE:
|
case DELETE:
|
||||||
favs.remove(serviceEvent.getStation());
|
favs.remove(serviceEvent.getStation());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// recreate parceable object and pass it everywhere
|
||||||
recreateListOfFavs();
|
recreateListOfFavs();
|
||||||
//Toast.makeText(this, intentServiceResult.getResultValue(), Toast.LENGTH_SHORT).show();
|
//Toast.makeText(this, intentServiceResult.getResultValue(), Toast.LENGTH_SHORT).show();
|
||||||
}
|
}
|
||||||
|
|
|
@ -135,9 +135,8 @@ public class StationDetailsActivity extends AppCompatActivity {
|
||||||
switch (item.getItemId()) {
|
switch (item.getItemId()) {
|
||||||
case R.id.menuItemStationDetailsAddFavourites:
|
case R.id.menuItemStationDetailsAddFavourites:
|
||||||
if (station != null) {
|
if (station != null) {
|
||||||
boolean result = false;
|
boolean result = true;
|
||||||
|
|
||||||
//result = AppConfiguration.favourites.addFav(station);
|
|
||||||
EventBus.getDefault().post(new WeatherStationListEvent(station, WeatherStationListEvent.EventReason.ADD));
|
EventBus.getDefault().post(new WeatherStationListEvent(station, WeatherStationListEvent.EventReason.ADD));
|
||||||
|
|
||||||
if (result) {
|
if (result) {
|
||||||
|
@ -153,7 +152,7 @@ public class StationDetailsActivity extends AppCompatActivity {
|
||||||
break;
|
break;
|
||||||
case R.id.menuItemStationDetailsDeleteFavourites:
|
case R.id.menuItemStationDetailsDeleteFavourites:
|
||||||
if (station != null) {
|
if (station != null) {
|
||||||
boolean result = false;
|
boolean result = true;
|
||||||
|
|
||||||
//result = AppConfiguration.favourites.removeFav(station);
|
//result = AppConfiguration.favourites.removeFav(station);
|
||||||
EventBus.getDefault().post(new WeatherStationListEvent(station, WeatherStationListEvent.EventReason.DELETE));
|
EventBus.getDefault().post(new WeatherStationListEvent(station, WeatherStationListEvent.EventReason.DELETE));
|
||||||
|
|
|
@ -1,10 +1,6 @@
|
||||||
package cc.pogoda.mobile.pogodacc.config;
|
package cc.pogoda.mobile.pogodacc.config;
|
||||||
|
|
||||||
import cc.pogoda.mobile.pogodacc.type.Favourites;
|
|
||||||
|
|
||||||
public class AppConfiguration {
|
public class AppConfiguration {
|
||||||
|
|
||||||
public static boolean replaceMsWithKnots = false;
|
public static boolean replaceMsWithKnots = false;
|
||||||
|
|
||||||
public static Favourites favourites = null;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
package cc.pogoda.mobile.pogodacc.file;
|
package cc.pogoda.mobile.pogodacc.file;
|
||||||
|
|
||||||
import com.google.gson.JsonParser;
|
|
||||||
|
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
@ -40,11 +38,26 @@ public class FavouritiesFile {
|
||||||
char buffer[] = new char[(int) file.length()];
|
char buffer[] = new char[(int) file.length()];
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// read the content of file
|
||||||
streamReader.read(buffer);
|
streamReader.read(buffer);
|
||||||
|
|
||||||
streamReader.close();
|
streamReader.close();
|
||||||
|
|
||||||
JSONObject root = new JSONObject(new String(buffer));
|
// parse JSON to array
|
||||||
|
JSONArray root = new JSONArray(new String(buffer));
|
||||||
|
|
||||||
|
if (root != null) {
|
||||||
|
for (int i = 0 ; i < root.length(); i++) {
|
||||||
|
|
||||||
|
// create new weather station data object
|
||||||
|
WeatherStation station = new WeatherStation();
|
||||||
|
|
||||||
|
// get onlu 'systemName' as the rest will be copied from current 'allStationsList'
|
||||||
|
station.setSystemName(root.getJSONObject(i).getString("systemName"));
|
||||||
|
|
||||||
|
out.add(station);
|
||||||
|
}
|
||||||
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
|
@ -100,9 +113,6 @@ public class FavouritiesFile {
|
||||||
}
|
}
|
||||||
|
|
||||||
FileOutputStream outputStream = new FileOutputStream(output);
|
FileOutputStream outputStream = new FileOutputStream(output);
|
||||||
// OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
|
|
||||||
//
|
|
||||||
// outputStreamWriter.write();
|
|
||||||
|
|
||||||
// write JSON content to file on disk
|
// write JSON content to file on disk
|
||||||
outputStream.write(jsonData.getBytes());
|
outputStream.write(jsonData.getBytes());
|
||||||
|
|
|
@ -1,59 +0,0 @@
|
||||||
package cc.pogoda.mobile.pogodacc.type;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.util.LinkedList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import cc.pogoda.mobile.pogodacc.config.AppConfiguration;
|
|
||||||
import cc.pogoda.mobile.pogodacc.file.FileNames;
|
|
||||||
|
|
||||||
public class Favourites {
|
|
||||||
|
|
||||||
private List<WeatherStation> favourites;
|
|
||||||
|
|
||||||
private FileNames fileNames;
|
|
||||||
|
|
||||||
public Favourites(FileNames fns) {
|
|
||||||
|
|
||||||
favourites = new LinkedList<WeatherStation>();
|
|
||||||
|
|
||||||
fileNames = fns;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void storeOnDisk() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean addFav(WeatherStation s) {
|
|
||||||
boolean out = false;
|
|
||||||
|
|
||||||
if (AppConfiguration.favourites != null && AppConfiguration.favourites.getFavourites() != null) {
|
|
||||||
|
|
||||||
if (!AppConfiguration.favourites.getFavourites().contains(s)) {
|
|
||||||
AppConfiguration.favourites.getFavourites().add(s);
|
|
||||||
out = true;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean removeFav(WeatherStation s) {
|
|
||||||
boolean out = false;
|
|
||||||
|
|
||||||
if (AppConfiguration.favourites != null && AppConfiguration.favourites.getFavourites() != null) {
|
|
||||||
out = AppConfiguration.favourites.getFavourites().remove(s);
|
|
||||||
}
|
|
||||||
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<WeatherStation> getFavourites() {
|
|
||||||
return favourites;
|
|
||||||
}
|
|
||||||
}
|
|
Ładowanie…
Reference in New Issue