Skip to content

Commit

Permalink
Fix oppia#577: Display profile name on navigation drawer. (oppia#578)
Browse files Browse the repository at this point in the history
* display profile name in navigation drawer

* Updated code implementation

* adding tests

* added testcases

* Update NavigationDrawerFragmentPresenter.kt

* updated nit changes.
  • Loading branch information
veena14cs authored and PrarabdhGarg committed Feb 12, 2020
1 parent 52e1eb4 commit 9bbd10b
Show file tree
Hide file tree
Showing 9 changed files with 314 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,80 @@ 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()

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

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

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 +106,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() {
val profileName = ObservableField<String>("")
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
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

class NavigationDrawerTestActivity : InjectableAppCompatActivity(), RouteToTopicListener {
@Inject
lateinit var homeActivityPresenter: HomeActivityPresenter
@Inject lateinit var homeActivityPresenter: HomeActivityPresenter

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
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
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

0 comments on commit 9bbd10b

Please sign in to comment.