Skip to content

Commit

Permalink
Refactor getWidgetCityID to PFASQLiteHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
woheller69 committed Aug 16, 2022
1 parent f94afe0 commit d1fe149
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 41 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ If you know the solution to a bug please report it in the corresponding issue an

| RadarWeather | Gas Prices | Smart Eggtimer | Level | hEARtest | GPS Cockpit | Audio Analyzer |
|:---:|:---:|:---:|:---:|:---:|:---:|:---:|
| [<img src="https://github.com/woheller69/weather/blob/main/fastlane/metadata/android/en-US/images/icon.png" height="80">](https://f-droid.org/packages/org.woheller69.weather/)| [<img src="https://github.com/woheller69/spritpreise/blob/main/fastlane/metadata/android/en-US/images/icon.png" height="80">](https://f-droid.org/packages/org.woheller69.spritpreise/) | [<img src="https://github.com/woheller69/eggtimer/blob/main/fastlane/metadata/android/en-US/images/icon.png" height="80">](https://f-droid.org/packages/org.woheller69.eggtimer/) | [<img src="https://github.com/woheller69/Level/blob/master/fastlane/metadata/android/en-US/images/icon.png" height="80">](https://f-droid.org/packages/org.woheller69.level/) | [<img src="https://github.com/woheller69/audiometry/blob/new/fastlane/metadata/android/en-US/images/icon.png" height="80">](https://f-droid.org/packages/org.woheller69.audiometry/) | [<img src="https://github.com/woheller69/gpscockpit/blob/master/fastlane/metadata/android/en-US/images/icon.png" height="80">](https://f-droid.org/packages/org.woheller69.gpscockpit/) | [<img src="https://github.com/woheller69/audio-analyzer-for-android/blob/master/fastlane/metadata/android/en-US/images/icon.png" height="80">](https://f-droid.org/packages/org.woheller69.audio_analyzer_for_android/) |
| [<img src="https://github.com/woheller69/weather/blob/main/fastlane/metadata/android/en-US/images/icon.png" height="80">](https://f-droid.org/packages/org.woheller69.weather/)| [<img src="https://github.com/woheller69/spritpreise/blob/main/fastlane/metadata/android/en-US/images/icon.png" height="80">](https://f-droid.org/packages/org.woheller69.spritpreise/) | [<img src="https://github.com/woheller69/eggtimer/blob/main/fastlane/metadata/android/en-US/images/icon.png" height="80">](https://f-droid.org/packages/org.woheller69.eggtimer/) | [<img src="https://github.com/woheller69/Level/blob/master/fastlane/metadata/android/en-US/images/icon.png" height="80">](https://f-droid.org/packages/org.woheller69.level/) | [<img src="https://github.com/woheller69/audiometry/blob/new/fastlane/metadata/android/en-US/images/icon.png" height="80">](https://f-droid.org/packages/org.woheller69.audiometry/) | [<img src="https://github.com/woheller69/gpscockpit/blob/master/fastlane/metadata/android/en-US/images/icon.png" height="80">](https://f-droid.org/packages/org.woheller69.gpscockpit/) | [<img src="https://github.com/woheller69/audio-analyzer-for-android/blob/master/fastlane/metadata/android/en-US/images/icon.png" height="80">](https://f-droid.org/packages/org.woheller69.audio_analyzer_for_android/) |
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import org.woheller69.weather.ui.updater.IUpdateableCityUI;
import org.woheller69.weather.ui.updater.ViewUpdater;
import org.woheller69.weather.ui.viewPager.WeatherPagerAdapter;
import org.woheller69.weather.widget.WeatherWidget;
import static org.woheller69.weather.database.PFASQLiteHelper.getWidgetCityID;

import java.lang.reflect.Field;
import java.util.List;
Expand Down Expand Up @@ -239,14 +239,14 @@ public boolean onOptionsItemSelected(final MenuItem item) {
public void onLocationChanged(android.location.Location location) {
Log.d("GPS", "Location changed");
PFASQLiteHelper db = PFASQLiteHelper.getInstance(context);
CityToWatch city = db.getCityToWatch(WeatherWidget.getWidgetCityID(context));
CityToWatch city = db.getCityToWatch(getWidgetCityID(context));
city.setLatitude((float) location.getLatitude());
city.setLongitude((float) location.getLongitude());
city.setCityName(String.format(Locale.getDefault(), "%.2f° / %.2f°", location.getLatitude(), location.getLongitude()));
db.updateCityToWatch(city);
db.deleteWeekForecastsByCityId(WeatherWidget.getWidgetCityID(context));
db.deleteCurrentWeatherByCityId(WeatherWidget.getWidgetCityID(context));
db.deleteForecastsByCityId(WeatherWidget.getWidgetCityID(context));
db.deleteWeekForecastsByCityId(getWidgetCityID(context));
db.deleteCurrentWeatherByCityId(getWidgetCityID(context));
db.deleteForecastsByCityId(getWidgetCityID(context));
pagerAdapter.loadCities();
viewPager2.setAdapter(pagerAdapter);
tabLayout.getTabAt(0).setText(city.getCityName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -940,4 +940,21 @@ public synchronized void deleteCurrentWeatherByCityId(int cityId) {
new String[]{Integer.toString(cityId)});
database.close();
}

public static int getWidgetCityID(Context context) {
PFASQLiteHelper db = PFASQLiteHelper.getInstance(context);
int cityID=0;
List<CityToWatch> cities = db.getAllCitiesToWatch();
int rank=cities.get(0).getRank();
for (int i = 0; i < cities.size(); i++) { //find cityID for first city to watch = lowest Rank
CityToWatch city = cities.get(i);
//Log.d("debugtag",Integer.toString(city.getRank()));
if (city.getRank() <= rank ){
rank=city.getRank();
cityID = city.getCityId();
}
}
return cityID;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.woheller69.weather.weather_api.IProcessHttpRequest;
import org.woheller69.weather.widget.WeatherWidget;
import org.woheller69.weather.widget.WeatherWidget5day;
import static org.woheller69.weather.database.PFASQLiteHelper.getWidgetCityID;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -198,7 +199,7 @@ public void run() {

private void possiblyUpdateWidgets(int cityID, CurrentWeatherData currentWeather, List<WeekForecast> weekforecasts, List<Forecast> hourlyforecasts) {
//search for widgets with same city ID
int widgetCityID=WeatherWidget.getWidgetCityID(context);
int widgetCityID=getWidgetCityID(context);

int[] widgetIDs = AppWidgetManager.getInstance(context).getAppWidgetIds(new ComponentName(context, WeatherWidget.class));

Expand All @@ -218,7 +219,7 @@ private void possiblyUpdateWidgets(int cityID, CurrentWeatherData currentWeather
}

//search for 5day widgets with same city ID
int widget5dayCityID= WeatherWidget5day.getWidgetCityID(context);
int widget5dayCityID= getWidgetCityID(context);
int[] widget5dayIDs = AppWidgetManager.getInstance(context).getAppWidgetIds(new ComponentName(context, WeatherWidget5day.class));

for (int widgetID : widget5dayIDs) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.woheller69.weather.weather_api.IDataExtractor;
import org.woheller69.weather.weather_api.IProcessHttpRequest;
import org.woheller69.weather.widget.WeatherWidget5day;
import static org.woheller69.weather.database.PFASQLiteHelper.getWidgetCityID;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -157,7 +158,7 @@ public void run() {

private void possiblyUpdate5DayWidgets(int cityID, List<WeekForecast> weekforecasts) {
//search for 5day widgets with same city ID
int widget5dayCityID= WeatherWidget5day.getWidgetCityID(context);
int widget5dayCityID= getWidgetCityID(context);
int[] widget5dayIDs = AppWidgetManager.getInstance(context).getAppWidgetIds(new ComponentName(context, WeatherWidget5day.class));

for (int widgetID : widget5dayIDs) {
Expand Down
18 changes: 2 additions & 16 deletions app/src/main/java/org/woheller69/weather/widget/WeatherWidget.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.woheller69.weather.services.UpdateDataService;
import org.woheller69.weather.ui.Help.StringFormatUtils;
import org.woheller69.weather.ui.UiResourceProvider;
import static org.woheller69.weather.database.PFASQLiteHelper.getWidgetCityID;

import java.util.Calendar;
import java.util.List;
Expand Down Expand Up @@ -64,21 +65,6 @@ public void updateAppWidget(Context context, final int appWidgetId) {
}
}

public static int getWidgetCityID(Context context) {
PFASQLiteHelper db = PFASQLiteHelper.getInstance(context);
int cityID=0;
List<CityToWatch> cities = db.getAllCitiesToWatch();
int rank=cities.get(0).getRank();
for (int i = 0; i < cities.size(); i++) { //find cityID for first city to watch = lowest Rank
CityToWatch city = cities.get(i);
//Log.d("debugtag",Integer.toString(city.getRank()));
if (city.getRank() <= rank ){
rank=city.getRank();
cityID = city.getCityId();
}
}
return cityID;
}

public static void updateLocation(final Context context, int cityID, boolean manual) {
PFASQLiteHelper db = PFASQLiteHelper.getInstance(context);
Expand Down Expand Up @@ -266,7 +252,7 @@ public void onEnabled(Context context) {
// Enter relevant functionality for when the first widget is created
PFASQLiteHelper dbHelper = PFASQLiteHelper.getInstance(context);

int widgetCityID=WeatherWidget.getWidgetCityID(context);
int widgetCityID=getWidgetCityID(context);

CurrentWeatherData currentWeather=dbHelper.getCurrentWeatherByCityId(widgetCityID);
List<WeekForecast> weekforecasts=dbHelper.getWeekForecastsByCityId(widgetCityID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.woheller69.weather.ui.Help.StringFormatUtils;
import org.woheller69.weather.ui.UiResourceProvider;
import org.woheller69.weather.weather_api.IApiToDatabaseConversion;
import static org.woheller69.weather.database.PFASQLiteHelper.getWidgetCityID;

import java.util.Calendar;
import java.util.List;
Expand Down Expand Up @@ -48,21 +49,6 @@ public void updateAppWidget(Context context, final int appWidgetId) {
}
}

public static int getWidgetCityID(Context context) {
PFASQLiteHelper db = PFASQLiteHelper.getInstance(context);
int cityID=0;
List<CityToWatch> cities = db.getAllCitiesToWatch();
int rank=cities.get(0).getRank();
for (int i = 0; i < cities.size(); i++) { //find cityID for first city to watch = lowest Rank
CityToWatch city = cities.get(i);
//Log.d("debugtag",Integer.toString(city.getRank()));
if (city.getRank() <= rank ){
rank=city.getRank();
cityID = city.getCityId();
}
}
return cityID;
}

public static void updateView(Context context, AppWidgetManager appWidgetManager, RemoteViews views, int appWidgetId, CityToWatch city, List<WeekForecast> weekforecasts) {

Expand Down Expand Up @@ -180,7 +166,7 @@ public void onEnabled(Context context) {
// Enter relevant functionality for when the first widget is created
PFASQLiteHelper dbHelper = PFASQLiteHelper.getInstance(context);

int widgetCityID=WeatherWidget.getWidgetCityID(context);
int widgetCityID=getWidgetCityID(context);

List<Forecast> forecasts=dbHelper.getForecastsByCityId(widgetCityID);
List<WeekForecast> weekforecasts=dbHelper.getWeekForecastsByCityId(widgetCityID);
Expand Down

0 comments on commit d1fe149

Please sign in to comment.