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 1 commit
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 @@ -7,31 +7,76 @@ import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.appcompat.widget.Toolbar
import androidx.databinding.DataBindingUtil
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.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 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 lateinit var navHeaderMainBinding: NavHeaderNavigationDrawerBinding
private lateinit var profileId: ProfileId

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)
navView = binding.fragmentDrawerNavView
binding.fragmentDrawerNavView.setNavigationItemSelectedListener(this)

fragment.setHasOptionsMenu(true)
return view
profileId = profileManagementController.getCurrentProfileId()
navHeaderMainBinding =
DataBindingUtil.inflate(inflater, R.layout.nav_header_navigation_drawer, navView, true)
navView.setNavigationItemSelectedListener(this)

subscribeToProfileLiveData()

return binding.root
}

private val profileLiveData: LiveData<Profile> by lazy {
veena14cs marked this conversation as resolved.
Show resolved Hide resolved
getProfileData()
}

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

private fun subscribeToProfileLiveData() {
profileLiveData.observe(fragment, Observer<Profile> { result ->
navHeaderMainBinding.navNameTextview.text = result.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 Down
43 changes: 43 additions & 0 deletions app/src/main/res/layout/drawer_fragment.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?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:headerLayout="@layout/nav_header_navigation_drawer"
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>
73 changes: 38 additions & 35 deletions app/src/main/res/layout/nav_header_navigation_drawer.xml
Original file line number Diff line number Diff line change
@@ -1,39 +1,42 @@
<?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" />

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

<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_name_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:paddingTop="8dp"
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:textColor="@color/white"
android:text="@string/nav_header_subtitle"
android:textSize="14sp" />
</LinearLayout>
<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>