fix issue:refresh button not rotating

new feature: show sum
new feature: clone location
update Readme and help
This commit is contained in:
woheller69 2023-04-18 16:54:51 +02:00
parent 38295c65e1
commit 2ae9a055eb
16 changed files with 274 additions and 82 deletions

View file

@ -20,8 +20,8 @@ public class SolarPowerPlant {
double inverterEfficiency;
double azimuthAngle;
double tiltAngle;
private int[] shadingElevation;
private int[] shadingOpacity;
private final int[] shadingElevation;
private final int[] shadingOpacity;
public SolarPowerPlant(double latitude, double longitude, double cellsMaxPower, double cellsArea, double cellsEfficiency, double cellsTempCoeff, double diffuseEfficiency, double inverterPowerLimit, double inverterEfficiency, double azimuthAngle, double tiltAngle, int[] shadingElevation, int[] shadingOpacity ) {
this.latitude = latitude;

View file

@ -48,6 +48,7 @@ public class ForecastCityActivity extends NavigationActivity implements IUpdatea
private ViewPager2 viewPager2;
private TabLayout tabLayout;
private TextView noCityText;
private static Boolean isRefreshing = false;
Context context;
@Override
@ -136,10 +137,15 @@ public class ForecastCityActivity extends NavigationActivity implements IUpdatea
}
private void initResources() {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
viewPager2 = findViewById(R.id.viewPager2);
reduceViewpager2DragSensitivity(viewPager2,2);
tabLayout = findViewById(R.id.tab_layout);
pagerAdapter = new WeatherPagerAdapter(this, getSupportFragmentManager(),getLifecycle());
if (sharedPreferences.getBoolean("pref_summarize",false)){
pagerAdapter = new WeatherPagerAdapter(this, getSupportFragmentManager(),getLifecycle(),true);
} else {
pagerAdapter = new WeatherPagerAdapter(this, getSupportFragmentManager(),getLifecycle(),false);
}
noCityText = findViewById(R.id.noCitySelectedText);
}
@ -158,6 +164,7 @@ public class ForecastCityActivity extends NavigationActivity implements IUpdatea
refreshActionButton = menu.findItem(R.id.menu_refresh);
refreshActionButton.setActionView(R.layout.menu_refresh_action_view);
refreshActionButton.getActionView().setOnClickListener(v -> m.performIdentifierAction(refreshActionButton.getItemId(), 0));
if (isRefreshing) startRefreshAnimation();
return true;
}
@ -208,28 +215,29 @@ public class ForecastCityActivity extends NavigationActivity implements IUpdatea
@Override
public void processNewGeneralData(GeneralData data) {
if (refreshActionButton != null && refreshActionButton.getActionView() != null) {
refreshActionButton.getActionView().clearAnimation();
}
stopRefreshAnimation();
}
@Override
public void processNewWeekForecasts(List<WeekForecast> forecasts) {
if (refreshActionButton != null && refreshActionButton.getActionView() != null) {
refreshActionButton.getActionView().clearAnimation();
}
stopRefreshAnimation();
}
@Override
public void processNewForecasts(List<HourlyForecast> hourlyForecasts) {
stopRefreshAnimation();
}
public static void stopRefreshAnimation(){
if (refreshActionButton != null && refreshActionButton.getActionView() != null) {
refreshActionButton.getActionView().clearAnimation();
}
isRefreshing = false;
}
public static void startRefreshAnimation(){
{
if(refreshActionButton !=null && refreshActionButton.getActionView() != null) {
isRefreshing = true;
if(refreshActionButton !=null && refreshActionButton.getActionView() != null) {
RotateAnimation rotate = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setDuration(500);
rotate.setRepeatCount(5);
@ -247,6 +255,7 @@ public class ForecastCityActivity extends NavigationActivity implements IUpdatea
refreshActionButton.getActionView().setActivated(true);
refreshActionButton.getActionView().setEnabled(true);
refreshActionButton.getActionView().setClickable(true);
isRefreshing = false;
}
@Override
@ -254,8 +263,7 @@ public class ForecastCityActivity extends NavigationActivity implements IUpdatea
}
});
refreshActionButton.getActionView().startAnimation(rotate);
}
}
}
}
//https://devdreamz.com/question/348298-how-to-modify-sensitivity-of-viewpager

View file

