Home badge

This commit is contained in:
Benoit Marty 2019-05-29 17:19:06 +02:00
parent 9da727b623
commit ae38917a33
4 changed files with 128 additions and 2 deletions

View File

@ -21,6 +21,8 @@ import android.os.Parcelable
import android.view.LayoutInflater
import androidx.core.view.forEachIndexed
import com.airbnb.mvrx.args
import com.airbnb.mvrx.fragmentViewModel
import com.airbnb.mvrx.withState
import com.google.android.material.bottomnavigation.BottomNavigationItemView
import com.google.android.material.bottomnavigation.BottomNavigationMenuView
import im.vector.riotredesign.R
@ -43,12 +45,18 @@ data class HomeDetailParams(

private const val CURRENT_DISPLAY_MODE = "CURRENT_DISPLAY_MODE"

private const val INDEX_CATCHUP = 0
private const val INDEX_PEOPLE = 1
private const val INDEX_ROOMS = 2

class HomeDetailFragment : VectorBaseFragment() {

private val params: HomeDetailParams by args()
private val unreadCounterBadgeViews = arrayListOf<UnreadCounterBadgeView>()
private lateinit var currentDisplayMode: RoomListFragment.DisplayMode

private val viewModel: HomeDetailViewModel by fragmentViewModel()

override fun getLayoutResId(): Int {
return R.layout.fragment_home_detail
}
@ -81,7 +89,7 @@ class HomeDetailFragment : VectorBaseFragment() {
groupToolbarAvatarImageView
)
groupToolbarAvatarImageView.setOnClickListener {

vectorBaseActivity.notImplemented("Group click in toolbar")
}
}

@ -103,7 +111,7 @@ class HomeDetailFragment : VectorBaseFragment() {
val menuView = bottomNavigationView.getChildAt(0) as BottomNavigationMenuView
menuView.forEachIndexed { index, view ->
val itemView = view as BottomNavigationItemView
val badgeLayout = LayoutInflater.from(requireContext()).inflate(R.layout.vector_unread_layout, menuView, false)
val badgeLayout = LayoutInflater.from(requireContext()).inflate(R.layout.vector_home_badge_unread_layout, menuView, false)
val unreadCounterBadgeView: UnreadCounterBadgeView = badgeLayout.findViewById(R.id.actionUnreadCounterBadgeView)
itemView.addView(badgeLayout)
unreadCounterBadgeViews.add(index, unreadCounterBadgeView)
@ -127,6 +135,13 @@ class HomeDetailFragment : VectorBaseFragment() {
.commit()
}


override fun invalidate() = withState(viewModel) {
unreadCounterBadgeViews[INDEX_CATCHUP].render(UnreadCounterBadgeView.State(it.notificationCountCatchup, it.notificationHighlightCatchup))
unreadCounterBadgeViews[INDEX_PEOPLE].render(UnreadCounterBadgeView.State(it.notificationCountPeople, it.notificationHighlightPeople))
unreadCounterBadgeViews[INDEX_ROOMS].render(UnreadCounterBadgeView.State(it.notificationCountRooms, it.notificationHighlightRooms))
}

companion object {

fun newInstance(args: HomeDetailParams): HomeDetailFragment {

View File

@ -0,0 +1,83 @@
/*
* Copyright 2019 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package im.vector.riotredesign.features.home

import com.airbnb.mvrx.MvRxViewModelFactory
import com.airbnb.mvrx.ViewModelContext
import im.vector.matrix.android.api.session.Session
import im.vector.matrix.rx.rx
import im.vector.riotredesign.core.platform.VectorViewModel
import org.koin.android.ext.android.get

class HomeDetailViewModel(initialState: HomeDetailViewState,
private val session: Session)
: VectorViewModel<HomeDetailViewState>(initialState) {

companion object : MvRxViewModelFactory<HomeDetailViewModel, HomeDetailViewState> {

@JvmStatic
override fun create(viewModelContext: ViewModelContext, state: HomeDetailViewState): HomeDetailViewModel? {
val currentSession = viewModelContext.activity.get<Session>()
return HomeDetailViewModel(state, currentSession)
}
}

init {
observeRoomSummaries()
}

// PRIVATE METHODS *****************************************************************************

// TODO Filter with selected group
private fun observeRoomSummaries() {
session
.rx()
.liveRoomSummaries()
.execute { state ->
state.invoke()?.let { summaries ->
val peopleNotifications = summaries
.filter { it.isDirect }
.map { it.notificationCount }
.reduce { acc, i -> acc + i }
val peopleHasHighlight = summaries
.filter { it.isDirect }
.any { it.highlightCount > 0 }

val roomsNotifications = summaries
.filter { !it.isDirect }
.map { it.notificationCount }
.reduce { acc, i -> acc + i }
val roomsHasHighlight = summaries
.filter { !it.isDirect }
.any { it.highlightCount > 0 }

copy(
notificationCountCatchup = peopleNotifications + roomsNotifications,
notificationHighlightCatchup = peopleHasHighlight || roomsHasHighlight,
notificationCountPeople = peopleNotifications,
notificationHighlightPeople = peopleHasHighlight,
notificationCountRooms = roomsNotifications,
notificationHighlightRooms = roomsHasHighlight
)
} ?: run {
// No change
copy()
}
}
}

}

View File

@ -0,0 +1,28 @@
/*
* Copyright 2019 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package im.vector.riotredesign.features.home

import com.airbnb.mvrx.MvRxState

data class HomeDetailViewState(
val notificationCountCatchup: Int = 0,
val notificationHighlightCatchup: Boolean = false,
val notificationCountPeople: Int = 0,
val notificationHighlightPeople: Boolean = false,
val notificationCountRooms: Int = 0,
val notificationHighlightRooms: Boolean = false
) : MvRxState