Skip to content

Commit

Permalink
Merge pull request #3333 from nextcloud/rememberFilter
Browse files Browse the repository at this point in the history
Save filter state
  • Loading branch information
AndyScherzinger authored Sep 21, 2023
2 parents 28ec0ba + 935eb09 commit dec27e9
Show file tree
Hide file tree
Showing 13 changed files with 270 additions and 64 deletions.
139 changes: 139 additions & 0 deletions app/schemas/com.nextcloud.talk.data.source.local.TalkDatabase/9.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
{
"formatVersion": 1,
"database": {
"version": 9,
"identityHash": "666fcc4bbbdf3ff121b8f1ace8fcbcb8",
"entities": [
{
"tableName": "User",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `userId` TEXT, `username` TEXT, `baseUrl` TEXT, `token` TEXT, `displayName` TEXT, `pushConfigurationState` TEXT, `capabilities` TEXT, `clientCertificate` TEXT, `externalSignalingServer` TEXT, `current` INTEGER NOT NULL, `scheduledForDeletion` INTEGER NOT NULL)",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "userId",
"columnName": "userId",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "username",
"columnName": "username",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "baseUrl",
"columnName": "baseUrl",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "token",
"columnName": "token",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "displayName",
"columnName": "displayName",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "pushConfigurationState",
"columnName": "pushConfigurationState",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "capabilities",
"columnName": "capabilities",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "clientCertificate",
"columnName": "clientCertificate",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "externalSignalingServer",
"columnName": "externalSignalingServer",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "current",
"columnName": "current",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "scheduledForDeletion",
"columnName": "scheduledForDeletion",
"affinity": "INTEGER",
"notNull": true
}
],
"primaryKey": {
"autoGenerate": true,
"columnNames": [
"id"
]
},
"indices": [],
"foreignKeys": []
},
{
"tableName": "ArbitraryStorage",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`accountIdentifier` INTEGER NOT NULL, `key` TEXT NOT NULL, `object` TEXT, `value` TEXT, PRIMARY KEY(`accountIdentifier`, `key`))",
"fields": [
{
"fieldPath": "accountIdentifier",
"columnName": "accountIdentifier",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "key",
"columnName": "key",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "storageObject",
"columnName": "object",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "value",
"columnName": "value",
"affinity": "TEXT",
"notNull": false
}
],
"primaryKey": {
"autoGenerate": false,
"columnNames": [
"accountIdentifier",
"key"
]
},
"indices": [],
"foreignKeys": []
}
],
"views": [],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '666fcc4bbbdf3ff121b8f1ace8fcbcb8')"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import com.nextcloud.talk.data.storage.model.ArbitraryStorage
import io.reactivex.Maybe