@ -251,6 +251,40 @@ public class ManageLocationsActivity extends NavigationActivity {
alert.setNegativeButton(getString(R.string.dialog_add_close_button), (dialog, whichButton) -> {
});
alert.setNeutralButton(getString(R.string.dialog_add_clone_button), (dialog, whichButton) -> {
CityToWatch cloneCity = new CityToWatch(
database.getMaxRank() + 1,
-1,
0, city.getLongitude(),city.getLatitude(),
city.getCityName()
);
long id=database.addCityToWatch(cloneCity);
cloneCity.setId((int) id);
cloneCity.setCityId((int) id); //use id also instead of city id as unique identifier
cities.add(cloneCity);
adapter.notifyDataSetChanged();
for (int i = 0; i < shadingElevation.length ; i++) {
shadingElevation[i]= Integer.parseInt(elevationViews[i].getText().toString().isEmpty() ? "0" : elevationViews[i].getText().toString());
shadingOpacity[i]= Integer.parseInt(opacityViews[i].getText().toString().isEmpty() ? "0" : opacityViews[i].getText().toString());
}
adapter.updateCity(cloneCity, String.valueOf(editCity.getText()),
Float.parseFloat(editLatitude.getText().toString().isEmpty() ? "0" : editLatitude.getText().toString()),
Float.parseFloat(editLongitude.getText().toString().isEmpty() ? "0" : editLongitude.getText().toString()),
Float.parseFloat(editAzimuth.getText().toString().isEmpty() ? "0" : editAzimuth.getText().toString()),
Float.parseFloat(editTilt.getText().toString().isEmpty() ? "0" : editTilt.getText().toString()),
Float.parseFloat(editCellsMaxPower.getText().toString().isEmpty() ? "0" : editCellsMaxPower.getText().toString()),
Float.parseFloat(editCellsArea.getText().toString().isEmpty() ? "0" : editCellsArea.getText().toString()),
Float.parseFloat(editCellsEfficiency.getText().toString().isEmpty() ? "0" : editCellsEfficiency.getText().toString()),
Float.parseFloat(editCellsTempCoeff.getText().toString().isEmpty() ? "0" : editCellsTempCoeff.getText().toString()),
Float.parseFloat(editDiffuseEfficiency.getText().toString().isEmpty() ? "0" : editDiffuseEfficiency.getText().toString()),
Float.parseFloat(editInverterPowerLimit.getText().toString().isEmpty() ? "0" : editInverterPowerLimit.getText().toString()),
Float.parseFloat(editInverterEfficiency.getText().toString().isEmpty() ? "0" : editInverterEfficiency.getText().toString()),
shadingElevation,
shadingOpacity
);
});
alert.show();
}
@ -283,7 +317,7 @@ public class ManageLocationsActivity extends NavigationActivity {
return new CityToWatch(
database.getMaxRank() + 1,
-1,
selectedCity.getCityId(), selectedCity.getLongitude(),selectedCity.getLatitude(),
0, selectedCity.getLongitude(),selectedCity.getLatitude(),
selectedCity.getCityName()
);
}

View file

@ -12,6 +12,7 @@ import androidx.preference.PreferenceManager;
import androidx.preference.SeekBarPreference;
import org.woheller69.weather.R;
import org.woheller69.weather.database.SQLiteHelper;
public class SettingsActivity extends NavigationActivity implements SharedPreferences.OnSharedPreferenceChangeListener{
@ -80,6 +81,11 @@ public class SettingsActivity extends NavigationActivity implements SharedPrefer
if (key.equals("pref_number_days")){
SeekBarPreference numberDays = findPreference("pref_number_days");
if (numberDays.getValue()<3) numberDays.setValue(3);
} else if (key.equals("pref_summarize")){
if (sharedPreferences.getBoolean("pref_summarize",false)) {
SQLiteHelper database = SQLiteHelper.getInstance(getActivity());
database.deleteAllForecasts();
}
}
}
}

View file

