saving restoring favourites list

fix/get-all-stations-hangout
Mateusz Lubecki 2021-08-12 22:42:41 +02:00
rodzic e460611820
commit 2f1652a9fb
6 zmienionych plików z 81 dodań i 88 usunięć

Wyświetl plik

@ -11,11 +11,14 @@ import androidx.recyclerview.widget.RecyclerView;
import cc.pogoda.mobile.pogodacc.R;
import cc.pogoda.mobile.pogodacc.adapter.WeatherStationRecyclerViewAdapter;
import cc.pogoda.mobile.pogodacc.config.AppConfiguration;
import cc.pogoda.mobile.pogodacc.type.ParceableStationsList;
public class FavouritesActivity extends AppCompatActivity {
RecyclerView recyclerViewFavourites;
ParceableStationsList favourites;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
@ -28,7 +31,9 @@ public class FavouritesActivity extends AppCompatActivity {
protected void onCreate(@Nullable Bundle 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);
}
else {
@ -39,7 +44,7 @@ public class FavouritesActivity extends AppCompatActivity {
if (recyclerViewFavourites != null) {
WeatherStationRecyclerViewAdapter adapter = null;
adapter = new WeatherStationRecyclerViewAdapter(AppConfiguration.favourites.getFavourites(), this);
adapter = new WeatherStationRecyclerViewAdapter(favourites.getList(), this);
recyclerViewFavourites.setAdapter(adapter);

Wyświetl plik

@ -15,6 +15,8 @@ import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
@ -34,6 +36,8 @@ public class MainActivity extends AppCompatActivity {
private FileNames fileNames;
private FavouritiesFile favouritiesFile;
private List<WeatherStation> listOfAllStations;
List<WeatherStation> favs;
@ -54,25 +58,46 @@ public class MainActivity extends AppCompatActivity {
// check if this is a first call after application start
if (favs == null) {
favs = new FavouritiesFile(fileNames).loadFavourites();
favs = favouritiesFile.loadFavourites();
}
// update values for the fav list with listOfAllStations
for (WeatherStation f : favs) {
// if favs is still null it means that favourites file doesn't even exists
// 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
WeatherStation fromAllStations = listOfAllStations.get(idx);
// find an index of updated station
int idx = listOfAllStations.indexOf(fromFavs);
// 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());
// get the station
WeatherStation fromAllStations = listOfAllStations.get(idx);
// update all parameters
fromFavs.setAvailableParameters(fromAllStations.getAvailableParameters());
fromFavs.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);
@ -123,6 +148,8 @@ public class MainActivity extends AppCompatActivity {
fileNames = new FileNames(baseContext);
favouritiesFile = new FavouritiesFile(fileNames);
// download all stations from API
listOfAllStations = new AllStationsDao().getAllStations();
@ -152,13 +179,28 @@ public class MainActivity extends AppCompatActivity {
switch (serviceEvent.getEventReason()) {
case ADD:
// check of list consist this station
if (favs.contains(serviceEvent.getStation())) {
return;
}
// add favourites to list
favs.add(serviceEvent.getStation());
try {
// save the list into JSON file
favouritiesFile.persistFavourities(favs);
} catch (IOException e) {
e.printStackTrace();
}
break;
case DELETE:
favs.remove(serviceEvent.getStation());
break;
}
// recreate parceable object and pass it everywhere
recreateListOfFavs();
//Toast.makeText(this, intentServiceResult.getResultValue(), Toast.LENGTH_SHORT).show();
}

Wyświetl plik

@ -135,9 +135,8 @@ public class StationDetailsActivity extends AppCompatActivity {
switch (item.getItemId()) {
case R.id.menuItemStationDetailsAddFavourites:
if (station != null) {
boolean result = false;
boolean result = true;
//result = AppConfiguration.favourites.addFav(station);
EventBus.getDefault().post(new WeatherStationListEvent(station, WeatherStationListEvent.EventReason.ADD));
if (result) {
@ -153,7 +152,7 @@ public class StationDetailsActivity extends AppCompatActivity {
break;
case R.id.menuItemStationDetailsDeleteFavourites:
if (station != null) {
boolean result = false;
boolean result = true;
//result = AppConfiguration.favourites.removeFav(station);
EventBus.getDefault().post(new WeatherStationListEvent(station, WeatherStationListEvent.EventReason.DELETE));

Wyświetl plik

@ -1,10 +1,6 @@
package cc.pogoda.mobile.pogodacc.config;
import cc.pogoda.mobile.pogodacc.type.Favourites;
public class AppConfiguration {
public static boolean replaceMsWithKnots = false;
public static Favourites favourites = null;
}

Wyświetl plik

@ -1,7 +1,5 @@
package cc.pogoda.mobile.pogodacc.file;
import com.google.gson.JsonParser;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
@ -40,11 +38,26 @@ public class FavouritiesFile {
char buffer[] = new char[(int) file.length()];
try {
// read the content of file
streamReader.read(buffer);
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) {
e.printStackTrace();
} catch (JSONException e) {
@ -100,9 +113,6 @@ public class FavouritiesFile {
}
FileOutputStream outputStream = new FileOutputStream(output);
// OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
//
// outputStreamWriter.write();
// write JSON content to file on disk
outputStream.write(jsonData.getBytes());

Wyświetl plik

@ -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;
}
}