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

스페이스 추가 화면 Jetpack Compose로 마이그레이션 하기 #324

Merged
merged 16 commits into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
@@ -1,70 +1,17 @@
package boostcamp.and07.mindsync.ui.space.generate

import android.content.pm.PackageManager
import android.net.Uri
import androidx.activity.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import boostcamp.and07.mindsync.R
import boostcamp.and07.mindsync.databinding.ActivityAddSpaceBinding
import boostcamp.and07.mindsync.ui.base.BaseActivity
import boostcamp.and07.mindsync.ui.base.BaseActivityViewModel
import boostcamp.and07.mindsync.ui.space.SpaceUiEvent
import boostcamp.and07.mindsync.ui.util.ImagePickerHandler
import boostcamp.and07.mindsync.ui.util.setClickEvent
import androidx.compose.runtime.Composable
import boostcamp.and07.mindsync.ui.base.BaseComposeActivity
import boostcamp.and07.mindsync.ui.theme.MindSyncTheme
import boostcamp.and07.mindsync.ui.util.toAbsolutePath
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import java.io.File

@AndroidEntryPoint
class AddSpaceActivity : BaseActivity<ActivityAddSpaceBinding>(R.layout.activity_add_space) {
class AddSpaceActivity : BaseComposeActivity() {
private val addSpaceViewModel: AddSpaceViewModel by viewModels()
private val imagePickerHandler =
ImagePickerHandler(this) { uri ->
createImage(uri)
}

override fun init() {
setBinding()
setBackBtn()
collectSpaceEvent()
setClickEventThrottle()
}

override fun getViewModel(): BaseActivityViewModel {
return addSpaceViewModel
}

private fun setBinding() {
binding.vm = addSpaceViewModel
}

private fun setClickEventThrottle() {
binding.imgbtnUpdateSpaceThumbnail.setClickEvent(lifecycleScope) {
imagePickerHandler.checkPermissionsAndLaunchImagePicker()
}
}

private fun collectSpaceEvent() {
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
addSpaceViewModel.event.collectLatest { spaceEvent ->
when (spaceEvent) {
is SpaceUiEvent.SuccessAdd -> {
finish()
}
is SpaceUiEvent.ShowMessage -> { // 에러일 경우가 잇나?
showMessage(spaceEvent.message)
}
else -> {}
}
}
}
}
}

private fun createImage(uri: Uri?) {
uri?.let { uri ->
Expand All @@ -74,22 +21,16 @@ class AddSpaceActivity : BaseActivity<ActivityAddSpaceBinding>(R.layout.activity
}
}

private fun setBackBtn() {
binding.imgbtnAddSpaceBack.setOnClickListener {
finish()
}
}

override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray,
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == ImagePickerHandler.REQUEST_CODE_PERMISSIONS &&
grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED
) {
imagePickerHandler.launchImagePicker()
@Composable
override fun Content() {
MindSyncTheme {
AddSpaceScreen(
onBackClicked = { this.finish() },
addSpaceViewModel = addSpaceViewModel,
createSpace = { addSpaceViewModel.addSpace(it) },
updateSpaceName = { addSpaceViewModel.onSpaceNameChanged(it, 0, 0, 0) },
createImage = { createImage(it) },
)
}
}
}
Loading
Loading