class ArbitraryStorageManager(private val arbitraryStoragesRepository: ArbitraryStoragesRepository) {
fun storeStorageSetting(accountIdentifier: Long, key: String?, value: String?, objectString: String?) {
fun storeStorageSetting(accountIdentifier: Long, key: String, value: String?, objectString: String?) {
arbitraryStoragesRepository.saveArbitraryStorage(ArbitraryStorage(accountIdentifier, key, objectString, value))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ import com.nextcloud.talk.adapters.items.MessageResultItem
import com.nextcloud.talk.adapters.items.MessagesTextHeaderItem
import com.nextcloud.talk.api.NcApi
import com.nextcloud.talk.application.NextcloudTalkApplication
import com.nextcloud.talk.arbitrarystorage.ArbitraryStorageManager
import com.nextcloud.talk.chat.ChatActivity
import com.nextcloud.talk.contacts.ContactsActivity
import com.nextcloud.talk.data.user.model.User
Expand Down Expand Up @@ -106,6 +107,7 @@ import com.nextcloud.talk.utils.ClosedInterfaceImpl
import com.nextcloud.talk.utils.FileUtils
import com.nextcloud.talk.utils.Mimetype
import com.nextcloud.talk.utils.ParticipantPermissions
import com.nextcloud.talk.utils.UserIdUtils
import com.nextcloud.talk.utils.bundle.BundleKeys
import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_FORWARD_HIDE_SOURCE_ROOM
import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_FORWARD_MSG_FLAG
Expand Down Expand Up @@ -158,6 +160,9 @@ class ConversationsListActivity :
@Inject
lateinit var platformPermissionUtil: PlatformPermissionUtil

@Inject
lateinit var arbitraryStorageManager: ArbitraryStorageManager

override val appBarLayoutType: AppBarLayoutType
get() = AppBarLayoutType.SEARCH_BAR

Expand Down Expand Up @@ -272,6 +277,65 @@ class ConversationsListActivity :

showSearchOrToolbar()
}
fun filterConversation() {
val accountId = UserIdUtils.getIdForUser(userManager.currentUser.blockingGet())
filterState[FilterConversationFragment.UNREAD] = (
arbitraryStorageManager.getStorageSetting(
accountId,
FilterConversationFragment.UNREAD,
""
).blockingGet()?.value ?: ""
) == "true"

filterState[FilterConversationFragment.MENTION] = (
arbitraryStorageManager.getStorageSetting(
accountId,
FilterConversationFragment.MENTION,
""
).blockingGet()?.value ?: ""
) == "true"

val newItems: MutableList<AbstractFlexibleItem<*>> = ArrayList()
if (filterState[FilterConversationFragment.UNREAD] == false &&
filterState[FilterConversationFragment.MENTION] == false
) {
adapter!!.updateDataSet(conversationItems, true)
} else {
val items = conversationItems
for (i in items) {
val conversation = (i as ConversationItem).model
if (filter(conversation)) {
newItems.add(i)
}
}
adapter!!.updateDataSet(newItems, true)
setFilterableItems(newItems)
}

updateFilterConversationButtonColor()
}

private fun filter(conversation: Conversation): Boolean {
var result = true
for ((k, v) in filterState) {
if (v) {
when (k) {
FilterConversationFragment.MENTION -> result = (result && conversation.unreadMention) ||
(
result &&
(
conversation.type == Conversation.ConversationType.ROOM_TYPE_ONE_TO_ONE_CALL ||
conversation.type == Conversation.ConversationType.FORMER_ONE_TO_ONE
) &&
(conversation.unreadMessages > 0)
)
FilterConversationFragment.UNREAD -> result = result && (conversation.unreadMessages > 0)
}
}
}

return result
}

private fun setupActionBar() {
setSupportActionBar(binding.conversationListToolbar)
Expand Down Expand Up @@ -578,6 +642,7 @@ class ConversationsListActivity :
sortConversations(conversationItems)
sortConversations(conversationItemsWithHeader)
if (!filterState.containsValue(true)) filterableConversationItems = conversationItems
filterConversation()
adapter!!.updateDataSet(filterableConversationItems, false)
Handler().postDelayed({ checkToShowUnreadBubble() }, UNREAD_BUBBLE_DELAY.toLong())
fetchOpenConversations(apiVersion)
Expand Down Expand Up @@ -788,8 +853,6 @@ class ConversationsListActivity :

binding.filterConversationsButton.setOnClickListener {
val newFragment: DialogFragment = FilterConversationFragment.newInstance(
adapter!!,
conversationItems,
filterState,
this
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ object Migrations {
}
}

val MIGRATION_8_9 = object : Migration(8, 9) {
override fun migrate(database: SupportSQLiteDatabase) {
Log.i("Migrations", "Migrating 8 to 9")
migrateToDualPrimaryKeyArbitraryStorage(database)
}
}

fun migrateToRoom(database: SupportSQLiteDatabase) {
database.execSQL(
"CREATE TABLE User_new (" +
Expand Down Expand Up @@ -92,4 +99,29 @@ object Migrations {
database.execSQL("ALTER TABLE User_new RENAME TO User")
database.execSQL("ALTER TABLE ArbitraryStorage_new RENAME TO ArbitraryStorage")
}

fun migrateToDualPrimaryKeyArbitraryStorage(database: SupportSQLiteDatabase) {
database.execSQL(
"CREATE TABLE ArbitraryStorage_dualPK (" +
"accountIdentifier INTEGER NOT NULL, " +
"\"key\" TEXT NOT NULL, " +
"object TEXT, " +
"value TEXT, " +
"PRIMARY KEY(accountIdentifier, \"key\")" +
")"
)
// Copy the data
database.execSQL(
"INSERT INTO ArbitraryStorage_dualPK (" +
"accountIdentifier, \"key\", object, value) " +
"SELECT " +
"accountIdentifier, \"key\", object, value " +
"FROM ArbitraryStorage"
)
// Remove the old table
database.execSQL("DROP TABLE ArbitraryStorage")

// Change the table name to the correct one
database.execSQL("ALTER TABLE ArbitraryStorage_dualPK RENAME TO ArbitraryStorage")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import java.util.Locale

@Database(
entities = [UserEntity::class, ArbitraryStorageEntity::class],
version = 8,
version = 9,
exportSchema = true
)
@TypeConverters(
Expand Down Expand Up @@ -96,7 +96,7 @@ abstract class TalkDatabase : RoomDatabase() {
.databaseBuilder(context.applicationContext, TalkDatabase::class.java, dbName)
// comment out openHelperFactory to view the database entries in Android Studio for debugging
.openHelperFactory(factory)
.addMigrations(Migrations.MIGRATION_6_8, Migrations.MIGRATION_7_8)
.addMigrations(Migrations.MIGRATION_6_8, Migrations.MIGRATION_7_8, Migrations.MIGRATION_8_9)
.allowMainThreadQueries()
.addCallback(
object : RoomDatabase.Callback() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ abstract class ArbitraryStoragesDao {
objectString: String
): Maybe<ArbitraryStorageEntity>

@Query(
"SELECT * FROM ArbitraryStorage"
)
abstract fun getAll(): Maybe<List<ArbitraryStorageEntity>>

@Query("DELETE FROM ArbitraryStorage WHERE accountIdentifier = :accountIdentifier")
abstract fun deleteArbitraryStorage(accountIdentifier: Long): Int

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
package com.nextcloud.talk.data.storage

import com.nextcloud.talk.data.storage.model.ArbitraryStorage
import com.nextcloud.talk.data.storage.model.ArbitraryStorageEntity
import io.reactivex.Maybe

interface ArbitraryStoragesRepository {
fun getStorageSetting(accountIdentifier: Long, key: String, objectString: String): Maybe<ArbitraryStorage>
fun deleteArbitraryStorage(accountIdentifier: Long): Int
fun saveArbitraryStorage(arbitraryStorage: ArbitraryStorage): Long
fun getAll(): Maybe<List<ArbitraryStorageEntity>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package com.nextcloud.talk.data.storage

import com.nextcloud.talk.data.storage.model.ArbitraryStorage
import com.nextcloud.talk.data.storage.model.ArbitraryStorageEntity
import io.reactivex.Maybe

class ArbitraryStoragesRepositoryImpl(private val arbitraryStoragesDao: ArbitraryStoragesDao) :
Expand All @@ -35,6 +36,10 @@ class ArbitraryStoragesRepositoryImpl(private val arbitraryStoragesDao: Arbitrar
.map { ArbitraryStorageMapper.toModel(it) }
}

override fun getAll(): Maybe<List<ArbitraryStorageEntity>> {
return arbitraryStoragesDao.getAll()
}

override fun deleteArbitraryStorage(accountIdentifier: Long): Int {
return arbitraryStoragesDao.deleteArbitraryStorage(accountIdentifier)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import kotlinx.parcelize.Parcelize

@Parcelize
data class ArbitraryStorage(
var accountIdentifier: Long = 0,
var key: String? = null,
var accountIdentifier: Long,
var key: String,
var storageObject: String? = null,
var value: String? = null
) : Parcelable
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,16 @@ package com.nextcloud.talk.data.storage.model
import android.os.Parcelable
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import kotlinx.parcelize.Parcelize

@Parcelize
@Entity(tableName = "ArbitraryStorage")
@Entity(tableName = "ArbitraryStorage", primaryKeys = ["accountIdentifier", "key"])
data class ArbitraryStorageEntity(
@PrimaryKey
@ColumnInfo(name = "accountIdentifier")
var accountIdentifier: Long = 0,

@ColumnInfo(name = "key")
var key: String? = null,
var key: String = "",

@ColumnInfo(name = "object")
var storageObject: String? = null,
Expand Down
Loading

0 comments on commit dec27e9

Please sign in to comment.