Skip to content

Commit

Permalink
Save filter state
Browse files Browse the repository at this point in the history
Signed-off-by: tobiasKaminsky <[email protected]>
  • Loading branch information
tobiasKaminsky committed Sep 21, 2023
1 parent 53ff6e7 commit 94d3e5d
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,10 @@ 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.storage.model.ArbitraryStorage
import com.nextcloud.talk.data.user.model.User
import com.nextcloud.talk.databinding.ControllerConversationsRvBinding
import com.nextcloud.talk.events.ConversationsListFetchDataEvent
Expand All @@ -106,6 +108,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 All @@ -120,6 +123,7 @@ import com.nextcloud.talk.utils.database.user.CapabilitiesUtilNew.isServerEOL
import com.nextcloud.talk.utils.database.user.CapabilitiesUtilNew.isUnifiedSearchAvailable
import com.nextcloud.talk.utils.database.user.CapabilitiesUtilNew.isUserStatusAvailable
import com.nextcloud.talk.utils.permissions.PlatformPermissionUtil
import com.nextcloud.talk.utils.preferences.AppPreferences
import com.nextcloud.talk.utils.rx.SearchViewObservable.Companion.observeSearchView
import com.nextcloud.talk.utils.singletons.ApplicationWideCurrentRoomHolder
import eu.davidea.flexibleadapter.FlexibleAdapter
Expand Down Expand Up @@ -272,6 +276,50 @@ class ConversationsListActivity :

showSearchOrToolbar()
}
fun filterConversation() {
filterState[FilterConversationFragment.UNREAD] = appPreferences.isUnreadFilterEnabled
filterState[FilterConversationFragment.MENTION] = appPreferences.isMentionFilterEnabled

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 +626,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 +837,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 @@ -30,26 +30,26 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.nextcloud.talk.R
import com.nextcloud.talk.adapters.items.ConversationItem
import com.nextcloud.talk.application.NextcloudTalkApplication
import com.nextcloud.talk.arbitrarystorage.ArbitraryStorageManager
import com.nextcloud.talk.conversationlist.ConversationsListActivity
import com.nextcloud.talk.databinding.DialogFilterConversationBinding
import com.nextcloud.talk.models.json.conversations.Conversation
import com.nextcloud.talk.ui.theme.ViewThemeUtils
import com.nextcloud.talk.users.UserManager
import com.nextcloud.talk.utils.UserIdUtils
import com.nextcloud.talk.utils.UserIdUtils.getIdForUser
import com.nextcloud.talk.utils.preferences.AppPreferences
import eu.davidea.flexibleadapter.FlexibleAdapter
import eu.davidea.flexibleadapter.items.AbstractFlexibleItem
import javax.inject.Inject

@AutoInjector(NextcloudTalkApplication::class)
class FilterConversationFragment(
adapter: FlexibleAdapter<AbstractFlexibleItem<*>>,
currentConversations: MutableList<AbstractFlexibleItem<*>>,
savedFilterState: MutableMap<String, Boolean>,
conversationsListActivity: ConversationsListActivity
) : DialogFragment() {
lateinit var binding: DialogFilterConversationBinding
private var dialogView: View? = null
private var currentAdapter: FlexibleAdapter<AbstractFlexibleItem<*>> = adapter
private var currentItems = currentConversations
private var filterState = savedFilterState
private var conversationsList = conversationsListActivity

Expand All @@ -58,6 +58,9 @@ class FilterConversationFragment(

@Inject
lateinit var viewThemeUtils: ViewThemeUtils

@Inject
lateinit var appPreferences: AppPreferences
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
binding = DialogFilterConversationBinding.inflate(LayoutInflater.from(context))
dialogView = binding.root
Expand Down Expand Up @@ -119,57 +122,19 @@ class FilterConversationFragment(
}

private fun processSubmit() {
val newItems: MutableList<AbstractFlexibleItem<*>> = ArrayList()
if (!filterState.containsValue(true)) {
currentAdapter.updateDataSet(currentItems, true)
} else {
val items = currentItems
for (i in items) {
val conversation = (i as ConversationItem).model
if (filter(conversation)) {
newItems.add(i)
}
}
currentAdapter.updateDataSet(newItems, true)
conversationsList.setFilterableItems(newItems)
}
conversationsList.updateFilterState(
filterState[MENTION]!!,
filterState[UNREAD]!!
)

conversationsList.updateFilterConversationButtonColor()
}
private fun filter(conversation: Conversation): Boolean {
var result = true
for ((k, v) in filterState) {
if (v) {
when (k) {
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)
)
UNREAD -> result = result && (conversation.unreadMessages > 0)
}
}
}
// store
appPreferences.setMentionFilter(filterState[MENTION] == true)
appPreferences.setUnreadFilter(filterState[UNREAD] == true)

return result
conversationsList.filterConversation()
}

companion object {
@JvmStatic
fun newInstance(
adapter: FlexibleAdapter<AbstractFlexibleItem<*>>,
currentConversations: MutableList<AbstractFlexibleItem<*>>,
savedFilterState: MutableMap<String, Boolean>,
conversationsListActivity: ConversationsListActivity
) = FilterConversationFragment(adapter, currentConversations, savedFilterState, conversationsListActivity)
) = FilterConversationFragment(savedFilterState, conversationsListActivity)
val TAG: String = FilterConversationFragment::class.java.simpleName
const val MENTION: String = "mention"
const val UNREAD: String = "unread"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,20 @@ public interface AppPreferences {
@KeyByResource(R.string.nc_file_browser_sort_by_key)
@DefaultValue(R.string.nc_file_browser_sort_by_default)
String getSorting();

@KeyByResource(R.string.nc_filter_unread)
@DefaultValue(R.bool.value_false)
boolean isUnreadFilterEnabled();

@KeyByResource(R.string.nc_filter_unread)
void setUnreadFilter(boolean value);

@KeyByResource(R.string.nc_filter_mention)
@DefaultValue(R.bool.value_false)
boolean isMentionFilterEnabled();

@KeyByResource(R.string.nc_filter_mention)
void setMentionFilter(boolean value);

@KeyByResource(R.string.nc_file_browser_sort_by_key)
@RegisterChangeListenerMethod
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,9 @@ How to translate with transifex:
<string name="nc_user">User</string>
<string name="nc_guest">Guest</string>
<string name="nc_following_link">User following a public link</string>

<string name="nc_filter_unread" translatable="false">nc_filter_unread</string>
<string name="nc_filter_mention" translatable="false">nc_filter_mention</string>

<!-- Browser component -->
<string name="nc_file_browser_back">Back</string>
Expand Down

0 comments on commit 94d3e5d

Please sign in to comment.