Bladeren bron

Improve notification level storage

Signed-off-by: Mario Danic <mario@lovelyhq.com>
Mario Danic 6 jaren geleden
bovenliggende
commit
d43c37d88b

+ 52 - 42
app/src/main/java/com/nextcloud/talk/utils/preferencestorage/DatabaseStorageModule.java

@@ -21,6 +21,7 @@
 package com.nextcloud.talk.utils.preferencestorage;
 package com.nextcloud.talk.utils.preferencestorage;
 
 
 import android.os.Bundle;
 import android.os.Bundle;
+import android.text.TextUtils;
 import autodagger.AutoInjector;
 import autodagger.AutoInjector;
 import com.nextcloud.talk.api.NcApi;
 import com.nextcloud.talk.api.NcApi;
 import com.nextcloud.talk.application.NextcloudTalkApplication;
 import com.nextcloud.talk.application.NextcloudTalkApplication;
@@ -49,6 +50,7 @@ public class DatabaseStorageModule implements StorageModule {
     private String conversationToken;
     private String conversationToken;
     private long accountIdentifier;
     private long accountIdentifier;
 
 
+    private String messageNotificationLevel;
     public DatabaseStorageModule(UserEntity conversationUser, String conversationToken) {
     public DatabaseStorageModule(UserEntity conversationUser, String conversationToken) {
         NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this);
         NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this);
 
 
@@ -68,45 +70,49 @@ public class DatabaseStorageModule implements StorageModule {
             arbitraryStorageUtils.storeStorageSetting(accountIdentifier, key, value, conversationToken);
             arbitraryStorageUtils.storeStorageSetting(accountIdentifier, key, value, conversationToken);
         } else {
         } else {
             if (conversationUser.hasSpreedCapabilityWithName("notification-levels")) {
             if (conversationUser.hasSpreedCapabilityWithName("notification-levels")) {
-                int intValue;
-                switch (value) {
-                    case "never":
-                        intValue = 3;
-                        break;
-                    case "mention":
-                        intValue = 2;
-                        break;
-                    case "always":
-                        intValue = 1;
-                        break;
-                    default:
-                        intValue = 0;
+                if (!TextUtils.isEmpty(messageNotificationLevel) && !messageNotificationLevel.equals(value)) {
+                    int intValue;
+                    switch (value) {
+                        case "never":
+                            intValue = 3;
+                            break;
+                        case "mention":
+                            intValue = 2;
+                            break;
+                        case "always":
+                            intValue = 1;
+                            break;
+                        default:
+                            intValue = 0;
+                    }
+
+                    ncApi.setNotificationLevel(ApiUtils.getCredentials(conversationUser.getUsername(), conversationUser.getToken()),
+                            ApiUtils.getUrlForSettingNotificationlevel(conversationUser.getBaseUrl(), conversationToken),
+                            intValue)
+                            .subscribeOn(Schedulers.newThread())
+                            .subscribe(new Observer<GenericOverall>() {
+                                @Override
+                                public void onSubscribe(Disposable d) {
+
+                                }
+
+                                @Override
+                                public void onNext(GenericOverall genericOverall) {
+                                    messageNotificationLevel = value;
+                                }
+
+                                @Override
+                                public void onError(Throwable e) {
+
+                                }
+
+                                @Override
+                                public void onComplete() {
+                                }
+                            });
+                } else {
+                    messageNotificationLevel = value;
                 }
                 }
-
-                ncApi.setNotificationLevel(ApiUtils.getCredentials(conversationUser.getUsername(), conversationUser.getToken()),
-                        ApiUtils.getUrlForSettingNotificationlevel(conversationUser.getBaseUrl(), conversationToken),
-                        intValue)
-                        .subscribeOn(Schedulers.newThread())
-                        .subscribe(new Observer<GenericOverall>() {
-                            @Override
-                            public void onSubscribe(Disposable d) {
-
-                            }
-
-                            @Override
-                            public void onNext(GenericOverall genericOverall) {
-
-                            }
-
-                            @Override
-                            public void onError(Throwable e) {
-
-                            }
-
-                            @Override
-                            public void onComplete() {
-                            }
-                        });
             }
             }
         }
         }
     }
     }
@@ -133,11 +139,15 @@ public class DatabaseStorageModule implements StorageModule {
 
 
     @Override
     @Override
     public String getString(String key, String defaultVal) {
     public String getString(String key, String defaultVal) {
-        ArbitraryStorageEntity valueFromDb = arbitraryStorageUtils.getStorageSetting(accountIdentifier, key, conversationToken);
-        if (valueFromDb == null) {
-            return defaultVal;
+        if (!key.equals("message_notification_level")) {
+            ArbitraryStorageEntity valueFromDb = arbitraryStorageUtils.getStorageSetting(accountIdentifier, key, conversationToken);
+            if (valueFromDb == null) {
+                return defaultVal;
+            } else {
+                return valueFromDb.getValue();
+            }
         } else {
         } else {
-            return valueFromDb.getValue();
+            return messageNotificationLevel;
         }
         }
     }
     }