From 0bdad9ad7ad9ac737872788f23f002fd8ee4e09e Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Tue, 1 Dec 2020 03:01:10 +0300 Subject: [PATCH] Notifications: add new virtual type for subscriptions --- app/src/husky/res/values/strings.xml | 4 ++ .../notifications/NotificationHelper.java | 39 +++++++++++++++---- .../NotificationPreferencesFragment.kt | 11 ++++++ .../keylesspalace/tusky/db/AccountEntity.kt | 1 + .../keylesspalace/tusky/db/AppDatabase.java | 9 ++++- .../com/keylesspalace/tusky/di/AppModule.kt | 3 +- .../tusky/settings/SettingsConstants.kt | 1 + 7 files changed, 59 insertions(+), 9 deletions(-) diff --git a/app/src/husky/res/values/strings.xml b/app/src/husky/res/values/strings.xml index 8c1fe345..a03ac32c 100644 --- a/app/src/husky/res/values/strings.xml +++ b/app/src/husky/res/values/strings.xml @@ -34,6 +34,9 @@ %s sent you a message Chat Messages Notifications about new chat messages + %s published a new post + Subscriptions + Notifications when somebody you\'re subscribed to published a new post Other Privacy @@ -44,6 +47,7 @@ Default formatting syntax(if supported by instance) my posts are reacted with emojis received a chat message + somebody I\'m subscribed to published a new post Hide muted users Enable bigger custom emojis Enable experimental Pleroma-FE stickers(if available) diff --git a/app/src/main/java/com/keylesspalace/tusky/components/notifications/NotificationHelper.java b/app/src/main/java/com/keylesspalace/tusky/components/notifications/NotificationHelper.java index ee2bbf92..7cc029ff 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/notifications/NotificationHelper.java +++ b/app/src/main/java/com/keylesspalace/tusky/components/notifications/NotificationHelper.java @@ -128,7 +128,7 @@ public class NotificationHelper { public static final String CHANNEL_POLL = "CHANNEL_POLL"; public static final String CHANNEL_EMOJI_REACTION = "CHANNEL_EMOJI_REACTION"; public static final String CHANNEL_CHAT_MESSAGES = "CHANNEL_CHAT_MESSAGES"; - + public static final String CHANNEL_SUBSCRIPTIONS = "CHANNEL_SUBSCRIPTIONS"; /** * WorkManager Tag @@ -400,7 +400,8 @@ public class NotificationHelper { CHANNEL_FAVOURITE + account.getIdentifier(), CHANNEL_POLL + account.getIdentifier(), CHANNEL_EMOJI_REACTION + account.getIdentifier(), - CHANNEL_CHAT_MESSAGES + account.getIdentifier() + CHANNEL_CHAT_MESSAGES + account.getIdentifier(), + CHANNEL_SUBSCRIPTIONS + account.getIdentifier() }; int[] channelNames = { R.string.notification_mention_name, @@ -411,6 +412,7 @@ public class NotificationHelper { R.string.notification_poll_name, R.string.notification_emoji_name, R.string.notification_chat_message_name, + R.string.notification_subscription_name }; int[] channelDescriptions = { R.string.notification_mention_descriptions, @@ -421,9 +423,10 @@ public class NotificationHelper { R.string.notification_poll_description, R.string.notification_emoji_description, R.string.notification_chat_message_description, + R.string.notification_subscription_description }; - List channels = new ArrayList<>(6); + List channels = new ArrayList<>(9); NotificationChannelGroup channelGroup = new NotificationChannelGroup(account.getIdentifier(), account.getFullName()); @@ -565,7 +568,9 @@ public class NotificationHelper { switch (notification.getType()) { case MENTION: - return account.getNotificationsMentioned(); + if(isMentionedInNotification(notification, account.getAccountId())) + return account.getNotificationsMentioned(); + else return account.getNotificationsSubscriptions(); case FOLLOW: return account.getNotificationsFollowed(); case FOLLOW_REQUEST: @@ -589,7 +594,9 @@ public class NotificationHelper { private static String getChannelId(AccountEntity account, Notification notification) { switch (notification.getType()) { case MENTION: - return CHANNEL_MENTION + account.getIdentifier(); + if(isMentionedInNotification(notification, account.getAccountId())) + return CHANNEL_MENTION + account.getIdentifier(); + else return CHANNEL_SUBSCRIPTIONS + account.getIdentifier(); case FOLLOW: return CHANNEL_FOLLOW + account.getIdentifier(); case FOLLOW_REQUEST: @@ -656,14 +663,32 @@ public class NotificationHelper { return null; } + + private static boolean isMentionedInNotification(Notification not, String id) { + if(not.getStatus() != null) { + for(int i = 0; i < not.getStatus().getMentions().length; i++) { + if(not.getStatus().getMentions()[i].getId().equals(id)) { + return true; + } + } + + return false; + } + + return true; // actually should never happen, true just in case someone breaks API again + } @Nullable private static String titleForType(Context context, Notification notification, AccountEntity account) { String accountName = StringUtils.unicodeWrap(notification.getAccount().getName()); switch (notification.getType()) { case MENTION: - return String.format(context.getString(R.string.notification_mention_format), - accountName); + if(isMentionedInNotification(notification, account.getAccountId())) { + return String.format(context.getString(R.string.notification_mention_format), + accountName); + } else { + return String.format(context.getString(R.string.notification_subscription_format), accountName); + } case FOLLOW: return String.format(context.getString(R.string.notification_follow_format), accountName); diff --git a/app/src/main/java/com/keylesspalace/tusky/components/preference/NotificationPreferencesFragment.kt b/app/src/main/java/com/keylesspalace/tusky/components/preference/NotificationPreferencesFragment.kt index 35f6ded3..a3310067 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/preference/NotificationPreferencesFragment.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/preference/NotificationPreferencesFragment.kt @@ -133,6 +133,17 @@ class NotificationPreferencesFragment : PreferenceFragmentCompat(), Injectable { true } } + + switchPreference { + setTitle(R.string.pref_title_notification_filter_subscriptions) + key = PrefKeys.NOTIFICATION_FILTER_SUBSCRIPTIONS + isIconSpaceReserved = false + isChecked = activeAccount.notificationsSubscriptions + setOnPreferenceChangeListener { _, newValue -> + updateAccount { it.notificationsSubscriptions = newValue as Boolean } + true + } + } } preferenceCategory(R.string.pref_title_notification_alerts) { category -> diff --git a/app/src/main/java/com/keylesspalace/tusky/db/AccountEntity.kt b/app/src/main/java/com/keylesspalace/tusky/db/AccountEntity.kt index 01cd8ac1..6d1b415b 100644 --- a/app/src/main/java/com/keylesspalace/tusky/db/AccountEntity.kt +++ b/app/src/main/java/com/keylesspalace/tusky/db/AccountEntity.kt @@ -46,6 +46,7 @@ data class AccountEntity(@field:PrimaryKey(autoGenerate = true) var id: Long, var notificationsPolls: Boolean = true, var notificationsEmojiReactions: Boolean = true, var notificationsChatMessages: Boolean = true, + var notificationsSubscriptions: Boolean = true, var notificationSound: Boolean = true, var notificationVibration: Boolean = true, var notificationLight: Boolean = true, diff --git a/app/src/main/java/com/keylesspalace/tusky/db/AppDatabase.java b/app/src/main/java/com/keylesspalace/tusky/db/AppDatabase.java index 92d3b844..51f90fd1 100644 --- a/app/src/main/java/com/keylesspalace/tusky/db/AppDatabase.java +++ b/app/src/main/java/com/keylesspalace/tusky/db/AppDatabase.java @@ -29,7 +29,7 @@ import androidx.annotation.NonNull; */ @Database(entities = {TootEntity.class, AccountEntity.class, InstanceEntity.class, TimelineStatusEntity.class, - TimelineAccountEntity.class, ConversationEntity.class, ChatEntity.class, ChatMessageEntity.class}, version = 25) + TimelineAccountEntity.class, ConversationEntity.class, ChatEntity.class, ChatMessageEntity.class}, version = 26) public abstract class AppDatabase extends RoomDatabase { public abstract TootDao tootDao(); @@ -376,4 +376,11 @@ public abstract class AppDatabase extends RoomDatabase { database.execSQL("ALTER TABLE `TimelineStatusEntity` ADD COLUMN `pleroma` TEXT"); } }; + + public static final Migration MIGRATION_25_26 = new Migration(25, 26) { + @Override + public void migrate(@NonNull SupportSQLiteDatabase database) { + database.execSQL("ALTER TABLE `AccountEntity` ADD COLUMN `notificationsSubscriptions` INTEGER NOT NULL DEFAULT 1"); + } + }; } diff --git a/app/src/main/java/com/keylesspalace/tusky/di/AppModule.kt b/app/src/main/java/com/keylesspalace/tusky/di/AppModule.kt index ecf0134a..9b27404e 100644 --- a/app/src/main/java/com/keylesspalace/tusky/di/AppModule.kt +++ b/app/src/main/java/com/keylesspalace/tusky/di/AppModule.kt @@ -80,7 +80,8 @@ class AppModule { AppDatabase.MIGRATION_13_14, AppDatabase.MIGRATION_14_15, AppDatabase.MIGRATION_15_16, AppDatabase.MIGRATION_16_17, AppDatabase.MIGRATION_17_18, AppDatabase.MIGRATION_18_19, AppDatabase.MIGRATION_19_20, AppDatabase.MIGRATION_20_21, AppDatabase.MIGRATION_21_22, - AppDatabase.MIGRATION_22_23, AppDatabase.MIGRATION_23_24, AppDatabase.MIGRATION_24_25) + AppDatabase.MIGRATION_22_23, AppDatabase.MIGRATION_23_24, AppDatabase.MIGRATION_24_25, + AppDatabase.MIGRATION_25_26) .build() } diff --git a/app/src/main/java/com/keylesspalace/tusky/settings/SettingsConstants.kt b/app/src/main/java/com/keylesspalace/tusky/settings/SettingsConstants.kt index 8aeed370..1981b8e0 100644 --- a/app/src/main/java/com/keylesspalace/tusky/settings/SettingsConstants.kt +++ b/app/src/main/java/com/keylesspalace/tusky/settings/SettingsConstants.kt @@ -61,6 +61,7 @@ object PrefKeys { const val NOTIFICATION_FILTER_REBLOGS = "notificationFilterReblogs" const val NOTIFICATION_FILTER_FOLLOW_REQUESTS = "notificationFilterFollowRequests" const val NOTIFICATION_FILTER_EMOJI_REACTIONS = "notificationFilterEmojis" + const val NOTIFICATION_FILTER_SUBSCRIPTIONS = "notificationFilterSubscriptions" const val NOTIFICATIONS_FILTER_FOLLOWS = "notificationFilterFollows" const val TAB_FILTER_HOME_REPLIES = "tabFilterHomeReplies"