Menu.kt 4.24 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
package chat.rocket.android.chatroom.ui

import android.content.Context
import android.view.Menu
import android.view.MenuItem
import android.widget.EditText
import androidx.appcompat.widget.SearchView
import androidx.core.content.res.ResourcesCompat
import chat.rocket.android.R
import chat.rocket.android.util.extension.onQueryTextListener
11
import chat.rocket.common.model.RoomType
12 13 14 15 16

internal fun ChatRoomFragment.setupMenu(menu: Menu) {
    setupSearchMessageMenuItem(menu, requireContext())
    setupFavoriteMenuItem(menu)

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
    menu.add(
        Menu.NONE,
        MENU_ACTION_PINNED_MESSAGES,
        Menu.NONE,
        R.string.title_pinned_messages
    )

    menu.add(
        Menu.NONE,
        MENU_ACTION_FAVORITE_MESSAGES,
        Menu.NONE,
        R.string.title_favorite_messages
    )

    if (chatRoomType != RoomType.DIRECT_MESSAGE && !disableMenu) {
32 33 34 35 36 37
        menu.add(
            Menu.NONE,
            MENU_ACTION_MEMBER,
            Menu.NONE,
            R.string.title_members_list
        )
38

39 40 41 42 43 44 45
        menu.add(
            Menu.NONE,
            MENU_ACTION_MENTIONS,
            Menu.NONE,
            R.string.msg_mentions
        )
    }
46

47 48 49 50 51 52 53 54
    if (!disableMenu) {
        menu.add(
            Menu.NONE,
            MENU_ACTION_FILES,
            Menu.NONE,
            R.string.title_files
        )
    }
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
}

internal fun ChatRoomFragment.setOnMenuItemClickListener(item: MenuItem) {
    when (item.itemId) {
        MENU_ACTION_FAVORITE_UNFAVORITE_CHAT -> presenter.toggleFavoriteChatRoom(
            chatRoomId,
            isFavorite
        )
        MENU_ACTION_MEMBER -> presenter.toMembersList(chatRoomId)
        MENU_ACTION_MENTIONS -> presenter.toMentions(chatRoomId)
        MENU_ACTION_PINNED_MESSAGES -> presenter.toPinnedMessageList(chatRoomId)
        MENU_ACTION_FAVORITE_MESSAGES -> presenter.toFavoriteMessageList(chatRoomId)
        MENU_ACTION_FILES -> presenter.toFileList(chatRoomId)
    }
}

private fun ChatRoomFragment.setupSearchMessageMenuItem(menu: Menu, context: Context) {
    val searchItem = menu.add(
        Menu.NONE,
        Menu.NONE,
        Menu.NONE,
        R.string.title_search_message
    ).setActionView(SearchView(context))
        .setIcon(R.drawable.ic_search_white_24dp)
        .setShowAsActionFlags(
            MenuItem.SHOW_AS_ACTION_IF_ROOM or MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
        )

    (searchItem?.actionView as? SearchView)?.let {
        // TODO: Check why we need to stylize the search text programmatically instead of by defining it in the styles.xml (ChatRoom.SearchView)
        stylizeSearchView(it, context)
        setupSearchViewTextListener(it)
        if (it.isIconified) {
            isSearchTermQueried = false
        }
    }
}

private fun stylizeSearchView(searchView: SearchView, context: Context) {
    val searchText = searchView.findViewById<EditText>(androidx.appcompat.R.id.search_src_text)
    searchText.setTextColor(ResourcesCompat.getColor(context.resources, R.color.color_white, null))
    searchText.setHintTextColor(
        ResourcesCompat.getColor(context.resources, R.color.color_white, null)
    )
}

private fun ChatRoomFragment.setupSearchViewTextListener(searchView: SearchView) {
    searchView.onQueryTextListener {
        // TODO: We use isSearchTermQueried to avoid querying when the search view is expanded but the user doesn't start typing. Check for a native solution.
        if (it.isEmpty() && isSearchTermQueried) {
            presenter.loadMessages(chatRoomId, chatRoomType, clearDataSet = true)
106
        } else if (it.isNotEmpty()) {
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
            presenter.searchMessages(chatRoomId, it)
            isSearchTermQueried = true
        }
    }
}

private fun ChatRoomFragment.setupFavoriteMenuItem(menu: Menu) {
    if (isFavorite) {
        menu.add(
            Menu.NONE,
            MENU_ACTION_FAVORITE_UNFAVORITE_CHAT,
            Menu.NONE,
            R.string.title_unfavorite_chat
        ).setIcon(R.drawable.ic_star_yellow_24dp)
            .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM)
    } else {
        menu.add(
            Menu.NONE,
            MENU_ACTION_FAVORITE_UNFAVORITE_CHAT,
            Menu.NONE,
            R.string.title_favorite_chat
        ).setIcon(R.drawable.ic_star_border_white_24dp)
            .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM)
    }
}