Skip to content

Commit

Permalink
kuring-168 메인 화면을 compose로 migrate (#235)
Browse files Browse the repository at this point in the history
* [추가] 메인 화면의 navigation route 정의

* [추가] 메인 화면의 navigation bar 정의

* [추가] compose type-safe navigation 라이브러리 추가

* [수정] 메인 화면 route에 @serializable 어노테이션 추가

* [수정] KuringSwitch에서 라이브러리 변경에 대응

* [추가] MainScreen composable 구현

* [수정] MainActivity에서 XML 대신 Compose MainScreen을 호출

* [제거] 사용하지 않는 view 코드 제거

* Fix transition issue

* [수정] KuringSwitch의 미구현 인터페이스 구현

---------

Co-authored-by: HyunWoo Lee <[email protected]>
  • Loading branch information
mwy3055 and l2hyunwoo committed Jun 19, 2024
1 parent be81489 commit c610f27
Show file tree
Hide file tree
Showing 11 changed files with 472 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class KotlinPlugin : Plugin<Project> {
with(target) {
with(plugins) {
apply("kotlin-android")
apply("kotlinx-serialization")
}

tasks.withType<KotlinCompile> {
Expand All @@ -36,6 +37,8 @@ class KotlinPlugin : Plugin<Project> {
implementation(libs.library("kotlinx-coroutines-android"))
implementation(libs.library("kotlinx-coroutines-reactive"))
implementation(libs.library("kotlinx-coroutines-rxjava3"))

implementation(libs.library("kotlinx-serialization"))
}
}
}
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ buildscript {
classpath(libs.google.services)
classpath(libs.firebase.crashlytics.gradle)
classpath(libs.oss.licenses)
classpath(libs.kotlinx.serialization.plugin)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.ku_stacks.ku_ring.designsystem.components

import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.core.AnimationVector
import androidx.compose.animation.core.DecayAnimationSpec
import androidx.compose.animation.core.TweenSpec
import androidx.compose.animation.core.TwoWayConverter
import androidx.compose.animation.core.VectorizedDecayAnimationSpec
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
Expand Down Expand Up @@ -102,13 +106,18 @@ fun KuringSwitch(
val anchoredDraggableState = remember(maxBound, switchVelocityThresholdPx) {
AnchoredDraggableState(
initialValue = checked,
animationSpec = AnimationSpec,
anchors = DraggableAnchors {
false at minBound
true at maxBound
},
positionalThreshold = { distance -> distance * SwitchPositionalThreshold },
velocityThreshold = { switchVelocityThresholdPx }
positionalThreshold = { distance: Float -> distance * SwitchPositionalThreshold },
velocityThreshold = { switchVelocityThresholdPx },
snapAnimationSpec = AnimationSpec,
decayAnimationSpec = object : DecayAnimationSpec<Float> {
override fun <V : AnimationVector> vectorize(typeConverter: TwoWayConverter<Float, V>): VectorizedDecayAnimationSpec<V> {
return vectorize(typeConverter)
}
},
)
}
val currentOnCheckedChange by rememberUpdatedState(onCheckedChange)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@ import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.MenuItem
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.viewpager2.widget.ViewPager2
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.navigation.compose.rememberNavController
import com.ku_stacks.ku_ring.designsystem.kuringtheme.KuringTheme
import com.ku_stacks.ku_ring.domain.WebViewNotice
import com.ku_stacks.ku_ring.main.databinding.ActivityMainBinding
import com.ku_stacks.ku_ring.ui_util.KuringNavigator
import com.ku_stacks.ku_ring.ui_util.showToast
import dagger.hilt.android.AndroidEntryPoint
Expand All @@ -17,23 +24,9 @@ import javax.inject.Inject
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {

private lateinit var binding: ActivityMainBinding

@Inject
lateinit var navigator: KuringNavigator

private val pageChangeCallback = object : ViewPager2.OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
binding.mainBottomNavigation.selectedItemId = when (position) {
0 -> R.id.notice_screen
1 -> R.id.archive_screen
2 -> R.id.campus_map_screen
3 -> R.id.setting_screen
else -> throw IllegalStateException("no such main viewpager position")
}
}
}

private var backPressedTime = 0L

override fun onNewIntent(intent: Intent) {
Expand All @@ -51,40 +44,23 @@ class MainActivity : AppCompatActivity() {
navToNoticeActivity(webViewNotice)
}

setupBinding()
setupView()
}

private fun setupBinding() {
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
}

private fun setupView() {
val pagerAdapter = MainPagerAdapter(supportFragmentManager, lifecycle)

binding.mainViewPager.apply {
adapter = pagerAdapter
registerOnPageChangeCallback(pageChangeCallback)
isUserInputEnabled = false
offscreenPageLimit = pagerAdapter.itemCount
}

binding.mainBottomNavigation.setOnItemSelectedListener {
navigationSelected(it)
}
}

private fun navigationSelected(item: MenuItem): Boolean {
when (item.setChecked(true).itemId) {
R.id.notice_screen -> binding.mainViewPager.setCurrentItem(0, false)
R.id.archive_screen -> binding.mainViewPager.setCurrentItem(1, false)
R.id.campus_map_screen -> binding.mainViewPager.setCurrentItem(2, false)
R.id.setting_screen -> binding.mainViewPager.setCurrentItem(3, false)
else -> return false
setContent {
KuringTheme {
var currentRoute: MainScreenRoute by remember { mutableStateOf(MainScreenRoute.Notice) }
val navController = rememberNavController()
MainScreen(
navController = navController,
currentRoute = currentRoute,
onNavigateToRoute = {
if (currentRoute != it) {
currentRoute = it
navController.navigate(it)
}
},
modifier = Modifier.fillMaxSize().background(KuringTheme.colors.background),
)
}
}

return true
}

private fun navToNoticeActivity(webViewNotice: WebViewNotice) {
Expand All @@ -100,11 +76,6 @@ class MainActivity : AppCompatActivity() {
}
}

override fun onDestroy() {
super.onDestroy()
binding.mainViewPager.unregisterOnPageChangeCallback(pageChangeCallback)
}

companion object {
fun createIntent(context: Context) = Intent(context, MainActivity::class.java)

Expand Down

This file was deleted.

Loading

0 comments on commit c610f27

Please sign in to comment.