kopia lustrzana https://github.com/SP8EBC/MeteoSystem
favourites and all station moved to main application class
rodzic
5e984ce264
commit
31135df0c9
Plik binarny nie jest wyświetlany.
Po Szerokość: | Wysokość: | Rozmiar: 188 KiB |
Plik binarny nie jest wyświetlany.
Po Szerokość: | Wysokość: | Rozmiar: 196 KiB |
|
@ -36,6 +36,8 @@ repositories {
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
|
||||||
|
implementation 'org.tinylog:tinylog-api:2.1.0'
|
||||||
|
runtimeOnly 'org.tinylog:tinylog-impl:2.1.0'
|
||||||
implementation 'androidx.appcompat:appcompat:1.3.1'
|
implementation 'androidx.appcompat:appcompat:1.3.1'
|
||||||
implementation 'com.google.android.material:material:1.4.0'
|
implementation 'com.google.android.material:material:1.4.0'
|
||||||
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
|
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
android:networkSecurityConfig="@xml/network_security_config"
|
android:networkSecurityConfig="@xml/network_security_config"
|
||||||
android:roundIcon="@mipmap/ic_launcher_icon_foreground"
|
android:roundIcon="@mipmap/ic_launcher_icon_foreground"
|
||||||
android:supportsRtl="true"
|
android:supportsRtl="true"
|
||||||
|
android:name=".Main"
|
||||||
android:theme="@style/Theme.Meteosystem">
|
android:theme="@style/Theme.Meteosystem">
|
||||||
<activity
|
<activity
|
||||||
android:name="cc.pogoda.mobile.meteosystem.activity.SettingsActivity"
|
android:name="cc.pogoda.mobile.meteosystem.activity.SettingsActivity"
|
||||||
|
|
|
@ -0,0 +1,217 @@
|
||||||
|
package cc.pogoda.mobile.meteosystem;
|
||||||
|
|
||||||
|
import android.app.Application;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.res.Configuration;
|
||||||
|
import android.content.res.Resources;
|
||||||
|
import android.os.Build;
|
||||||
|
import android.os.StrictMode;
|
||||||
|
import android.os.strictmode.Violation;
|
||||||
|
|
||||||
|
import com.jakewharton.threetenabp.AndroidThreeTen;
|
||||||
|
|
||||||
|
import org.greenrobot.eventbus.EventBus;
|
||||||
|
import org.greenrobot.eventbus.Subscribe;
|
||||||
|
import org.greenrobot.eventbus.ThreadMode;
|
||||||
|
import org.tinylog.Logger;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
import cc.pogoda.mobile.meteosystem.activity.handler.MainActImageButtonFavouritesClickEvent;
|
||||||
|
import cc.pogoda.mobile.meteosystem.config.AppConfiguration;
|
||||||
|
import cc.pogoda.mobile.meteosystem.dao.AllStationsDao;
|
||||||
|
import cc.pogoda.mobile.meteosystem.file.ConfigurationFile;
|
||||||
|
import cc.pogoda.mobile.meteosystem.file.FavouritiesFile;
|
||||||
|
import cc.pogoda.mobile.meteosystem.file.FileNames;
|
||||||
|
import cc.pogoda.mobile.meteosystem.type.ParceableStationsList;
|
||||||
|
import cc.pogoda.mobile.meteosystem.type.WeatherStation;
|
||||||
|
import cc.pogoda.mobile.meteosystem.type.WeatherStationListEvent;
|
||||||
|
|
||||||
|
public class Main extends Application {
|
||||||
|
|
||||||
|
private File directory;
|
||||||
|
|
||||||
|
private File directoryForLogs;
|
||||||
|
|
||||||
|
private Context ctx;
|
||||||
|
|
||||||
|
private ConfigurationFile confFile;
|
||||||
|
|
||||||
|
private FileNames fileNames;
|
||||||
|
|
||||||
|
private FavouritiesFile favouritiesFile;
|
||||||
|
|
||||||
|
public List<WeatherStation> getListOfAllStations() {
|
||||||
|
return listOfAllStations;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<WeatherStation> listOfAllStations;
|
||||||
|
|
||||||
|
public List<WeatherStation> getFavs() {
|
||||||
|
return favs;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<WeatherStation> favs;
|
||||||
|
|
||||||
|
public File getDirectory() {
|
||||||
|
return directory;
|
||||||
|
}
|
||||||
|
|
||||||
|
public File getDirectoryForLogs() {
|
||||||
|
return directoryForLogs;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ConfigurationFile getConfFile() {
|
||||||
|
return confFile;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCreate() {
|
||||||
|
super.onCreate();
|
||||||
|
|
||||||
|
ctx = this.getApplicationContext();
|
||||||
|
|
||||||
|
confFile = new ConfigurationFile(ctx);
|
||||||
|
|
||||||
|
StrictMode.VmPolicy.Builder b = new StrictMode.VmPolicy.Builder();
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
|
||||||
|
StrictMode.VmPolicy policy = b.detectAll().detectNonSdkApiUsage().penaltyListener((Runnable r) -> r.run(), (Violation v) -> {v.printStackTrace();}).build();
|
||||||
|
StrictMode.setVmPolicy(policy);
|
||||||
|
}
|
||||||
|
|
||||||
|
directory = getApplicationContext().getDir("meteosystem", Context.MODE_PRIVATE);
|
||||||
|
|
||||||
|
directoryForLogs = new File(directory.getAbsolutePath() + "/logs/");
|
||||||
|
|
||||||
|
System.setProperty("tinylog.directory", directoryForLogs.getAbsolutePath());
|
||||||
|
|
||||||
|
Logger.info("Application starting...");
|
||||||
|
|
||||||
|
AndroidThreeTen.init(this);
|
||||||
|
|
||||||
|
EventBus.getDefault().register(this);
|
||||||
|
|
||||||
|
ConfigurationFile confFile = new ConfigurationFile(ctx);
|
||||||
|
|
||||||
|
confFile.restoreFromFile();
|
||||||
|
|
||||||
|
fileNames = new FileNames(ctx);
|
||||||
|
|
||||||
|
favouritiesFile = new FavouritiesFile(fileNames);
|
||||||
|
|
||||||
|
// download all stations from API
|
||||||
|
listOfAllStations = new AllStationsDao().getAllStations();
|
||||||
|
|
||||||
|
Logger.info("[Main][onCreate][listOfAllStations.size() = " + listOfAllStations.size() + "]");
|
||||||
|
|
||||||
|
// recreate list of favorites
|
||||||
|
recreateListOfFavs();
|
||||||
|
|
||||||
|
if (AppConfiguration.locale != null && !AppConfiguration.locale.equals("default") ) {
|
||||||
|
Logger.debug("[Main][onCreate][AppConfiguration.locale = " + AppConfiguration.locale + "]");
|
||||||
|
Locale locale = new Locale(AppConfiguration.locale);
|
||||||
|
Locale.setDefault(locale);
|
||||||
|
Resources resources = this.getResources();
|
||||||
|
Configuration config = resources.getConfiguration();
|
||||||
|
config.setLocale(locale);
|
||||||
|
Logger.debug("[Main][onCreate][locale = " + locale.toLanguageTag() + "]");
|
||||||
|
resources.updateConfiguration(config, resources.getDisplayMetrics());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void recreateListOfFavs() {
|
||||||
|
|
||||||
|
// check if this is a first call after application start
|
||||||
|
if (favs == null) {
|
||||||
|
favs = favouritiesFile.loadFavourites();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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++) {
|
||||||
|
|
||||||
|
//
|
||||||
|
WeatherStation fromFavs = favs.get(i);
|
||||||
|
|
||||||
|
// find an index of updated station
|
||||||
|
int idx = listOfAllStations.indexOf(fromFavs);
|
||||||
|
|
||||||
|
// 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());
|
||||||
|
fromFavs.setTimezone(fromAllStations.getTimezone());
|
||||||
|
fromFavs.setCallsignSsid(fromAllStations.getCallsignSsid());
|
||||||
|
fromFavs.setStationNameTextColor(fromAllStations.getStationNameTextColor());
|
||||||
|
|
||||||
|
// 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
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Subscribe(threadMode = ThreadMode.MAIN)
|
||||||
|
public void weatherStationListHandler(WeatherStationListEvent serviceEvent) {
|
||||||
|
Logger.info("[Main][weatherStationListHandler][serviceEvent = " + serviceEvent + "]");
|
||||||
|
|
||||||
|
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());
|
||||||
|
|
||||||
|
try {
|
||||||
|
// save the list into JSON file
|
||||||
|
favouritiesFile.persistFavourities(favs);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// recreate parceable object and pass it everywhere
|
||||||
|
recreateListOfFavs();
|
||||||
|
//Toast.makeText(this, intentServiceResult.getResultValue(), Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -4,34 +4,43 @@ import androidx.appcompat.app.AppCompatActivity;
|
||||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||||
import androidx.recyclerview.widget.RecyclerView;
|
import androidx.recyclerview.widget.RecyclerView;
|
||||||
|
|
||||||
|
import cc.pogoda.mobile.meteosystem.Main;
|
||||||
import cc.pogoda.mobile.meteosystem.R;
|
import cc.pogoda.mobile.meteosystem.R;
|
||||||
import cc.pogoda.mobile.meteosystem.adapter.WeatherStationRecyclerViewAdapter;
|
import cc.pogoda.mobile.meteosystem.adapter.WeatherStationRecyclerViewAdapter;
|
||||||
import cc.pogoda.mobile.meteosystem.type.ParceableFavsCallReason;
|
import cc.pogoda.mobile.meteosystem.type.ParceableFavsCallReason;
|
||||||
import cc.pogoda.mobile.meteosystem.type.ParceableStationsList;
|
import cc.pogoda.mobile.meteosystem.type.ParceableStationsList;
|
||||||
|
import cc.pogoda.mobile.meteosystem.type.WeatherStation;
|
||||||
import cc.pogoda.mobile.meteosystem.type.web.Summary;
|
import cc.pogoda.mobile.meteosystem.type.web.Summary;
|
||||||
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class AllStationsActivity extends AppCompatActivity {
|
public class AllStationsActivity extends AppCompatActivity {
|
||||||
|
|
||||||
RecyclerView recyclerViewAllStations;
|
RecyclerView recyclerViewAllStations;
|
||||||
|
|
||||||
Summary test;
|
Summary test;
|
||||||
|
|
||||||
ParceableStationsList allStationsList;
|
private List<WeatherStation> allStationsList;
|
||||||
|
|
||||||
|
Main main;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_all_stations);
|
setContentView(R.layout.activity_all_stations);
|
||||||
|
|
||||||
allStationsList = getIntent().getParcelableExtra("all_stations");
|
main = (Main) getApplication();
|
||||||
|
|
||||||
|
//allStationsList = getIntent().getParcelableExtra("all_stations");
|
||||||
|
allStationsList = main.getListOfAllStations();
|
||||||
|
|
||||||
recyclerViewAllStations = findViewById(R.id.recyclerViewAllStations);
|
recyclerViewAllStations = findViewById(R.id.recyclerViewAllStations);
|
||||||
|
|
||||||
WeatherStationRecyclerViewAdapter adapter = null;
|
WeatherStationRecyclerViewAdapter adapter = null;
|
||||||
|
|
||||||
adapter = new WeatherStationRecyclerViewAdapter(allStationsList.getList(), this, ParceableFavsCallReason.Reason.ALL_STATIONS);
|
adapter = new WeatherStationRecyclerViewAdapter(allStationsList, this, ParceableFavsCallReason.Reason.ALL_STATIONS);
|
||||||
|
|
||||||
recyclerViewAllStations.setAdapter(adapter);
|
recyclerViewAllStations.setAdapter(adapter);
|
||||||
|
|
||||||
|
|
|
@ -153,8 +153,6 @@ public class ExportDataActivity extends AppCompatActivity {
|
||||||
|
|
||||||
act = this;
|
act = this;
|
||||||
|
|
||||||
ParceableStationsList favs = getIntent().getParcelableExtra("favs");
|
|
||||||
|
|
||||||
formatSpinner = findViewById(R.id.spinnerOutputFormat);
|
formatSpinner = findViewById(R.id.spinnerOutputFormat);
|
||||||
selectStationButton = findViewById(R.id.buttonSelectStationExport);
|
selectStationButton = findViewById(R.id.buttonSelectStationExport);
|
||||||
stationNameToExport = findViewById(R.id.textViewStationToExport);
|
stationNameToExport = findViewById(R.id.textViewStationToExport);
|
||||||
|
@ -189,7 +187,6 @@ public class ExportDataActivity extends AppCompatActivity {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View view) {
|
public void onClick(View view) {
|
||||||
intent = new Intent(act, FavouritesActivity.class);
|
intent = new Intent(act, FavouritesActivity.class);
|
||||||
intent.putExtra("favs", favs);
|
|
||||||
|
|
||||||
ParceableFavsCallReason callReason = new ParceableFavsCallReason(ParceableFavsCallReason.Reason.EXPORT_SELECT);
|
ParceableFavsCallReason callReason = new ParceableFavsCallReason(ParceableFavsCallReason.Reason.EXPORT_SELECT);
|
||||||
intent.putExtra("callReason", callReason);
|
intent.putExtra("callReason", callReason);
|
||||||
|
|
|
@ -10,8 +10,11 @@ import androidx.appcompat.app.AppCompatActivity;
|
||||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||||
import androidx.recyclerview.widget.RecyclerView;
|
import androidx.recyclerview.widget.RecyclerView;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import cc.pogoda.mobile.meteosystem.Main;
|
||||||
import cc.pogoda.mobile.meteosystem.R;
|
import cc.pogoda.mobile.meteosystem.R;
|
||||||
import cc.pogoda.mobile.meteosystem.adapter.WeatherStationRecyclerViewAdapter;
|
import cc.pogoda.mobile.meteosystem.adapter.WeatherStationRecyclerViewAdapter;
|
||||||
import cc.pogoda.mobile.meteosystem.type.ParceableFavsCallReason;
|
import cc.pogoda.mobile.meteosystem.type.ParceableFavsCallReason;
|
||||||
|
@ -20,9 +23,11 @@ import cc.pogoda.mobile.meteosystem.type.WeatherStation;
|
||||||
|
|
||||||
public class FavouritesActivity extends AppCompatActivity {
|
public class FavouritesActivity extends AppCompatActivity {
|
||||||
|
|
||||||
|
Main main;
|
||||||
|
|
||||||
RecyclerView recyclerViewFavourites;
|
RecyclerView recyclerViewFavourites;
|
||||||
|
|
||||||
ParceableStationsList favourites, sortedFavourites;
|
List<WeatherStation> favourites, sortedFavourites;
|
||||||
|
|
||||||
boolean sorting = false;
|
boolean sorting = false;
|
||||||
|
|
||||||
|
@ -58,7 +63,7 @@ public class FavouritesActivity extends AppCompatActivity {
|
||||||
sorting = true;
|
sorting = true;
|
||||||
|
|
||||||
if (recyclerViewFavourites != null) {
|
if (recyclerViewFavourites != null) {
|
||||||
adapter = new WeatherStationRecyclerViewAdapter(sortedFavourites.getList(), this, callReason.getReason());
|
adapter = new WeatherStationRecyclerViewAdapter(sortedFavourites, this, callReason.getReason());
|
||||||
|
|
||||||
adapter.createAndStartUpdater();
|
adapter.createAndStartUpdater();
|
||||||
|
|
||||||
|
@ -70,7 +75,7 @@ public class FavouritesActivity extends AppCompatActivity {
|
||||||
sorting = false;
|
sorting = false;
|
||||||
|
|
||||||
if (recyclerViewFavourites != null) {
|
if (recyclerViewFavourites != null) {
|
||||||
adapter = new WeatherStationRecyclerViewAdapter(favourites.getList(), this, callReason.getReason());
|
adapter = new WeatherStationRecyclerViewAdapter(favourites, this, callReason.getReason());
|
||||||
|
|
||||||
adapter.createAndStartUpdater();
|
adapter.createAndStartUpdater();
|
||||||
|
|
||||||
|
@ -89,15 +94,17 @@ public class FavouritesActivity extends AppCompatActivity {
|
||||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
favourites = getIntent().getParcelableExtra("favs");
|
//favourites = getIntent().getParcelableExtra("favs");
|
||||||
|
main = (Main)getApplication();
|
||||||
|
|
||||||
sortedFavourites = new ParceableStationsList(favourites);
|
favourites = main.getFavs();
|
||||||
|
sortedFavourites = new ArrayList<>(favourites);
|
||||||
|
|
||||||
sortedFavourites.getList().sort(new WxStationComparator());
|
sortedFavourites.sort(new WxStationComparator());
|
||||||
|
|
||||||
callReason = getIntent().getParcelableExtra("callReason");
|
callReason = getIntent().getParcelableExtra("callReason");
|
||||||
|
|
||||||
if (favourites == null || favourites.getList().size() == 0) {
|
if (favourites == null || favourites.size() == 0) {
|
||||||
setContentView(R.layout.activity_favourites_empty);
|
setContentView(R.layout.activity_favourites_empty);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -106,7 +113,7 @@ public class FavouritesActivity extends AppCompatActivity {
|
||||||
recyclerViewFavourites = findViewById(R.id.recyclerViewFavourites);
|
recyclerViewFavourites = findViewById(R.id.recyclerViewFavourites);
|
||||||
|
|
||||||
if (recyclerViewFavourites != null) {
|
if (recyclerViewFavourites != null) {
|
||||||
adapter = new WeatherStationRecyclerViewAdapter(favourites.getList(), this, callReason.getReason());
|
adapter = new WeatherStationRecyclerViewAdapter(favourites, this, callReason.getReason());
|
||||||
|
|
||||||
adapter.createAndStartUpdater();
|
adapter.createAndStartUpdater();
|
||||||
|
|
||||||
|
|
|
@ -23,12 +23,14 @@ import com.jakewharton.threetenabp.AndroidThreeTen;
|
||||||
import org.greenrobot.eventbus.EventBus;
|
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 org.tinylog.Logger;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
|
import cc.pogoda.mobile.meteosystem.Main;
|
||||||
import cc.pogoda.mobile.meteosystem.R;
|
import cc.pogoda.mobile.meteosystem.R;
|
||||||
import cc.pogoda.mobile.meteosystem.activity.handler.MainActImageButtonAllStationsClickEvent;
|
import cc.pogoda.mobile.meteosystem.activity.handler.MainActImageButtonAllStationsClickEvent;
|
||||||
import cc.pogoda.mobile.meteosystem.activity.handler.MainActImageButtonExportClickEvent;
|
import cc.pogoda.mobile.meteosystem.activity.handler.MainActImageButtonExportClickEvent;
|
||||||
|
@ -45,20 +47,10 @@ import cc.pogoda.mobile.meteosystem.type.WeatherStationListEvent;
|
||||||
|
|
||||||
public class MainActivity extends AppCompatActivity {
|
public class MainActivity extends AppCompatActivity {
|
||||||
|
|
||||||
|
private Main main;
|
||||||
|
|
||||||
private Context baseContext;
|
private Context baseContext;
|
||||||
|
|
||||||
private FileNames fileNames;
|
|
||||||
|
|
||||||
private FavouritiesFile favouritiesFile;
|
|
||||||
|
|
||||||
private List<WeatherStation> listOfAllStations;
|
|
||||||
|
|
||||||
List<WeatherStation> favs;
|
|
||||||
|
|
||||||
private ParceableStationsList parceableListOfAllStations;
|
|
||||||
|
|
||||||
private ParceableStationsList parceableListOfFavStations;
|
|
||||||
|
|
||||||
private MainActImageButtonFavouritesClickEvent mainActImageButtonFavouritesClickEvent = null;
|
private MainActImageButtonFavouritesClickEvent mainActImageButtonFavouritesClickEvent = null;
|
||||||
|
|
||||||
private ImageButton imageButtonFavourites;
|
private ImageButton imageButtonFavourites;
|
||||||
|
@ -71,66 +63,6 @@ public class MainActivity extends AppCompatActivity {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void recreateListOfFavs() {
|
|
||||||
|
|
||||||
// check if this is a first call after application start
|
|
||||||
if (favs == null) {
|
|
||||||
favs = favouritiesFile.loadFavourites();
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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++) {
|
|
||||||
|
|
||||||
//
|
|
||||||
WeatherStation fromFavs = favs.get(i);
|
|
||||||
|
|
||||||
// find an index of updated station
|
|
||||||
int idx = listOfAllStations.indexOf(fromFavs);
|
|
||||||
|
|
||||||
// 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());
|
|
||||||
fromFavs.setTimezone(fromAllStations.getTimezone());
|
|
||||||
fromFavs.setCallsignSsid(fromAllStations.getCallsignSsid());
|
|
||||||
fromFavs.setStationNameTextColor(fromAllStations.getStationNameTextColor());
|
|
||||||
|
|
||||||
// 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);
|
|
||||||
|
|
||||||
// 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
|
@Override
|
||||||
protected void onDestroy() {
|
protected void onDestroy() {
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
|
@ -143,69 +75,45 @@ public class MainActivity extends AppCompatActivity {
|
||||||
@Override
|
@Override
|
||||||
protected void onResume() {
|
protected void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
recreateListOfFavs();
|
//recreateListOfFavs();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
|
main = (Main) getApplication();
|
||||||
|
|
||||||
baseContext = getApplicationContext();
|
baseContext = getApplicationContext();
|
||||||
|
|
||||||
StrictMode.VmPolicy.Builder b = new StrictMode.VmPolicy.Builder();
|
// create an event handler fired when a user click 'favourites' button
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
|
mainActImageButtonFavouritesClickEvent = new MainActImageButtonFavouritesClickEvent(this);
|
||||||
StrictMode.VmPolicy policy = b.detectAll().detectNonSdkApiUsage().penaltyListener((Runnable r) -> r.run(), (Violation v) -> {v.printStackTrace();}).build();
|
|
||||||
StrictMode.setVmPolicy(policy);
|
|
||||||
}
|
|
||||||
|
|
||||||
// register to Event bus to receive events when a station is added od removed from favourites
|
// assign on click listener
|
||||||
EventBus.getDefault().register(this);
|
if (imageButtonFavourites != null) {
|
||||||
|
imageButtonFavourites.setOnClickListener(mainActImageButtonFavouritesClickEvent);
|
||||||
AndroidThreeTen.init(this);
|
|
||||||
|
|
||||||
ConfigurationFile confFile = new ConfigurationFile(baseContext);
|
|
||||||
|
|
||||||
confFile.restoreFromFile();
|
|
||||||
|
|
||||||
if (AppConfiguration.locale != null && !AppConfiguration.locale.equals("default") ) {
|
|
||||||
Locale locale = new Locale(AppConfiguration.locale);
|
|
||||||
Locale.setDefault(locale);
|
|
||||||
Resources resources = this.getResources();
|
|
||||||
Configuration config = resources.getConfiguration();
|
|
||||||
config.setLocale(locale);
|
|
||||||
resources.updateConfiguration(config, resources.getDisplayMetrics());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setContentView(R.layout.activity_main);
|
setContentView(R.layout.activity_main);
|
||||||
|
|
||||||
fileNames = new FileNames(baseContext);
|
|
||||||
|
|
||||||
favouritiesFile = new FavouritiesFile(fileNames);
|
|
||||||
|
|
||||||
// download all stations from API
|
|
||||||
listOfAllStations = new AllStationsDao().getAllStations();
|
|
||||||
|
|
||||||
// convert this to parceable to exchange across intents
|
|
||||||
parceableListOfAllStations = ParceableStationsList.createFromStdList(listOfAllStations);
|
|
||||||
|
|
||||||
// recreate list of favorites
|
|
||||||
recreateListOfFavs();
|
|
||||||
|
|
||||||
ImageButton imageButtonAllStations = (ImageButton)findViewById(R.id.imageButtonAllStations);
|
ImageButton imageButtonAllStations = (ImageButton)findViewById(R.id.imageButtonAllStations);
|
||||||
if (imageButtonAllStations != null)
|
if (imageButtonAllStations != null)
|
||||||
imageButtonAllStations.setOnClickListener(new MainActImageButtonAllStationsClickEvent(this, parceableListOfAllStations));
|
imageButtonAllStations.setOnClickListener(new MainActImageButtonAllStationsClickEvent(this));
|
||||||
|
|
||||||
imageButtonFavourites = (ImageButton)findViewById(R.id.imageButtonFavourites);
|
imageButtonFavourites = (ImageButton)findViewById(R.id.imageButtonFavourites);
|
||||||
|
if (imageButtonFavourites != null) {
|
||||||
|
imageButtonFavourites.setOnClickListener(new MainActImageButtonFavouritesClickEvent(this));
|
||||||
|
}
|
||||||
|
|
||||||
// set an action for clicking on export data button
|
// set an action for clicking on export data button
|
||||||
exportButton = (ImageButton)findViewById(R.id.imageButtonExport);
|
exportButton = (ImageButton)findViewById(R.id.imageButtonExport);
|
||||||
if (exportButton != null) {
|
if (exportButton != null) {
|
||||||
exportButton.setOnClickListener(new MainActImageButtonExportClickEvent(this, parceableListOfFavStations));
|
exportButton.setOnClickListener(new MainActImageButtonExportClickEvent(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
settingsButton = (ImageButton) findViewById(R.id.imageButtonSettings);
|
settingsButton = (ImageButton) findViewById(R.id.imageButtonSettings);
|
||||||
if (settingsButton != null) {
|
if (settingsButton != null) {
|
||||||
settingsButton.setOnClickListener(new MainActImageButtonSettingsClickEvent(this, confFile));
|
settingsButton.setOnClickListener(new MainActImageButtonSettingsClickEvent(this, main.getConfFile()));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -239,37 +147,4 @@ public class MainActivity extends AppCompatActivity {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Subscribe(threadMode = ThreadMode.MAIN)
|
|
||||||
public void weatherStationListHandler(WeatherStationListEvent serviceEvent) {
|
|
||||||
System.out.println(serviceEvent.toString());
|
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
|
@ -14,11 +14,10 @@ public class MainActImageButtonAllStationsClickEvent implements View.OnClickList
|
||||||
|
|
||||||
Intent intent;
|
Intent intent;
|
||||||
|
|
||||||
public MainActImageButtonAllStationsClickEvent(AppCompatActivity parent, ParceableStationsList allStations) {
|
public MainActImageButtonAllStationsClickEvent(AppCompatActivity parent) {
|
||||||
this.parent = parent;
|
this.parent = parent;
|
||||||
|
|
||||||
intent = new Intent(this.parent, AllStationsActivity.class);
|
intent = new Intent(this.parent, AllStationsActivity.class);
|
||||||
intent.putExtra("all_stations", allStations);
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,8 @@ import android.view.View;
|
||||||
|
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
|
|
||||||
|
import org.tinylog.Logger;
|
||||||
|
|
||||||
import cc.pogoda.mobile.meteosystem.activity.ExportDataActivity;
|
import cc.pogoda.mobile.meteosystem.activity.ExportDataActivity;
|
||||||
import cc.pogoda.mobile.meteosystem.type.ParceableStationsList;
|
import cc.pogoda.mobile.meteosystem.type.ParceableStationsList;
|
||||||
|
|
||||||
|
@ -14,16 +16,17 @@ public class MainActImageButtonExportClickEvent implements View.OnClickListener{
|
||||||
|
|
||||||
Intent intent;
|
Intent intent;
|
||||||
|
|
||||||
public MainActImageButtonExportClickEvent(AppCompatActivity p, ParceableStationsList favs) {
|
public MainActImageButtonExportClickEvent(AppCompatActivity p) {
|
||||||
parent = p;
|
parent = p;
|
||||||
|
|
||||||
intent = new Intent(this.parent, ExportDataActivity.class);
|
intent = new Intent(this.parent, ExportDataActivity.class);
|
||||||
intent.putExtra("favs", favs);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View view) {
|
public void onClick(View view) {
|
||||||
|
|
||||||
|
Logger.info("[MainActImageButtonExportClickEvent][onClick]");
|
||||||
|
|
||||||
parent.startActivity(intent);
|
parent.startActivity(intent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,11 +15,10 @@ public class MainActImageButtonFavouritesClickEvent implements View.OnClickListe
|
||||||
|
|
||||||
Intent intent;
|
Intent intent;
|
||||||
|
|
||||||
public MainActImageButtonFavouritesClickEvent(AppCompatActivity parent, ParceableStationsList favs) {
|
public MainActImageButtonFavouritesClickEvent(AppCompatActivity parent) {
|
||||||
this.parent = parent;
|
this.parent = parent;
|
||||||
|
|
||||||
intent = new Intent(this.parent, FavouritesActivity.class);
|
intent = new Intent(this.parent, FavouritesActivity.class);
|
||||||
intent.putExtra("favs", favs);
|
|
||||||
|
|
||||||
ParceableFavsCallReason callReason = new ParceableFavsCallReason(ParceableFavsCallReason.Reason.FAVOURITES);
|
ParceableFavsCallReason callReason = new ParceableFavsCallReason(ParceableFavsCallReason.Reason.FAVOURITES);
|
||||||
intent.putExtra("callReason", callReason);
|
intent.putExtra("callReason", callReason);
|
||||||
|
|
|
@ -3,6 +3,7 @@ package cc.pogoda.mobile.meteosystem.file;
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
import org.tinylog.Logger;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
|
@ -29,6 +30,8 @@ public class FavouritiesFile {
|
||||||
|
|
||||||
File file = fileNames.getFavJsonFile();
|
File file = fileNames.getFavJsonFile();
|
||||||
|
|
||||||
|
Logger.info("[FavouritiesFile][loadFavourites][file = " + file + "]");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// create an input stream to load file content
|
// create an input stream to load file content
|
||||||
FileInputStream fns = new FileInputStream(file);
|
FileInputStream fns = new FileInputStream(file);
|
||||||
|
@ -56,6 +59,8 @@ public class FavouritiesFile {
|
||||||
station.setSystemName(root.getJSONObject(i).getString("systemName"));
|
station.setSystemName(root.getJSONObject(i).getString("systemName"));
|
||||||
|
|
||||||
out.add(station);
|
out.add(station);
|
||||||
|
|
||||||
|
Logger.debug("[FavouritiesFile][loadFavourites][i = " + i +"][station.getSystemName() = " + station.getSystemName() + "]");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
@ -81,6 +86,8 @@ public class FavouritiesFile {
|
||||||
// main array for all stations
|
// main array for all stations
|
||||||
JSONArray mainArray = new JSONArray();
|
JSONArray mainArray = new JSONArray();
|
||||||
|
|
||||||
|
Logger.info("[FavouritiesFile][persistFavourities][favourites.size() = " + favourites.size() + "]");
|
||||||
|
|
||||||
for (WeatherStation wx : favourites) {
|
for (WeatherStation wx : favourites) {
|
||||||
JSONObject obj = new JSONObject();
|
JSONObject obj = new JSONObject();
|
||||||
|
|
||||||
|
@ -95,6 +102,8 @@ public class FavouritiesFile {
|
||||||
obj.put("lon", wx.getLon());
|
obj.put("lon", wx.getLon());
|
||||||
|
|
||||||
mainArray.put(obj);
|
mainArray.put(obj);
|
||||||
|
|
||||||
|
Logger.debug("[FavouritiesFile][persistFavourities][wx.getSystemName() = " + wx.getSystemName() + "]");
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
|
||||||
|
exception = unpack
|
||||||
|
writer1 = logcat
|
||||||
|
writer1.level = TRACE
|
||||||
|
writer1.format = {date: HH:mm:ss.SSS} {tag} {method}({file}:{line}): {message}
|
||||||
|
|
||||||
|
writer2 = rolling file
|
||||||
|
writer2.level = debug
|
||||||
|
writer2.file = #{tinylog.directory}/log_{date:yyyy-MM-dd}.txt
|
||||||
|
writer2.format = {date: HH:mm:ss.SSS} {class}.{method}()\n{level}: {message}
|
||||||
|
writer2.policies = daily
|
||||||
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
exception = unpack
|
||||||
|
writer1 = rolling file
|
||||||
|
writer1.level = debug
|
||||||
|
writer1.file = #{tinylog.directory}/log_{date:yyyy-MM-dd}.txt
|
||||||
|
writer1.backups = 7
|
||||||
|
writer1.format = {date: HH:mm:ss.SSS} {class}.{method}()\n{level}: {message}
|
||||||
|
writer1.policies = daily
|
||||||
|
|
||||||
|
writer2 = logcat
|
||||||
|
writer2.level = TRACE
|
||||||
|
writer2.format = {date: HH:mm:ss.SSS} {class}.{method}()\n{level}: {message}
|
||||||
|
|
Ładowanie…
Reference in New Issue