Skip to content

Commit

Permalink
#91 AI카메라 프래그먼트 로직 오류 수정, 캡처된 이미지 보여주는 dialog수정중
Browse files Browse the repository at this point in the history
  • Loading branch information
pknujsp committed Jun 1, 2023
1 parent 9d130af commit d6b1a43
Show file tree
Hide file tree
Showing 11 changed files with 254 additions and 187 deletions.
27 changes: 12 additions & 15 deletions app/src/main/java/com/android/mediproject/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ package com.android.mediproject
import android.animation.ObjectAnimator
import android.os.Build
import android.view.View
import android.view.ViewGroup
import android.view.ViewTreeObserver
import android.view.animation.AnticipateInterpolator
import androidx.activity.viewModels
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.animation.doOnEnd
import androidx.core.net.toUri
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
Expand All @@ -21,8 +19,7 @@ import dagger.hilt.android.AndroidEntryPoint
import repeatOnStarted

@AndroidEntryPoint
class MainActivity :
BaseActivity<ActivityMainBinding, MainViewModel>(ActivityMainBinding::inflate) {
class MainActivity : BaseActivity<ActivityMainBinding, MainViewModel>(ActivityMainBinding::inflate) {

private val windowViewModel: WindowViewModel by viewModels()

Expand All @@ -49,8 +46,7 @@ class MainActivity :
}

binding.apply {
val navHostFragment =
supportFragmentManager.findFragmentById(R.id.fragmentContainerView) as NavHostFragment
val navHostFragment = supportFragmentManager.findFragmentById(R.id.fragmentContainerView) as NavHostFragment
navController = navHostFragment.navController

bottomNav.apply {
Expand All @@ -70,11 +66,13 @@ class MainActivity :
override fun onPreDraw(): Boolean {
if (bottomAppBar.height > 0) {
root.viewTreeObserver.removeOnPreDrawListener(this)
/**
val containerHeight = root.height - bottomAppBar.height
windowViewModel.setBottomNavHeight(containerHeight)
fragmentContainerView.layoutParams = CoordinatorLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, containerHeight
ViewGroup.LayoutParams.MATCH_PARENT, containerHeight
)
*/
}
return true
}
Expand Down Expand Up @@ -110,15 +108,14 @@ class MainActivity :
*
* argument 를 통해 bottomNav 를 숨길지 말지 결정한다.
*/
private fun setDestinationListener() =
navController.addOnDestinationChangedListener { _, destination, arg ->
log(arg.toString())
if (destination.id in hideBottomNavDestinationIds) {
bottomVisible(INVISIBLE)
} else {
bottomVisible(VISIBLE)
}
private fun setDestinationListener() = navController.addOnDestinationChangedListener { _, destination, arg ->
log(arg.toString())
if (destination.id in hideBottomNavDestinationIds) {
bottomVisible(INVISIBLE)
} else {
bottomVisible(VISIBLE)
}
}

private fun bottomVisible(isVisible: Int) {
log(isVisible.toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.core.content.ContextCompat
import androidx.fragment.app.activityViewModels
import androidx.navigation.fragment.findNavController
import com.android.mediproject.core.common.dialog.LoadingDialog
import com.android.mediproject.core.common.viewmodel.UiState
import com.android.mediproject.core.ui.base.BaseFragment
import com.android.mediproject.feature.camera.databinding.FragmentMedicinesDetectorBinding
import com.google.android.material.dialog.MaterialAlertDialogBuilder
Expand All @@ -26,13 +27,12 @@ class MedicinesDetectorFragment :

override val fragmentViewModel: MedicinesDetectorViewModel by activityViewModels()


private val requestPermissionLauncher = registerForActivityResult(
ActivityResultContracts.RequestPermission()
) { isGranted: Boolean ->
private val requestPermissionLauncher = registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted: Boolean ->
if (isGranted) {
// 권한 부여 받았으므로 카메라 초기화
initializeCamera()
} else {
// 권한 부여 거부된 경우 뒤로가기
findNavController().popBackStack()
}
}
Expand All @@ -41,63 +41,58 @@ class MedicinesDetectorFragment :
super.onViewCreated(view, savedInstanceState)
binding.viewModel = fragmentViewModel

when {
ContextCompat.checkSelfPermission(
requireContext(), Manifest.permission.CAMERA
) == PackageManager.PERMISSION_GRANTED -> {
initializeCamera()
}
viewLifecycleOwner.repeatOnStarted {
launch {
// 검출된 객체 상태
fragmentViewModel.detectedObjects.collectLatest { state ->
when (state) {
is UiState.Success -> {
LoadingDialog.dismiss()
findNavController().navigate(MedicinesDetectorFragmentDirections.actionMedicinesDetectorFragmentToConfirmDialogFragment())
}

shouldShowRequestPermissionRationale(Manifest.permission.CAMERA) -> {
MaterialAlertDialogBuilder(requireContext()).setTitle(getString(R.string.cameraPermission))
.setMessage(getString(R.string.cameraPermissionMessage)).setPositiveButton(getString(R.string.ok)) { dialog, _ ->
dialog.dismiss()
requestPermissionLauncher.launch(
Manifest.permission.CAMERA
)
}.setNegativeButton(getString(R.string.close)) { _, _ -> }.setCancelable(false).show()
}
is UiState.Error -> {
LoadingDialog.dismiss()
toast(getString(R.string.noMedicinesDetected))
fragmentViewModel.openCamera()
}

else -> {
requestPermissionLauncher.launch(
Manifest.permission.CAMERA
)
}
}
is UiState.Loading -> {
LoadingDialog.showLoadingDialog(requireActivity(), getString(R.string.getDetectedObjects))
}

is UiState.Initial -> {

viewLifecycleOwner.repeatOnStarted {
/**
* Collecting the detected objects from the camera preview
*
* If the detected objects are not empty, then navigate to the [ConfirmDialogFragment]
*
* If the detected objects are empty, then show a toast and open the camera again
*
*/
launch {
fragmentViewModel.detectedObjects.collectLatest { objs ->
if (objs.isNotEmpty()) {
findNavController().navigate(
MedicinesDetectorFragmentDirections.actionMedicinesDetectorFragmentToConfirmDialogFragment()
)
} else {
toast(getString(R.string.noMedicinesDetected))
fragmentViewModel.openCamera()
}
}
}
}

launch {
fragmentViewModel.loadedModel.collectLatest { isLoaded ->
if (isLoaded) {
fragmentViewModel.openCamera()
activity?.apply {
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
// AI모델 로드 상태
fragmentViewModel.aiModelState.collectLatest { state ->
when (state) {
is AiModelState.Loaded -> {
LoadingDialog.dismiss()
activity?.apply {
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
}
fragmentViewModel.openCamera()
}

is AiModelState.Loading -> {
LoadingDialog.showLoadingDialog(requireActivity(), getString(R.string.loadingAiModels))
}

is AiModelState.LoadFailed -> {
LoadingDialog.dismiss()
}

is AiModelState.NotLoaded -> {

}
}

LoadingDialog.dismiss()
}
}
}
Expand All @@ -107,9 +102,6 @@ class MedicinesDetectorFragment :

private fun initializeCamera() {
binding.apply {

LoadingDialog.showLoadingDialog(requireActivity(), getString(R.string.loadingAiModels))

val surfaceHolder = object : SurfaceHolder.Callback2 {
override fun surfaceCreated(holder: SurfaceHolder) {
}
Expand All @@ -128,11 +120,36 @@ class MedicinesDetectorFragment :
surfaceView.holder.setFormat(PixelFormat.RGBA_8888)
surfaceView.holder.addCallback(surfaceHolder)

// Load the model from the assets folder
// AI모델 로드
fragmentViewModel.loadModel(requireContext().assets)
}
}

override fun onStart() {
super.onStart()
when {
// 권한 확인
ContextCompat.checkSelfPermission(requireContext(), Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED -> {
// 권한 부여 받은 상태이므로 카메라 초기화
initializeCamera()
}

// 권한 요청 다이얼로그 표시
shouldShowRequestPermissionRationale(Manifest.permission.CAMERA) -> {
MaterialAlertDialogBuilder(requireContext()).setTitle(getString(R.string.cameraPermission))
.setMessage(getString(R.string.cameraPermissionMessage)).setPositiveButton(getString(R.string.ok)) { dialog, _ ->
dialog.dismiss()
requestPermissionLauncher.launch(Manifest.permission.CAMERA)
}.setNegativeButton(getString(R.string.close)) { _, _ -> }.setCancelable(false).show()
}

// 권한 요청
else -> {
requestPermissionLauncher.launch(Manifest.permission.CAMERA)
}
}
}


override fun onPause() {
super.onPause()
Expand Down
Loading

0 comments on commit d6b1a43

Please sign in to comment.