@ -403,6 +403,17 @@ public class SQLiteHelper extends SQLiteOpenHelper {
}
public synchronized void deleteAllForecasts(){
SQLiteDatabase database = this.getWritableDatabase();
database.delete(TABLE_GENERAL_DATA, COLUMN_CITY_ID + " <> ?",
new String[]{Integer.toString(-1)});
database.delete(TABLE_HOURLY_FORECAST, FORECAST_CITY_ID + " <> ?",
new String[]{Integer.toString(-1)});
database.delete(TABLE_WEEKFORECAST, WEEKFORECAST_CITY_ID + " <> ?",
new String[]{Integer.toString(-1)});
database.close();
}
public synchronized List<HourlyForecast> getForecastsByCityId(int cityId) {
SQLiteDatabase database = this.getWritableDatabase();

View file

@ -21,6 +21,7 @@ import com.db.chart.view.AxisController;
import com.db.chart.view.BarChartView;
import org.woheller69.weather.R;
import org.woheller69.weather.database.CityToWatch;
import org.woheller69.weather.database.GeneralData;
import org.woheller69.weather.database.HourlyForecast;
import org.woheller69.weather.database.SQLiteHelper;
@ -69,7 +70,34 @@ public class CityWeatherAdapter extends RecyclerView.Adapter<CityWeatherAdapter.
// function update 3-hour or 1-hour forecast list
public void updateForecastData(List<HourlyForecast> hourlyForecasts) {
if (hourlyForecasts.isEmpty()) return;
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
if (sp.getBoolean("pref_summarize",false)){
int cityId = hourlyForecasts.get(0).getCity_id();
ArrayList<Integer> CityIDList = new ArrayList<Integer>();
SQLiteHelper dbHelper = SQLiteHelper.getInstance(context.getApplicationContext());
hourlyForecasts = dbHelper.getForecastsByCityId(cityId); //get fresh values from database to make sure we do add new values to sum values from last update
List<CityToWatch> citiesToWatch = dbHelper.getAllCitiesToWatch();
CityToWatch requestedCity = dbHelper.getCityToWatch(cityId);
for (int i = 0; i < citiesToWatch.size(); i++) {
CityToWatch city = citiesToWatch.get(i);
if (city.getCityId()!=requestedCity.getCityId() && city.getLatitude() == requestedCity.getLatitude() && city.getLongitude() == requestedCity.getLongitude()) {
CityIDList.add(city.getCityId());
}
}
if (CityIDList.size()>0){
for (int c=0; c<CityIDList.size();c++) {
int iteratorCityId = CityIDList.get(c);
List<HourlyForecast> hfc = dbHelper.getForecastsByCityId(iteratorCityId);
if (hfc.size()!=hourlyForecasts.size()) break; //maybe something went wrong during update or city is not yet updated
for (int i=0;i<hfc.size();i++){
hourlyForecasts.get(i).setPower(hourlyForecasts.get(i).getPower()+hfc.get(i).getPower());
}
}
}
}
courseDayList = new ArrayList<>();
for (HourlyForecast f : hourlyForecasts) {
@ -87,9 +115,33 @@ public class CityWeatherAdapter extends RecyclerView.Adapter<CityWeatherAdapter.
// function for week forecast list
public void updateWeekForecastData(List<WeekForecast> forecasts) {
if (forecasts.isEmpty()) return;
int cityId = forecasts.get(0).getCity_id();
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
if (sp.getBoolean("pref_summarize",false)){
ArrayList<Integer> CityIDList = new ArrayList<Integer>();
SQLiteHelper dbHelper = SQLiteHelper.getInstance(context.getApplicationContext());
forecasts = dbHelper.getWeekForecastsByCityId(cityId); //get fresh values from database to make sure we do add new values to sum values from last update
List<CityToWatch> citiesToWatch = dbHelper.getAllCitiesToWatch();
CityToWatch requestedCity = dbHelper.getCityToWatch(cityId);
for (int i = 0; i < citiesToWatch.size(); i++) {
CityToWatch city = citiesToWatch.get(i);
if (city.getCityId()!=requestedCity.getCityId() && city.getLatitude() == requestedCity.getLatitude() && city.getLongitude() == requestedCity.getLongitude()) {
CityIDList.add(city.getCityId());
}
}
if (CityIDList.size()>0){
for (int c=0; c<CityIDList.size();c++) {
int iteratorCityId = CityIDList.get(c);
List<WeekForecast> wfc = dbHelper.getWeekForecastsByCityId(iteratorCityId);
if (wfc.size() != forecasts.size()) break; //maybe something went wrong during update or city is not yet updated
for (int i=0;i<wfc.size();i++){
forecasts.get(i).setEnergyDay(forecasts.get(i).getEnergyDay()+wfc.get(i).getEnergyDay());
}
}
}
}
SQLiteHelper dbHelper = SQLiteHelper.getInstance(context.getApplicationContext());
int zonemilliseconds = dbHelper.getGeneralDataByCityId(cityId).getTimeZoneSeconds() * 1000;

View file

@ -17,6 +17,7 @@ import org.woheller69.weather.services.UpdateDataService;
import org.woheller69.weather.ui.WeatherCityFragment;
import org.woheller69.weather.ui.updater.IUpdateableCityUI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@ -35,18 +36,41 @@ public class WeatherPagerAdapter extends FragmentStateAdapter implements IUpdate
private List<CityToWatch> cities;
private boolean showSum;
public WeatherPagerAdapter(Context context, @NonNull FragmentManager supportFragmentManager, @NonNull Lifecycle lifecycle) {
public WeatherPagerAdapter(Context context, @NonNull FragmentManager supportFragmentManager, @NonNull Lifecycle lifecycle, Boolean showSum) {
super(supportFragmentManager,lifecycle);
this.mContext = context;
this.database = SQLiteHelper.getInstance(context);
this.showSum = showSum;
loadCities();
}
public void loadCities() {
this.cities = database.getAllCitiesToWatch();
Collections.sort(cities, (o1, o2) -> o1.getRank() - o2.getRank());
List<CityToWatch> allCities = database.getAllCitiesToWatch();
Collections.sort(allCities, (o1, o2) -> o1.getRank() - o2.getRank());
if (showSum && !allCities.isEmpty()) {
List<CityToWatch> citiesFiltered = new ArrayList<>();
citiesFiltered.add(allCities.get(0)); //always add first element
if (allCities.size() > 0) { //if there is more than one element
for (int i = 1; i < allCities.size(); i++) {
boolean alreadyHasLatLon = false;
for (int j = 0; j < citiesFiltered.size(); j++) {
if (citiesFiltered.get(j).getLatitude() == allCities.get(i).getLatitude() && citiesFiltered.get(j).getLongitude() == allCities.get(i).getLongitude()) {
alreadyHasLatLon = true;
}
}
if (!alreadyHasLatLon) {
citiesFiltered.add(allCities.get(i));
}
}
}
this.cities = citiesFiltered;
} else {
this.cities = allCities;
}
}
@NonNull
@ -64,7 +88,19 @@ public class WeatherPagerAdapter extends FragmentStateAdapter implements IUpdate
}
public CharSequence getPageTitle(int position) {
return cities.get(position).getCityName();
String title;
if (showSum){
String name = cities.get(position).getCityName();
if (name.contains("|")){
String[] name2 = name.split("\\|");
title = name2[0];
} else {
title = cities.get(position).getCityName();
}
} else {
title = cities.get(position).getCityName();
}
return title;
}
public static void refreshSingleData(Context context, Boolean asap, int cityId) {
@ -106,14 +142,4 @@ public class WeatherPagerAdapter extends FragmentStateAdapter implements IUpdate
return -1; //item not found
}
public float getLatForPos(int pos) {
CityToWatch city = cities.get(pos);
return city.getLatitude();
}
public float getLonForPos(int pos) {
CityToWatch city = cities.get(pos);
return city.getLongitude();
}
}

View file

@ -1,15 +1,18 @@
package org.woheller69.weather.weather_api.open_meteo;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.widget.Toast;
import androidx.preference.PreferenceManager;
import com.android.volley.VolleyError;
import org.json.JSONException;
import org.json.JSONObject;
import org.woheller69.weather.R;
import org.woheller69.weather.activities.NavigationActivity;
import org.woheller69.weather.database.CityToWatch;
import org.woheller69.weather.database.GeneralData;
import org.woheller69.weather.database.HourlyForecast;
import org.woheller69.weather.database.WeekForecast;
@ -62,66 +65,88 @@ public class ProcessOMweatherAPIRequest implements IProcessHttpRequest {
public void processSuccessScenario(String response, int cityId) {
IDataExtractor extractor = new OMDataExtractor(context);
try {
JSONObject json = new JSONObject(response);
//Extract daily weather
dbHelper.deleteWeekForecastsByCityId(cityId);
List<WeekForecast> weekforecasts = new ArrayList<>();
weekforecasts = extractor.extractWeekForecast(json.getString("daily"));
if (weekforecasts!=null && !weekforecasts.isEmpty()){
for (WeekForecast weekForecast: weekforecasts){
weekForecast.setCity_id(cityId);
ArrayList<Integer> CityIDList = new ArrayList<Integer>();
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
if (sharedPreferences.getBoolean("pref_summarize",false)){
List<CityToWatch> citiesToWatch = dbHelper.getAllCitiesToWatch();
CityToWatch requestedCity = dbHelper.getCityToWatch(cityId);
for (int i = 0; i < citiesToWatch.size(); i++) {
CityToWatch city = citiesToWatch.get(i);
if (city.getCityId()!=requestedCity.getCityId() && city.getLatitude() == requestedCity.getLatitude() && city.getLongitude() == requestedCity.getLongitude()) {
CityIDList.add(city.getCityId());
}
} else {
final String ERROR_MSG = context.getResources().getString(R.string.error_convert_to_json);
if (NavigationActivity.isVisible)
Toast.makeText(context, ERROR_MSG, Toast.LENGTH_LONG).show();
return;
}
}
//Extract current weather
GeneralData generalData = new GeneralData();
generalData.setTimestamp(System.currentTimeMillis() / 1000);
generalData.setCity_id(cityId);
generalData.setTimeSunrise(weekforecasts.get(0).getTimeSunrise());
generalData.setTimeSunset(weekforecasts.get(0).getTimeSunset());
generalData.setTimeZoneSeconds(json.getInt("utc_offset_seconds"));
GeneralData current = dbHelper.getGeneralDataByCityId(cityId);
if (current != null && current.getCity_id() == cityId) {
dbHelper.updateGeneralData(generalData);
} else {
dbHelper.addGeneralData(generalData);
}
CityIDList.add(cityId); //add requested city at end of list. Call Viewupdater if last (requested) city is updated
//Extract hourly weather
dbHelper.deleteForecastsByCityId(cityId);
List<HourlyForecast> hourlyforecasts = new ArrayList<>();
hourlyforecasts = extractor.extractHourlyForecast(json.getString("hourly"), cityId);
for (int c=0; c<CityIDList.size();c++) {
cityId = CityIDList.get(c);
if (hourlyforecasts!=null && !hourlyforecasts.isEmpty()){
for (HourlyForecast hourlyForecast: hourlyforecasts){
hourlyForecast.setCity_id(cityId);
try {
JSONObject json = new JSONObject(response);
//Extract daily weather
dbHelper.deleteWeekForecastsByCityId(cityId);
List<WeekForecast> weekforecasts = new ArrayList<>();
weekforecasts = extractor.extractWeekForecast(json.getString("daily"));
if (weekforecasts != null && !weekforecasts.isEmpty()) {
for (WeekForecast weekForecast : weekforecasts) {
weekForecast.setCity_id(cityId);
}
} else {
final String ERROR_MSG = context.getResources().getString(R.string.error_convert_to_json);
if (NavigationActivity.isVisible)
Toast.makeText(context, ERROR_MSG, Toast.LENGTH_LONG).show();
return;
}
} else {
final String ERROR_MSG = context.getResources().getString(R.string.error_convert_to_json);
if (NavigationActivity.isVisible)
Toast.makeText(context, ERROR_MSG, Toast.LENGTH_LONG).show();
return;
//Extract current weather
GeneralData generalData = new GeneralData();
generalData.setTimestamp(System.currentTimeMillis() / 1000);
generalData.setCity_id(cityId);
generalData.setTimeSunrise(weekforecasts.get(0).getTimeSunrise());
generalData.setTimeSunset(weekforecasts.get(0).getTimeSunset());
generalData.setTimeZoneSeconds(json.getInt("utc_offset_seconds"));
GeneralData current = dbHelper.getGeneralDataByCityId(cityId);
if (current != null && current.getCity_id() == cityId) {
dbHelper.updateGeneralData(generalData);
} else {
dbHelper.addGeneralData(generalData);
}
//Extract hourly weather
dbHelper.deleteForecastsByCityId(cityId);
List<HourlyForecast> hourlyforecasts = new ArrayList<>();
hourlyforecasts = extractor.extractHourlyForecast(json.getString("hourly"), cityId);
if (hourlyforecasts != null && !hourlyforecasts.isEmpty()) {
for (HourlyForecast hourlyForecast : hourlyforecasts) {
hourlyForecast.setCity_id(cityId);
}
} else {
final String ERROR_MSG = context.getResources().getString(R.string.error_convert_to_json);
if (NavigationActivity.isVisible)
Toast.makeText(context, ERROR_MSG, Toast.LENGTH_LONG).show();
return;
}
dbHelper.addForecasts(hourlyforecasts);
weekforecasts = reanalyzeWeekIDs(weekforecasts, hourlyforecasts);
dbHelper.addWeekForecasts(weekforecasts);
if (c == CityIDList.size()-1) ViewUpdater.updateGeneralDataData(generalData); // Call Viewupdater if last (requested) city is updated
if (c == CityIDList.size()-1) ViewUpdater.updateWeekForecasts(weekforecasts);
if (c == CityIDList.size()-1) ViewUpdater.updateForecasts(hourlyforecasts);
} catch (JSONException e) {
e.printStackTrace();
}
dbHelper.addForecasts(hourlyforecasts);
weekforecasts = reanalyzeWeekIDs(weekforecasts, hourlyforecasts);
dbHelper.addWeekForecasts(weekforecasts);
ViewUpdater.updateGeneralDataData(generalData);
ViewUpdater.updateWeekForecasts(weekforecasts);
ViewUpdater.updateForecasts(hourlyforecasts);
} catch (JSONException e) {
e.printStackTrace();
}
}