Added folder select to count widget

pull/214/head
M66B 1 year ago
parent b2f7e97b8c
commit b277eda261

@ -29,6 +29,7 @@ import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.util.TypedValue; import android.util.TypedValue;
import android.view.View; import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.Button; import android.widget.Button;
@ -39,6 +40,8 @@ import android.widget.RadioButton;
import android.widget.Spinner; import android.widget.Spinner;
import android.widget.TextView; import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.Group; import androidx.constraintlayout.widget.Group;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import androidx.core.graphics.ColorUtils; import androidx.core.graphics.ColorUtils;
@ -49,12 +52,14 @@ import com.flask.colorpicker.builder.ColorPickerClickListener;
import com.flask.colorpicker.builder.ColorPickerDialogBuilder; import com.flask.colorpicker.builder.ColorPickerDialogBuilder;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
public class ActivityWidget extends ActivityBase { public class ActivityWidget extends ActivityBase {
private int appWidgetId; private int appWidgetId;
private Spinner spAccount; private Spinner spAccount;
private Spinner spFolder;
private CheckBox cbDayNight; private CheckBox cbDayNight;
private CheckBox cbSemiTransparent; private CheckBox cbSemiTransparent;
private ViewButtonColor btnBgColor; private ViewButtonColor btnBgColor;
@ -70,6 +75,7 @@ public class ActivityWidget extends ActivityBase {
private Group grpReady; private Group grpReady;
private ArrayAdapter<EntityAccount> adapterAccount; private ArrayAdapter<EntityAccount> adapterAccount;
private ArrayAdapter<EntityFolder> adapterFolder;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -86,6 +92,7 @@ public class ActivityWidget extends ActivityBase {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
long account = prefs.getLong("widget." + appWidgetId + ".account", -1L); long account = prefs.getLong("widget." + appWidgetId + ".account", -1L);
long folder = prefs.getLong("widget." + appWidgetId + ".folder", -1L);
boolean daynight = prefs.getBoolean("widget." + appWidgetId + ".daynight", false); boolean daynight = prefs.getBoolean("widget." + appWidgetId + ".daynight", false);
boolean semi = prefs.getBoolean("widget." + appWidgetId + ".semi", true); boolean semi = prefs.getBoolean("widget." + appWidgetId + ".semi", true);
int background = prefs.getInt("widget." + appWidgetId + ".background", Color.TRANSPARENT); int background = prefs.getInt("widget." + appWidgetId + ".background", Color.TRANSPARENT);
@ -101,6 +108,7 @@ public class ActivityWidget extends ActivityBase {
setContentView(R.layout.activity_widget); setContentView(R.layout.activity_widget);
spAccount = findViewById(R.id.spAccount); spAccount = findViewById(R.id.spAccount);
spFolder = findViewById(R.id.spFolder);
cbDayNight = findViewById(R.id.cbDayNight); cbDayNight = findViewById(R.id.cbDayNight);
cbSemiTransparent = findViewById(R.id.cbSemiTransparent); cbSemiTransparent = findViewById(R.id.cbSemiTransparent);
btnBgColor = findViewById(R.id.btnBgColor); btnBgColor = findViewById(R.id.btnBgColor);
@ -256,14 +264,19 @@ public class ActivityWidget extends ActivityBase {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
EntityAccount account = (EntityAccount) spAccount.getSelectedItem(); EntityAccount account = (EntityAccount) spAccount.getSelectedItem();
EntityFolder folder = (EntityFolder) spFolder.getSelectedItem();
int pos = spFontSize.getSelectedItemPosition(); int pos = spFontSize.getSelectedItemPosition();
SharedPreferences.Editor editor = prefs.edit(); SharedPreferences.Editor editor = prefs.edit();
if (account != null && account.id > 0) if (folder == null) {
editor.putString("widget." + appWidgetId + ".name", account.name); if (account != null && account.id > 0)
else editor.putString("widget." + appWidgetId + ".name", account.name);
editor.remove("widget." + appWidgetId + ".name"); else
editor.remove("widget." + appWidgetId + ".name");
} else
editor.putString("widget." + appWidgetId + ".name", folder.getDisplayName(ActivityWidget.this));
editor.putLong("widget." + appWidgetId + ".account", account == null ? -1L : account.id); editor.putLong("widget." + appWidgetId + ".account", account == null ? -1L : account.id);
editor.putLong("widget." + appWidgetId + ".folder", folder == null ? -1L : folder.id);
editor.putBoolean("widget." + appWidgetId + ".daynight", cbDayNight.isChecked()); editor.putBoolean("widget." + appWidgetId + ".daynight", cbDayNight.isChecked());
editor.putBoolean("widget." + appWidgetId + ".semi", cbSemiTransparent.isChecked()); editor.putBoolean("widget." + appWidgetId + ".semi", cbSemiTransparent.isChecked());
editor.putInt("widget." + appWidgetId + ".background", btnBgColor.getColor()); editor.putInt("widget." + appWidgetId + ".background", btnBgColor.getColor());
@ -288,6 +301,89 @@ public class ActivityWidget extends ActivityBase {
adapterAccount.setDropDownViewResource(R.layout.spinner_item1_dropdown); adapterAccount.setDropDownViewResource(R.layout.spinner_item1_dropdown);
spAccount.setAdapter(adapterAccount); spAccount.setAdapter(adapterAccount);
adapterFolder = new ArrayAdapter<EntityFolder>(this, R.layout.spinner_item1, android.R.id.text1, new ArrayList<EntityFolder>()) {
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
return localize(position, super.getView(position, convertView, parent));
}
@Override
public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
return localize(position, super.getDropDownView(position, convertView, parent));
}
private View localize(int position, View view) {
EntityFolder folder = getItem(position);
if (folder != null) {
TextView tv = view.findViewById(android.R.id.text1);
tv.setText(EntityFolder.localizeName(view.getContext(), folder.name));
}
return view;
}
};
adapterFolder.setDropDownViewResource(R.layout.spinner_item1_dropdown);
spFolder.setAdapter(adapterFolder);
spAccount.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
EntityAccount account = (EntityAccount) spAccount.getAdapter().getItem(position);
setFolders(account.id);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
setFolders(-1);
}
private void setFolders(long account) {
Bundle args = new Bundle();
args.putLong("account", account);
new SimpleTask<List<EntityFolder>>() {
@Override
protected List<EntityFolder> onExecute(Context context, Bundle args) {
long account = args.getLong("account");
DB db = DB.getInstance(context);
List<EntityFolder> folders = db.folder().getNotifyingFolders(account);
if (folders != null && folders.size() > 0)
Collections.sort(folders, folders.get(0).getComparator(context));
return folders;
}
@Override
protected void onExecuted(Bundle args, List<EntityFolder> folders) {
if (folders == null)
folders = new ArrayList<>();
EntityFolder notifying = new EntityFolder();
notifying.id = -1L;
notifying.name = getString(R.string.title_widget_folder_notifying);
folders.add(0, notifying);
adapterFolder.clear();
adapterFolder.addAll(folders);
int select = 0;
for (int i = 0; i < folders.size(); i++)
if (folders.get(i).id.equals(folder)) {
select = i;
break;
}
spFolder.setSelection(select);
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getSupportFragmentManager(), ex);
}
}.execute(ActivityWidget.this, args, "widget:folders");
}
});
// Initialize // Initialize
((TextView) inOld.findViewById(R.id.tvCount)).setText("3"); ((TextView) inOld.findViewById(R.id.tvCount)).setText("3");
((TextView) inNew.findViewById(R.id.tvCount)).setText("3"); ((TextView) inNew.findViewById(R.id.tvCount)).setText("3");

@ -174,10 +174,11 @@ public interface DaoFolder {
" AND folder.synchronize") " AND folder.synchronize")
List<EntityFolder> getSynchronizingFolders(long account); List<EntityFolder> getSynchronizingFolders(long account);
@Query("SELECT * FROM folder" + @Query("SELECT folder.* FROM folder" +
" JOIN account_view AS account ON account.id = folder.account" +
" WHERE folder.account = :account" + " WHERE folder.account = :account" +
" AND folder.`synchronize`" + " AND folder.notify" +
" AND folder.notify") " AND account.`synchronize`")
List<EntityFolder> getNotifyingFolders(long account); List<EntityFolder> getNotifyingFolders(long account);
@Query("SELECT * FROM folder" + @Query("SELECT * FROM folder" +

@ -568,7 +568,7 @@ public interface DaoMessage {
LiveData<List<TupleMessageEx>> liveUnseenNotify(); LiveData<List<TupleMessageEx>> liveUnseenNotify();
@Transaction @Transaction
@Query("SELECT account.id AS account," + @Query("SELECT account.id AS account, folder.id AS folder," +
" COUNT(message.id) AS unseen," + " COUNT(message.id) AS unseen," +
" SUM(CASE WHEN account.created IS NULL OR message.received > account.created OR message.sent > account.created THEN NOT ui_ignored ELSE 0 END) AS notifying" + " SUM(CASE WHEN account.created IS NULL OR message.received > account.created OR message.sent > account.created THEN NOT ui_ignored ELSE 0 END) AS notifying" +
" FROM message" + " FROM message" +
@ -579,11 +579,11 @@ public interface DaoMessage {
" AND folder.notify" + " AND folder.notify" +
" AND message.notifying <> " + EntityMessage.NOTIFYING_IGNORE + " AND message.notifying <> " + EntityMessage.NOTIFYING_IGNORE +
" AND NOT (message.ui_seen OR message.ui_hide)" + " AND NOT (message.ui_seen OR message.ui_hide)" +
" GROUP BY account.id" + " GROUP BY folder.id" +
" ORDER BY account.id") " ORDER BY folder.id")
LiveData<List<TupleMessageStats>> liveWidgetUnseen(Long account); LiveData<List<TupleMessageStats>> liveWidgetUnseen(Long account);
@Query("SELECT :account AS account," + @Query("SELECT :account AS account, folder.id AS folder," +
" COUNT(message.id) AS unseen," + " COUNT(message.id) AS unseen," +
" SUM(CASE WHEN account.created IS NULL OR message.received > account.created OR message.sent > account.created THEN NOT ui_ignored ELSE 0 END) AS notifying" + " SUM(CASE WHEN account.created IS NULL OR message.received > account.created OR message.sent > account.created THEN NOT ui_ignored ELSE 0 END) AS notifying" +
" FROM message" + " FROM message" +
@ -591,10 +591,11 @@ public interface DaoMessage {
" JOIN folder_view AS folder ON folder.id = message.folder" + " JOIN folder_view AS folder ON folder.id = message.folder" +
" WHERE (:account IS NULL OR account.id = :account)" + " WHERE (:account IS NULL OR account.id = :account)" +
" AND account.`synchronize`" + " AND account.`synchronize`" +
" AND (:folder IS NULL OR folder.id = :folder)" +
" AND folder.notify" + " AND folder.notify" +
" AND message.notifying <> " + EntityMessage.NOTIFYING_IGNORE + " AND message.notifying <> " + EntityMessage.NOTIFYING_IGNORE +
" AND NOT (message.ui_seen OR message.ui_hide)") " AND NOT (message.ui_seen OR message.ui_hide)")
TupleMessageStats getWidgetUnseen(Long account); TupleMessageStats getWidgetUnseen(Long account, Long folder);
@Transaction @Transaction
@Query("SELECT folder, COUNT(*) AS total" + @Query("SELECT folder, COUNT(*) AS total" +

@ -26,6 +26,7 @@ import java.util.Objects;
public class TupleMessageStats { public class TupleMessageStats {
public Long account; public Long account;
public Long folder;
public Integer unseen; public Integer unseen;
public Integer notifying; public Integer notifying;
@ -34,6 +35,7 @@ public class TupleMessageStats {
if (obj instanceof TupleMessageStats) { if (obj instanceof TupleMessageStats) {
TupleMessageStats other = (TupleMessageStats) obj; TupleMessageStats other = (TupleMessageStats) obj;
return (Objects.equals(this.account, other.account) && return (Objects.equals(this.account, other.account) &&
Objects.equals(this.folder, other.folder) &&
Objects.equals(this.unseen, other.unseen) && Objects.equals(this.unseen, other.unseen) &&
Objects.equals(this.notifying, other.notifying)); Objects.equals(this.notifying, other.notifying));
} else } else

@ -59,6 +59,7 @@ public class Widget extends AppWidgetProvider {
for (int appWidgetId : appWidgetIds) { for (int appWidgetId : appWidgetIds) {
String name = prefs.getString("widget." + appWidgetId + ".name", null); String name = prefs.getString("widget." + appWidgetId + ".name", null);
long account = prefs.getLong("widget." + appWidgetId + ".account", -1L); long account = prefs.getLong("widget." + appWidgetId + ".account", -1L);
long folder = prefs.getLong("widget." + appWidgetId + ".folder", -1L);
boolean daynight = prefs.getBoolean("widget." + appWidgetId + ".daynight", false); boolean daynight = prefs.getBoolean("widget." + appWidgetId + ".daynight", false);
boolean semi = prefs.getBoolean("widget." + appWidgetId + ".semi", true); boolean semi = prefs.getBoolean("widget." + appWidgetId + ".semi", true);
int background = prefs.getInt("widget." + appWidgetId + ".background", Color.TRANSPARENT); int background = prefs.getInt("widget." + appWidgetId + ".background", Color.TRANSPARENT);
@ -76,7 +77,7 @@ public class Widget extends AppWidgetProvider {
folders = new ArrayList<>(); folders = new ArrayList<>();
PendingIntent pi; PendingIntent pi;
if (folders.size() == 1) { if (folders.size() == 1 || folder >= 0) {
Intent view = new Intent(context, ActivityView.class); Intent view = new Intent(context, ActivityView.class);
view.setAction("folder:" + folders.get(0).id); view.setAction("folder:" + folders.get(0).id);
view.putExtra("account", account); view.putExtra("account", account);
@ -106,8 +107,11 @@ public class Widget extends AppWidgetProvider {
} }
} }
TupleMessageStats stats = db.message().getWidgetUnseen(account < 0 ? null : account); TupleMessageStats stats = db.message().getWidgetUnseen(
EntityLog.log(context, "Widget account=" + account + " ignore=" + unseen_ignored + " " + stats); account < 0 ? null : account,
folder < 0 ? null : folder);
EntityLog.log(context, "Widget account=" + account + " folder=" + folder +
" ignore=" + unseen_ignored + " " + stats);
Integer unseen = (unseen_ignored ? stats.notifying : stats.unseen); Integer unseen = (unseen_ignored ? stats.notifying : stats.unseen);
if (unseen == null) if (unseen == null)

@ -30,6 +30,24 @@
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvAccount" /> app:layout_constraintTop_toBottomOf="@id/tvAccount" />
<TextView
android:id="@+id/tvFolder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="@string/title_widget_folder"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/spAccount" />
<Spinner
android:id="@+id/spFolder"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvFolder" />
<CheckBox <CheckBox
android:id="@+id/cbDayNight" android:id="@+id/cbDayNight"
android:layout_width="wrap_content" android:layout_width="wrap_content"
@ -39,7 +57,7 @@
android:text="@string/title_widget_day_night" android:text="@string/title_widget_day_night"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" android:textAppearance="@style/TextAppearance.AppCompat.Medium"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/spAccount" /> app:layout_constraintTop_toBottomOf="@id/spFolder" />
<CheckBox <CheckBox
android:id="@+id/cbSemiTransparent" android:id="@+id/cbSemiTransparent"

@ -2330,6 +2330,7 @@
<string name="title_widget_account_all">All</string> <string name="title_widget_account_all">All</string>
<string name="title_widget_folder">Folder</string> <string name="title_widget_folder">Folder</string>
<string name="title_widget_folder_unified">Unified inbox folders</string> <string name="title_widget_folder_unified">Unified inbox folders</string>
<string name="title_widget_folder_notifying">Folders with new message notifications</string>
<string name="title_widget_unseen">Unread messages only</string> <string name="title_widget_unseen">Unread messages only</string>
<string name="title_widget_flagged">Starred messages only</string> <string name="title_widget_flagged">Starred messages only</string>
<string name="title_widget_font_size">Text size</string> <string name="title_widget_font_size">Text size</string>

Loading…
Cancel
Save