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.deletion.FailedDialogFragment
26 import com.android.healthconnect.controller.deletion.SuccessDialogFragment
27 import com.android.healthconnect.controller.selectabledeletion.DeletionConstants.CONFIRMATION_KEY
28 import com.android.healthconnect.controller.selectabledeletion.DeletionConstants.START_DELETION_KEY
29 import com.android.healthconnect.controller.selectabledeletion.DeletionConstants.TRY_AGAIN_EVENT
30 import dagger.hilt.android.AndroidEntryPoint
31 
32 @AndroidEntryPoint(Fragment::class)
33 class DeletionFragment : Hilt_DeletionFragment() {
34     private val viewModel: DeletionViewModel by activityViewModels()
35 
36     override fun onCreate(savedInstanceState: Bundle?) {
37         super.onCreate(savedInstanceState)
38         parentFragmentManager.setFragmentResultListener(START_DELETION_KEY, this) { _, _ ->
39             showConfirmationDialog()
40         }
41 
42         childFragmentManager.setFragmentResultListener(TRY_AGAIN_EVENT, this) { _, _ ->
43             showConfirmationDialog()
44         }
45 
46         childFragmentManager.setFragmentResultListener(CONFIRMATION_KEY, this) { _, _ ->
47             viewModel.delete()
48         }
49     }
50 
51     override fun onCreateView(
52         inflater: LayoutInflater,
53         container: ViewGroup?,
54         savedInstanceState: Bundle?
55     ): View? {
56         return inflater.inflate(R.layout.fragment_deletion, container, false)
57     }
58 
59     override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
60         super.onViewCreated(view, savedInstanceState)
61 
62         viewModel.deletionProgress.observe(viewLifecycleOwner) { deletion ->
63             when (deletion) {
64                 DeletionViewModel.DeletionProgress.NOT_STARTED -> {
65                     dismissLoadingDialog()
66                 }
67                 DeletionViewModel.DeletionProgress.PROGRESS_INDICATOR_CAN_START -> {
68                     showLoadingDialog()
69                 }
70                 DeletionViewModel.DeletionProgress.COMPLETED -> {
71                     showSuccessDialog()
72                 }
73                 DeletionViewModel.DeletionProgress.PROGRESS_INDICATOR_CAN_END -> {
74                     dismissLoadingDialog()
75                 }
76                 DeletionViewModel.DeletionProgress.FAILED -> {
77                     showFailedDialog()
78                 }
79                 else -> {
80                     // do nothing
81                 }
82             }
83         }
84     }
85 
86     private fun showFailedDialog() {
87         dismissLoadingDialog()
88         FailedDialogFragment().show(childFragmentManager, FailedDialogFragment.TAG)
89     }
90 
91     private fun dismissLoadingDialog() {
92         val loadingDialog =
93             childFragmentManager.findFragmentByTag(DeletionLoadingDialogFragment.TAG)
94         if (loadingDialog != null) {
95             (loadingDialog as DeletionLoadingDialogFragment).dismiss()
96         }
97     }
98 
99     private fun showLoadingDialog() {
100         if (childFragmentManager.findFragmentByTag(DeletionLoadingDialogFragment.TAG) == null) {
101             DeletionLoadingDialogFragment()
102                 .show(childFragmentManager, DeletionLoadingDialogFragment.TAG)
103         }
104     }
105 
106     private fun showConfirmationDialog() {
107         NewDeletionConfirmationDialogFragment()
108             .show(childFragmentManager, NewDeletionConfirmationDialogFragment.TAG)
109     }
110 
111     private fun showSuccessDialog() {
112         SuccessDialogFragment().show(childFragmentManager, SuccessDialogFragment.TAG)
113     }
114 }
115