Skip to content

Commit

Permalink
Merge pull request #43 from junjange/feature/bottom_navi
Browse files Browse the repository at this point in the history
  • Loading branch information
junjange authored Jul 14, 2024
2 parents 97ab13a + 0527834 commit 9180143
Show file tree
Hide file tree
Showing 13 changed files with 218 additions and 32 deletions.
1 change: 1 addition & 0 deletions android/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
alias(libs.plugins.androidApplication)
alias(libs.plugins.jetbrainsKotlinAndroid)
id("kotlin-kapt")
alias(libs.plugins.navigation.safeargs)
}

android {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import androidx.annotation.LayoutRes
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import androidx.navigation.NavDirections
import androidx.navigation.fragment.findNavController
import com.google.android.material.snackbar.Snackbar

abstract class BaseFragment<T : ViewDataBinding>(
Expand Down Expand Up @@ -57,6 +59,12 @@ abstract class BaseFragment<T : ViewDataBinding>(
snackbar?.show()
}

protected fun Fragment.navigate(directions: NavDirections) {
val controller = findNavController()
controller.currentDestination?.getAction(directions.actionId) ?: return
controller.navigate(directions)
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,67 @@
package com.woowacourse.friendogly.presentation.ui

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import android.view.View
import androidx.navigation.NavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupWithNavController
import com.woowacourse.friendogly.NavigationGraphDirections
import com.woowacourse.friendogly.R
import com.woowacourse.friendogly.databinding.ActivityMainBinding
import com.woowacourse.friendogly.presentation.base.BaseActivity

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
class MainActivity : BaseActivity<ActivityMainBinding>(R.layout.activity_main) {
private lateinit var navHostFragment: NavHostFragment
private lateinit var navController: NavController
private var waitTime = 0L

override fun initCreateView() {
initNavController()
}

private fun initNavController() {
navHostFragment =
supportFragmentManager.findFragmentById(R.id.nav_host_fragment_container) as NavHostFragment
navController = navHostFragment.navController

navController.addOnDestinationChangedListener { _, destination, _ ->
when (destination.id) {
R.id.homeFragment, R.id.woofFragment, R.id.chatFragment, R.id.myPageFragment -> showBottomNav()
else -> hideBottomNav()
}
}
binding.bottomNavi.setupWithNavController(navController)
binding.bottomNavi.setOnItemReselectedListener {}
}

private fun showBottomNav() {
binding.bottomNavi.visibility = View.VISIBLE
}

private fun hideBottomNav() {
binding.bottomNavi.visibility = View.GONE
}

override fun onBackPressed() {
try {
if (onBackPressedDispatcher.hasEnabledCallbacks()) {
super.onBackPressed()
} else {
when (navController.currentDestination?.id) {
R.id.homeFragment -> {
if (System.currentTimeMillis() - waitTime >= 1500) {
waitTime = System.currentTimeMillis()
showToastMessage(getString(R.string.on_back_pressed_Message))
} else {
finishAffinity()
}
}

null -> super.onBackPressed()
else -> navController.navigate(NavigationGraphDirections.actionHomeFragment())
}
}
} catch (e: Exception) {
showToastMessage(e.message.toString())
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.woowacourse.friendogly.presentation.ui.mypage

import com.woowacourse.friendogly.R
import com.woowacourse.friendogly.databinding.FragmentMyPageBinding
import com.woowacourse.friendogly.presentation.base.BaseFragment

class MyPageFragment : BaseFragment<FragmentMyPageBinding>(R.layout.fragment_my_page) {
override fun initViewCreated() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/gray05" android:state_checked="false" />
<item android:color="@color/black" android:state_checked="true" />
</selector>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/gray05" android:state_checked="false" />
<item android:color="@color/black" android:state_checked="true" />
</selector>
67 changes: 50 additions & 17 deletions android/app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,19 +1,52 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".presentation.ui.MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
xmlns:tools="http://schemas.android.com/tools">

<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".presentation.ui.MainActivity">

<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment_container"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@+id/line_bottom_navigation"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/navigation_main" />

<View
android:id="@+id/line_bottom_navigation"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/black"
app:layout_constraintBottom_toTopOf="@id/bottom_navi"
app:layout_constraintTop_toBottomOf="@id/nav_host_fragment_container" />

<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipChildren="false"
app:elevation="0dp"
app:itemActiveIndicatorStyle="@android:color/transparent"
app:itemIconTint="@drawable/sel_main_icon_color_bnv_menu"
app:itemTextColor="@drawable/sel_main_text_color_bnv_menu"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/menu_main_bottom" />


</androidx.constraintlayout.widget.ConstraintLayout>

</layout>

13 changes: 13 additions & 0 deletions android/app/src/main/res/layout/fragment_my_page.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?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"
xmlns:tools="http://schemas.android.com/tools">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">


</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

28 changes: 28 additions & 0 deletions android/app/src/main/res/menu/menu_main_bottom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item
android:id="@+id/homeFragment"
android:enabled="true"
android:icon="@drawable/ic_home"
android:title="@string/bottom_home" />

<item
android:id="@+id/woofFragment"
android:enabled="true"
android:icon="@drawable/ic_dog"
android:title="@string/bottom_woof" />

<item
android:id="@+id/chatFragment"
android:enabled="true"
android:icon="@drawable/ic_chat"
android:title="@string/bottom_chat" />

<item
android:id="@+id/myPageFragment"
android:enabled="true"
android:icon="@drawable/ic_my"
android:title="@string/bottom_my_page" />

</menu>
26 changes: 26 additions & 0 deletions android/app/src/main/res/navigation/navigation_main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/navigation_graph"
app:startDestination="@id/myPageFragment">

<action
android:id="@+id/action_home_fragment"
app:destination="@id/myPageFragment"
app:enterAnim="@anim/nav_default_enter_anim"
app:exitAnim="@anim/nav_default_exit_anim"
app:popEnterAnim="@anim/nav_default_enter_anim"
app:popExitAnim="@anim/nav_default_pop_exit_anim"
app:popUpTo="@id/navigation_graph"
app:popUpToInclusive="true" />

<fragment
android:id="@+id/myPageFragment"
android:name="com.woowacourse.friendogly.presentation.ui.mypage.MyPageFragment"
android:label="MyPageFragment"
tools:layout="@layout/fragment_my_page">

</fragment>

</navigation>
12 changes: 11 additions & 1 deletion android/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
<resources>
<string name="app_name">friendogly</string>
</resources>

<!--홈화면-->
<string name="on_back_pressed_Message">뒤로가기 버튼을\n한번 더 누르면 종료됩니다.</string>

<!--내비게이션 메뉴-->
<string name="bottom_home">홈</string>
<string name="bottom_woof">멍멍짖기</string>
<string name="bottom_chat">채팅</string>
<string name="bottom_my_page">나의 댕댕이</string>

</resources>
1 change: 1 addition & 0 deletions android/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ plugins {
alias(libs.plugins.jetbrainsKotlinAndroid) apply false
alias(libs.plugins.android.library) apply false
alias(libs.plugins.ktlint) apply false
alias(libs.plugins.navigation.safeargs) apply false
}

allprojects {
Expand Down
1 change: 1 addition & 0 deletions android/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,4 @@ androidApplication = { id = "com.android.application", version.ref = "agp" }
jetbrainsKotlinAndroid = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
android-library = { id = "com.android.library", version.ref = "agp" }
ktlint = { id = "org.jlleitschuh.gradle.ktlint", version.ref = "ktlint" }
navigation-safeargs = { id = "androidx.navigation.safeargs.kotlin", version.ref = "navigation" }

0 comments on commit 9180143

Please sign in to comment.