1 /* <lambda>null2 * Copyright (C) 2023 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 17 package com.android.wallpaper.picker.preview.ui.binder 18 19 import android.app.Activity 20 import android.app.AlertDialog 21 import androidx.lifecycle.Lifecycle 22 import androidx.lifecycle.LifecycleOwner 23 import androidx.lifecycle.lifecycleScope 24 import androidx.lifecycle.repeatOnLifecycle 25 import com.android.wallpaper.R 26 import com.android.wallpaper.picker.preview.ui.viewmodel.WallpaperPreviewViewModel 27 import kotlinx.coroutines.launch 28 29 /** Binds the set wallpaper progress dialog. */ 30 object SetWallpaperProgressDialogBinder { 31 32 fun bind( 33 viewModel: WallpaperPreviewViewModel, 34 activity: Activity, 35 lifecycleOwner: LifecycleOwner, 36 ) { 37 var setWallpaperProgressDialog: AlertDialog? = null 38 39 lifecycleOwner.lifecycleScope.launch { 40 lifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) { 41 launch { 42 viewModel.isSetWallpaperProgressBarVisible.collect { visible -> 43 if (visible) { 44 val dialog = 45 setWallpaperProgressDialog 46 ?: createSetWallpaperProgressDialog(activity).also { 47 setWallpaperProgressDialog = it 48 } 49 dialog.show() 50 } else { 51 setWallpaperProgressDialog?.hide() 52 } 53 } 54 } 55 } 56 } 57 } 58 59 private fun createSetWallpaperProgressDialog( 60 activity: Activity, 61 ): AlertDialog { 62 val dialogView = 63 activity.layoutInflater.inflate(R.layout.set_wallpaper_progress_dialog_view, null) 64 return AlertDialog.Builder(activity).setView(dialogView).create() 65 } 66 } 67