convert java code to kotlin - DatabaseStorageModule

Signed-off-by: sowjanyakch <sowjanya.kch@gmail.com>
This commit is contained in:
sowjanyakch 2024-11-15 20:06:25 +01:00 committed by Marcel Hibbe
parent 9e8a5476f9
commit 77b55dcd9b
No known key found for this signature in database
GPG Key ID: C793F8B59F43CE7B

View File

@ -6,237 +6,239 @@
* SPDX-FileCopyrightText: 2017-2018 Mario Danic <mario@lovelyhq.com> * SPDX-FileCopyrightText: 2017-2018 Mario Danic <mario@lovelyhq.com>
* SPDX-License-Identifier: GPL-3.0-or-later * SPDX-License-Identifier: GPL-3.0-or-later
*/ */
package com.nextcloud.talk.utils.preferences.preferencestorage; package com.nextcloud.talk.utils.preferences.preferencestorage
import android.text.TextUtils; import android.text.TextUtils
import android.util.Log; import android.util.Log
import autodagger.AutoInjector
import com.nextcloud.talk.api.NcApi
import com.nextcloud.talk.application.NextcloudTalkApplication
import com.nextcloud.talk.application.NextcloudTalkApplication.Companion.sharedApplication
import com.nextcloud.talk.arbitrarystorage.ArbitraryStorageManager
import com.nextcloud.talk.data.storage.model.ArbitraryStorage
import com.nextcloud.talk.data.user.model.User
import com.nextcloud.talk.models.json.generic.GenericOverall
import com.nextcloud.talk.utils.ApiUtils
import com.nextcloud.talk.utils.ApiUtils.getConversationApiVersion
import com.nextcloud.talk.utils.ApiUtils.getCredentials
import com.nextcloud.talk.utils.ApiUtils.getUrlForMessageExpiration
import com.nextcloud.talk.utils.ApiUtils.getUrlForRoomNotificationCalls
import com.nextcloud.talk.utils.ApiUtils.getUrlForRoomNotificationLevel
import com.nextcloud.talk.utils.CapabilitiesUtil.hasSpreedFeatureCapability
import com.nextcloud.talk.utils.SpreedFeatures
import com.nextcloud.talk.utils.UserIdUtils.getIdForUser
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import javax.inject.Inject
import com.nextcloud.talk.api.NcApi; @AutoInjector(NextcloudTalkApplication::class)
import com.nextcloud.talk.application.NextcloudTalkApplication; class DatabaseStorageModule(conversationUser: User, conversationToken: String) {
import com.nextcloud.talk.arbitrarystorage.ArbitraryStorageManager; @JvmField
import com.nextcloud.talk.data.storage.model.ArbitraryStorage;
import com.nextcloud.talk.data.user.model.User;
import com.nextcloud.talk.models.json.generic.GenericOverall;
import com.nextcloud.talk.utils.ApiUtils;
import com.nextcloud.talk.utils.SpreedFeatures;
import com.nextcloud.talk.utils.UserIdUtils;
import com.nextcloud.talk.utils.CapabilitiesUtil;
import javax.inject.Inject;
import autodagger.AutoInjector;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
@AutoInjector(NextcloudTalkApplication.class)
public class DatabaseStorageModule {
private static final String TAG = "DatabaseStorageModule";
@Inject @Inject
ArbitraryStorageManager arbitraryStorageManager; var arbitraryStorageManager: ArbitraryStorageManager? = null
@JvmField
@Inject @Inject
NcApi ncApi; var ncApi: NcApi? = null
private int messageExpiration; private var messageExpiration = 0
private final User conversationUser; private val conversationUser: User
private final String conversationToken; private val conversationToken: String
private final long accountIdentifier; private val accountIdentifier: Long
private boolean lobbyValue; private var lobbyValue = false
private String messageNotificationLevel; private var messageNotificationLevel: String? = null
public DatabaseStorageModule(User conversationUser, String conversationToken) { init {
NextcloudTalkApplication.Companion.getSharedApplication().getComponentApplication().inject(this); sharedApplication!!.componentApplication.inject(this)
this.conversationUser = conversationUser; this.conversationUser = conversationUser
this.accountIdentifier = UserIdUtils.INSTANCE.getIdForUser(conversationUser); this.accountIdentifier = getIdForUser(conversationUser)
this.conversationToken = conversationToken; this.conversationToken = conversationToken
} }
public void saveBoolean(String key, boolean value) { fun saveBoolean(key: String, value: Boolean) {
if ("call_notifications_switch".equals(key)) { if ("call_notifications_switch" == key) {
int apiVersion = ApiUtils.getConversationApiVersion(conversationUser, new int[]{4}); val apiVersion = getConversationApiVersion(conversationUser, intArrayOf(4))
ncApi.notificationCalls(ApiUtils.getCredentials(conversationUser.getUsername(), ncApi!!.notificationCalls(
conversationUser.getToken()), getCredentials(
ApiUtils.getUrlForRoomNotificationCalls(apiVersion, conversationUser.username,
conversationUser.getBaseUrl(), conversationUser.token
conversationToken), ),
value ? 1 : 0) getUrlForRoomNotificationCalls(
apiVersion,
conversationUser.baseUrl,
conversationToken
),
if (value) 1 else 0
)
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<GenericOverall>() { .subscribe(
@Override object : Observer<GenericOverall> {
public void onSubscribe(@NonNull Disposable d) { override fun onSubscribe(d: Disposable) {
// unused atm // unused atm
} }
@Override override fun onNext(genericOverall: GenericOverall) {
public void onNext(@NonNull GenericOverall genericOverall) { Log.d(TAG, "Toggled notification calls")
Log.d(TAG, "Toggled notification calls"); }
}
@Override override fun onError(e: Throwable) {
public void onError(@NonNull Throwable e) { Log.e(TAG, "Error when trying to toggle notification calls", e)
Log.e(TAG, "Error when trying to toggle notification calls", e); }
}
@Override override fun onComplete() {
public void onComplete() { // unused atm
// unused atm }
} }
} )
);
} }
if (!"lobby_switch".equals(key)) { if ("lobby_switch" != key) {
arbitraryStorageManager.storeStorageSetting(accountIdentifier, arbitraryStorageManager!!.storeStorageSetting(
key, accountIdentifier,
Boolean.toString(value), key,
conversationToken); value.toString(),
conversationToken
)
} else { } else {
lobbyValue = value; lobbyValue = value
} }
} }
public void saveString(String key, String value) { fun saveString(key: String, value: String) {
if ("conversation_settings_dropdown".equals(key)) { if ("conversation_settings_dropdown" == key) {
int apiVersion = ApiUtils.getConversationApiVersion(conversationUser, new int[]{4}); val apiVersion = getConversationApiVersion(conversationUser, intArrayOf(4))
String trimmedValue = value.replace("expire_", ""); val trimmedValue = value.replace("expire_", "")
int valueInt = Integer.parseInt(trimmedValue); val valueInt = trimmedValue.toInt()
ncApi.setMessageExpiration( ncApi!!.setMessageExpiration(
ApiUtils.getCredentials( getCredentials(
conversationUser.getUsername(), conversationUser.username,
conversationUser.getToken()), conversationUser.token
ApiUtils.getUrlForMessageExpiration( ),
apiVersion, getUrlForMessageExpiration(
conversationUser.getBaseUrl(), apiVersion,
conversationToken), conversationUser.baseUrl,
valueInt) conversationToken
),
valueInt
)
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<GenericOverall>() { .subscribe(object : Observer<GenericOverall> {
@Override override fun onSubscribe(d: Disposable) {
public void onSubscribe(@NonNull Disposable d) {
// unused atm // unused atm
} }
@Override override fun onNext(genericOverall: GenericOverall) {
public void onNext(@NonNull GenericOverall genericOverall) { messageExpiration = valueInt
messageExpiration = valueInt;
} }
@Override override fun onError(e: Throwable) {
public void onError(@NonNull Throwable e) { Log.e(TAG, "Error when trying to set message expiration", e)
Log.e(TAG, "Error when trying to set message expiration", e);
} }
@Override override fun onComplete() {
public void onComplete() {
// unused atm // unused atm
} }
}); })
} else if ("conversation_info_message_notifications_dropdown" == key) {
} else if ("conversation_info_message_notifications_dropdown".equals(key)) { if (hasSpreedFeatureCapability(
if (CapabilitiesUtil.hasSpreedFeatureCapability( conversationUser.capabilities!!.spreedCapability!!,
conversationUser.getCapabilities().getSpreedCapability(), SpreedFeatures.NOTIFICATION_LEVELS
SpreedFeatures.NOTIFICATION_LEVELS) )
) { ) {
if (TextUtils.isEmpty(messageNotificationLevel) || !messageNotificationLevel.equals(value)) { if (TextUtils.isEmpty(messageNotificationLevel) || messageNotificationLevel != value) {
int intValue; val intValue = when (value) {
switch (value) { "never" -> 3
case "never": "mention" -> 2
intValue = 3; "always" -> 1
break; else -> 0
case "mention":
intValue = 2;
break;
case "always":
intValue = 1;
break;
default:
intValue = 0;
} }
int apiVersion = ApiUtils.getConversationApiVersion(conversationUser, new int[]{ApiUtils.API_V4, 1}); val apiVersion = getConversationApiVersion(conversationUser, intArrayOf(ApiUtils.API_V4, 1))
ncApi.setNotificationLevel(ApiUtils.getCredentials(conversationUser.getUsername(), ncApi!!.setNotificationLevel(
conversationUser.getToken()), getCredentials(
ApiUtils.getUrlForRoomNotificationLevel(apiVersion, conversationUser.username,
conversationUser.getBaseUrl(), conversationUser.token
conversationToken), ),
intValue) getUrlForRoomNotificationLevel(
apiVersion,
conversationUser.baseUrl,
conversationToken
),
intValue
)
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.subscribe(new Observer<GenericOverall>() { .subscribe(object : Observer<GenericOverall?> {
@Override override fun onSubscribe(d: Disposable) {
public void onSubscribe(Disposable d) {
// unused atm // unused atm
} }
@Override override fun onNext(p0: GenericOverall) {
public void onNext(GenericOverall genericOverall) {messageNotificationLevel = value;} messageNotificationLevel = value
}
@Override override fun onError(e: Throwable) {
public void onError(Throwable e) {
// unused atm // unused atm
} }
@Override override fun onComplete() {
public void onComplete() {
// unused atm // unused atm
} }
}); })
} else { } else {
messageNotificationLevel = value; messageNotificationLevel = value
} }
} }
} else { } else {
arbitraryStorageManager.storeStorageSetting(accountIdentifier, key, value, conversationToken); arbitraryStorageManager!!.storeStorageSetting(accountIdentifier, key, value, conversationToken)
}
}
public boolean getBoolean(String key, boolean defaultVal) {
if ("lobby_switch".equals(key)) {
return lobbyValue;
} else {
return arbitraryStorageManager
.getStorageSetting(accountIdentifier, key, conversationToken)
.map(arbitraryStorage -> Boolean.parseBoolean(arbitraryStorage.getValue()))
.blockingGet(defaultVal);
} }
} }
public String getString(String key, String defaultVal) { fun getBoolean(key: String, defaultVal: Boolean): Boolean {
if ("conversation_settings_dropdown".equals(key)) { return if ("lobby_switch" == key) {
switch (messageExpiration) { lobbyValue
case 2419200: } else {
return "expire_2419200"; arbitraryStorageManager!!
case 604800: .getStorageSetting(accountIdentifier, key, conversationToken)
return "expire_604800"; .map { arbitraryStorage: ArbitraryStorage -> arbitraryStorage.value.toBoolean() }
case 86400: .blockingGet(defaultVal)
return "expire_86400"; }
case 28800: }
return "expire_28800";
case 3600: fun getString(key: String, defaultVal: String): String? {
return "expire_3600"; return if ("conversation_settings_dropdown" == key) {
default: when (messageExpiration) {
return "expire_0"; 2419200 -> "expire_2419200"
604800 -> "expire_604800"
86400 -> "expire_86400"
28800 -> "expire_28800"
3600 -> "expire_3600"
else -> "expire_0"
} }
} else if ("conversation_info_message_notifications_dropdown".equals(key)) { } else if ("conversation_info_message_notifications_dropdown" == key) {
return messageNotificationLevel; messageNotificationLevel
} else { } else {
return arbitraryStorageManager arbitraryStorageManager!!
.getStorageSetting(accountIdentifier, key, conversationToken) .getStorageSetting(accountIdentifier, key, conversationToken)
.map(ArbitraryStorage::getValue) .map(ArbitraryStorage::value)
.blockingGet(defaultVal); .blockingGet(defaultVal)
} }
} }
public void setMessageExpiration(int messageExpiration) { fun setMessageExpiration(messageExpiration: Int) {
this.messageExpiration = messageExpiration; this.messageExpiration = messageExpiration
}
companion object {
private const val TAG = "DatabaseStorageModule"
} }
} }