Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #577: Display profile name on navigation drawer. #578

Merged
merged 7 commits into from
Feb 3, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,81 @@ import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.drawerlayout.widget.DrawerLayout
import androidx.fragment.app.Fragment
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import androidx.lifecycle.Transformations
import com.google.android.material.navigation.NavigationView
import org.oppia.app.R
import org.oppia.app.databinding.DrawerFragmentBinding
import org.oppia.app.databinding.NavHeaderNavigationDrawerBinding
import org.oppia.app.fragment.FragmentScope
import org.oppia.app.help.HelpActivity
import org.oppia.app.home.HomeActivity
import org.oppia.app.home.KEY_HOME_PROFILE_ID
import org.oppia.app.model.Profile
import org.oppia.app.model.ProfileId
import org.oppia.domain.profile.ProfileManagementController
import org.oppia.util.data.AsyncResult
import org.oppia.util.logging.Logger
import javax.inject.Inject

/** The presenter for [NavigationDrawerFragment]. */
@FragmentScope
class NavigationDrawerFragmentPresenter @Inject constructor(
private val fragment: Fragment
private val activity: AppCompatActivity,
private val fragment: Fragment,
private val profileManagementController: ProfileManagementController,
private val logger: Logger
) : NavigationView.OnNavigationItemSelectedListener {
private lateinit var navView: NavigationView
private lateinit var drawerToggle: ActionBarDrawerToggle
private lateinit var drawerLayout: DrawerLayout
private var previousMenuItemId: Int? = null
private lateinit var binding: DrawerFragmentBinding
private var internalProfileId: Int = -1
private lateinit var profileId: ProfileId
private lateinit var navigationDrawerHeaderViewModel: NavigationDrawerHeaderViewModel

fun handleCreateView(inflater: LayoutInflater, container: ViewGroup?): View? {
val view: View? = inflater.inflate(R.layout.fragment_drawer, container, false)
navView = view!!.findViewById(R.id.fragment_drawer_nav_view)
navView.setNavigationItemSelectedListener(this)
binding = DrawerFragmentBinding.inflate(inflater, container, /* attachToRoot= */ false)
binding.fragmentDrawerNavView.setNavigationItemSelectedListener(this)

fragment.setHasOptionsMenu(true)
return view

internalProfileId = activity.intent.getIntExtra(KEY_HOME_PROFILE_ID, -1)
profileId = ProfileId.newBuilder().setInternalId(internalProfileId).build()

navigationDrawerHeaderViewModel = NavigationDrawerHeaderViewModel()
veena14cs marked this conversation as resolved.
Show resolved Hide resolved

val headerBinding = NavHeaderNavigationDrawerBinding.inflate(inflater, container, /* attachToRoot= */ false)
headerBinding.viewModel = navigationDrawerHeaderViewModel
subscribeToProfileLiveData()

binding.fragmentDrawerNavView.addHeaderView(headerBinding.root)
binding.executePendingBindings()


veena14cs marked this conversation as resolved.
Show resolved Hide resolved
return binding.root
}

private fun getProfileData(): LiveData<Profile> {
return Transformations.map(profileManagementController.getProfile(profileId), ::processGetProfileResult)
}

private fun subscribeToProfileLiveData() {
getProfileData().observe(fragment, Observer<Profile> {
navigationDrawerHeaderViewModel.profileName.set(it.name)
})
}

private fun processGetProfileResult(profileResult: AsyncResult<Profile>): Profile {
if (profileResult.isFailure()) {
logger.e("NavigationDrawerFragmentPresenter", "Failed to retrieve profile", profileResult.getErrorOrNull()!!)
}
return profileResult.getOrDefault(Profile.getDefaultInstance())
}

private fun openActivityByMenuItemId(menuItemId: Int) {
Expand All @@ -58,10 +107,10 @@ class NavigationDrawerFragmentPresenter @Inject constructor(
fun setUpDrawer(drawerLayout: DrawerLayout, toolbar: Toolbar, menuItemId: Int) {
when (NavigationDrawerItem.valueFromNavId(menuItemId)) {
NavigationDrawerItem.HOME -> {
navView.menu.getItem(NavigationDrawerItem.HOME.ordinal).isChecked = true
binding.fragmentDrawerNavView.menu.getItem(NavigationDrawerItem.HOME.ordinal).isChecked = true
}
NavigationDrawerItem.HELP -> {
navView.menu.getItem(NavigationDrawerItem.HELP.ordinal).isChecked = true
binding.fragmentDrawerNavView.menu.getItem(NavigationDrawerItem.HELP.ordinal).isChecked = true
}
}
this.drawerLayout = drawerLayout
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.oppia.app.drawer

import androidx.databinding.ObservableField
import org.oppia.app.viewmodel.ObservableViewModel
import javax.inject.Inject

class NavigationDrawerHeaderViewModel @Inject constructor(): ObservableViewModel() {
veena14cs marked this conversation as resolved.
Show resolved Hide resolved
val profileName = ObservableField<String>("")
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package org.oppia.app.testing

import android.content.Context
import android.content.Intent
import android.os.Bundle
import org.oppia.app.R
import org.oppia.app.activity.InjectableAppCompatActivity
import org.oppia.app.home.HomeActivityPresenter
import org.oppia.app.home.KEY_HOME_PROFILE_ID
import org.oppia.app.home.RouteToTopicListener
import org.oppia.app.topic.TopicActivity
import javax.inject.Inject
Expand All @@ -12,6 +15,13 @@ class NavigationDrawerTestActivity : InjectableAppCompatActivity(), RouteToTopic
@Inject
lateinit var homeActivityPresenter: HomeActivityPresenter
veena14cs marked this conversation as resolved.
Show resolved Hide resolved

companion object {
fun createNavigationDrawerTestActivity(context: Context, profileId: Int?): Intent {
val intent = Intent(context, NavigationDrawerTestActivity::class.java)
intent.putExtra(KEY_HOME_PROFILE_ID, profileId)
return intent
}
}
veena14cs marked this conversation as resolved.
Show resolved Hide resolved
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
activityComponent.inject(this)
Expand Down
42 changes: 42 additions & 0 deletions app/src/main/res/layout/drawer_fragment.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<com.google.android.material.navigation.NavigationView
android:id="@+id/fragment_drawer_nav_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:itemBackground="@android:color/transparent"
app:itemIconTint="@color/drawer_item"
app:itemTextColor="@color/drawer_item"
app:menu="@menu/navigation_drawer_menu">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="horizontal"
android:padding="16dp">

<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="center_vertical"
android:layout_margin="12dp"
android:src="@drawable/ic_admin_settings_icon_grey_24dp" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="32dp"
android:layout_marginEnd="32dp"
android:fontFamily="sans-serif-medium"
android:text="@string/admin_settings"
android:textColor="@color/black"
android:textSize="14sp" />
</LinearLayout>
</com.google.android.material.navigation.NavigationView>
</layout>
39 changes: 0 additions & 39 deletions app/src/main/res/layout/fragment_drawer.xml

This file was deleted.

4 changes: 2 additions & 2 deletions app/src/main/res/layout/help_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="@layout/fragment_drawer"
tools:layout="@layout/fragment_drawer" />
app:layout="@layout/drawer_fragment"
tools:layout="@layout/drawer_fragment" />
</androidx.drawerlayout.widget.DrawerLayout>
4 changes: 2 additions & 2 deletions app/src/main/res/layout/home_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="@layout/fragment_drawer"
tools:layout="@layout/fragment_drawer" />
app:layout="@layout/drawer_fragment"
tools:layout="@layout/drawer_fragment" />
</androidx.drawerlayout.widget.DrawerLayout>
78 changes: 44 additions & 34 deletions app/src/main/res/layout/nav_header_navigation_drawer.xml
Original file line number Diff line number Diff line change
@@ -1,39 +1,49 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="176dp"
android:background="@drawable/side_nav_bar"
android:gravity="bottom"
android:orientation="vertical"
android:paddingStart="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingEnd="@dimen/activity_horizontal_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:theme="@style/ThemeOverlay.AppCompat.Dark">
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:contentDescription="@string/nav_header_desc"
android:paddingTop="8dp"
app:srcCompat="@mipmap/ic_launcher_round" />
<data>
<variable
name="viewModel"
type="org.oppia.app.drawer.NavigationDrawerHeaderViewModel" />
</data>

<TextView
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:paddingTop="8dp"
android:textColor="@color/white"
android:text="@string/nav_header_title"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textSize="14sp" />
android:layout_height="176dp"
android:background="@drawable/side_nav_bar"
android:gravity="bottom"
android:orientation="vertical"
android:paddingStart="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingEnd="@dimen/activity_horizontal_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:theme="@style/ThemeOverlay.AppCompat.Dark">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif"
android:textColor="@color/white"
android:text="@string/nav_header_subtitle"
android:textSize="14sp" />
</LinearLayout>
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:contentDescription="@string/nav_header_desc"
android:paddingTop="8dp"
app:srcCompat="@mipmap/ic_launcher_round" />

<TextView
android:id="@+id/nav_header_profile_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:paddingTop="8dp"
android:text="@{viewModel.profileName}"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textColor="@color/white"
android:textSize="14sp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif"
android:text="@string/nav_header_subtitle"
android:textColor="@color/white"
android:textSize="14sp" />
</LinearLayout>
</layout>
Loading