• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.healthconnect.controller.selectabledeletion
17 
18 import android.os.Bundle
19 import android.view.LayoutInflater
20 import android.view.View
21 import android.view.ViewGroup
22 import androidx.fragment.app.Fragment
23 import androidx.fragment.app.activityViewModels
24 import com.android.healthconnect.controller.R
25 import com.android.healthconnect.controller.selectabledeletion.DeletionConstants.CONFIRMATION_KEY
26 import com.android.healthconnect.controller.selectabledeletion.DeletionConstants.START_DELETION_KEY
27 import com.android.healthconnect.controller.selectabledeletion.DeletionConstants.TRY_AGAIN_EVENT
28 import dagger.hilt.android.AndroidEntryPoint
29 
30 @AndroidEntryPoint(Fragment::class)
31 class DeletionFragment : Hilt_DeletionFragment() {
32     private val viewModel: DeletionViewModel by activityViewModels()
33 
34     override fun onCreate(savedInstanceState: Bundle?) {
35         super.onCreate(savedInstanceState)
36         parentFragmentManager.setFragmentResultListener(START_DELETION_KEY, this) { _, _ ->
37             showConfirmationDialog()
38         }
39 
40         childFragmentManager.setFragmentResultListener(TRY_AGAIN_EVENT, this) { _, _ ->
41             showConfirmationDialog()
42         }
43 
44         childFragmentManager.setFragmentResultListener(CONFIRMATION_KEY, this) { _, _ ->
45             viewModel.delete()
46         }
47     }
48 
49     override fun onCreateView(
50         inflater: LayoutInflater,
51         container: ViewGroup?,
52         savedInstanceState: Bundle?,
53     ): View? {
54         return inflater.inflate(R.layout.fragment_empty, container, false)
55     }
56 
57     override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
58         super.onViewCreated(view, savedInstanceState)
59         viewModel.deletionProgress.observe(viewLifecycleOwner) { deletion ->
60             when (deletion) {
61                 DeletionViewModel.DeletionProgress.NOT_STARTED -> {
62                     dismissLoadingDialog()
63                 }
64                 DeletionViewModel.DeletionProgress.PROGRESS_INDICATOR_CAN_START -> {
65                     showLoadingDialog()
66                 }
67                 DeletionViewModel.DeletionProgress.COMPLETED -> {
68                     showSuccessDialog()
69                 }
70                 DeletionViewModel.DeletionProgress.PROGRESS_INDICATOR_CAN_END -> {
71                     dismissLoadingDialog()
72                 }
73                 DeletionViewModel.DeletionProgress.FAILED -> {
74                     showFailedDialog()
75                 }
76                 else -> {
77                     // do nothing
78                 }
79             }
80         }
81     }
82 
83     private fun showFailedDialog() {
84         dismissLoadingDialog()
85         FailedDialogFragment().show(childFragmentManager, FailedDialogFragment.TAG)
86     }
87 
88     private fun dismissLoadingDialog() {
89         val loadingDialog =
90             childFragmentManager.findFragmentByTag(DeletionLoadingDialogFragment.TAG)
91         if (loadingDialog != null) {
92             (loadingDialog as DeletionLoadingDialogFragment).dismiss()
93         }
94     }
95 
96     private fun showLoadingDialog() {
97         if (childFragmentManager.findFragmentByTag(DeletionLoadingDialogFragment.TAG) == null) {
98             DeletionLoadingDialogFragment()
99                 .show(childFragmentManager, DeletionLoadingDialogFragment.TAG)
100         }
101     }
102 
103     private fun showConfirmationDialog() {
104         DeletionConfirmationDialogFragment()
105             .show(childFragmentManager, DeletionConfirmationDialogFragment.TAG)
106     }
107 
108     private fun showSuccessDialog() {
109         SuccessDialogFragment().show(childFragmentManager, SuccessDialogFragment.TAG)
110     }
111 }
112