Skip to content

Commit

Permalink
fix widget update bug
Browse files Browse the repository at this point in the history
  • Loading branch information
VishnuSanal committed Dec 17, 2023
1 parent 53e7610 commit ae0c9d6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class SharedPreferenceHelper {

private final String WIDGET_QUOTE_STRING = "widgetQuoteString";
private final String WIDGET_AUTHOR_STRING = "widgetAuthorString";
private final String WIDGET_LAST_UPDATED = "widgetLastUpdatedString";

private final String TOTAL_QUOTE_COUNT = "totalQuoteInt";
private final String FAV_HINT_SHOWN_COUNT = "favHintShownCount";
Expand Down Expand Up @@ -247,6 +248,14 @@ private void setWidgetAuthorString(String author) {
sharedPreferences.edit().putString(WIDGET_AUTHOR_STRING, author).apply();
}

public String getWidgetLastUpdated() {
return sharedPreferences.getString(WIDGET_LAST_UPDATED, null);
}

public void setWidgetLastUpdated(String lastUpdated) {
sharedPreferences.edit().putString(WIDGET_LAST_UPDATED, lastUpdated).apply();
}

public void deleteFontPreference() {
String FONT_ARRAY_STRING = "fontArrayString";

Expand Down
21 changes: 20 additions & 1 deletion app/src/main/java/phone/vishnu/quotes/receiver/QuoteWidget.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
import android.text.TextPaint;
import android.widget.RemoteViews;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
import phone.vishnu.quotes.R;
import phone.vishnu.quotes.activity.MainActivity;
import phone.vishnu.quotes.helper.AlarmHelper;
Expand Down Expand Up @@ -174,7 +177,23 @@ public Bitmap buildBitmap(Context context, String text, Layout.Alignment alignme
}

private void initAppWidget(final Context context) {
new QuotesRepository().getRandomQuote(quote -> updateQuoteWidget(context, quote));

String date =
new SimpleDateFormat("dd-MMM-yyyy", Locale.getDefault())
.format(Calendar.getInstance().getTime());

SharedPreferenceHelper helper = new SharedPreferenceHelper(context);

Quote widgetQuote = helper.getWidgetQuote();

if (!date.equals(helper.getWidgetLastUpdated()) || widgetQuote == null)
new QuotesRepository()
.getRandomQuote(
quote -> {
updateQuoteWidget(context, quote);
helper.setWidgetLastUpdated(date);
});
else updateQuoteWidget(context, widgetQuote);
}

private void saveWidgetQuote(Context context, Quote quote) {
Expand Down

0 comments on commit ae0c9d6

Please sign in to comment.