1
1
package com.whyranoid.presentation.runningstart
2
2
3
3
import android.Manifest
4
+ import android.app.Activity
4
5
import android.content.Intent
5
6
import android.content.pm.PackageManager
6
7
import android.net.Uri
@@ -9,12 +10,16 @@ import android.view.View
9
10
import androidx.activity.result.contract.ActivityResultContracts
10
11
import androidx.core.content.ContextCompat
11
12
import androidx.fragment.app.viewModels
13
+ import androidx.navigation.fragment.findNavController
12
14
import com.google.android.material.dialog.MaterialAlertDialogBuilder
13
15
import com.google.android.material.snackbar.Snackbar
14
16
import com.whyranoid.presentation.R
15
17
import com.whyranoid.presentation.base.BaseFragment
16
18
import com.whyranoid.presentation.databinding.FragmentRunningStartBinding
17
19
import com.whyranoid.presentation.running.RunningActivity
20
+ import com.whyranoid.presentation.running.RunningFinishData
21
+ import com.whyranoid.presentation.running.RunningViewModel.Companion.RUNNING_FINISH_DATA_KEY
22
+ import com.whyranoid.presentation.util.getSerializableData
18
23
import com.whyranoid.presentation.util.gpsstate.GPSState
19
24
import com.whyranoid.presentation.util.repeatWhenUiStarted
20
25
import dagger.hilt.android.AndroidEntryPoint
@@ -23,16 +28,38 @@ import dagger.hilt.android.AndroidEntryPoint
23
28
internal class RunningStartFragment :
24
29
BaseFragment <FragmentRunningStartBinding >(R .layout.fragment_running_start) {
25
30
31
+ // 위치 권한 요청
26
32
private val locationPermissionRequest = registerForActivityResult(
27
33
ActivityResultContracts .RequestPermission ()
28
34
) { isGranted: Boolean ->
29
35
if (isGranted) {
30
- startActivity (Intent (requireContext(), RunningActivity ::class .java))
36
+ runningActivityLauncher.launch (Intent (requireContext(), RunningActivity ::class .java))
31
37
} else {
32
38
showPermissionRequestDialog()
33
39
}
34
40
}
35
41
42
+ // Running Activity 로 이동 런쳐(운동 결과를 받고 종료 화면으로 이동)
43
+ private val runningActivityLauncher = registerForActivityResult(
44
+ ActivityResultContracts .StartActivityForResult ()
45
+ ) { activityResult ->
46
+ if (activityResult.resultCode == Activity .RESULT_OK ) {
47
+
48
+ // 결과 받아오기
49
+ val runningFinishData =
50
+ activityResult.data?.getSerializableData<RunningFinishData >(
51
+ RUNNING_FINISH_DATA_KEY
52
+ )
53
+
54
+ // 결과 넘겨주기
55
+ val direction =
56
+ RunningStartFragmentDirections .actionRunningStartFragmentToRunningFinish(
57
+ runningFinishData
58
+ )
59
+ findNavController().navigate(direction)
60
+ }
61
+ }
62
+
36
63
private val viewModel by viewModels<RunningStartViewModel >()
37
64
38
65
override fun onViewCreated (view : View , savedInstanceState : Bundle ? ) {
@@ -70,7 +97,12 @@ internal class RunningStartFragment :
70
97
requireContext(),
71
98
Manifest .permission.ACCESS_FINE_LOCATION
72
99
) == PackageManager .PERMISSION_GRANTED -> {
73
- startActivity(Intent (requireContext(), RunningActivity ::class .java))
100
+ runningActivityLauncher.launch(
101
+ Intent (
102
+ requireContext(),
103
+ RunningActivity ::class .java
104
+ )
105
+ )
74
106
}
75
107
shouldShowRequestPermissionRationale(Manifest .permission.ACCESS_FINE_LOCATION ) -> {
76
108
// 다이얼로그 띄우기, 거부된 상태
0 commit comments