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 17 package com.android.wallpaper.picker.category.ui.binder 18 19 import android.view.View 20 import android.widget.ProgressBar 21 import androidx.lifecycle.Lifecycle 22 import androidx.lifecycle.LifecycleOwner 23 import androidx.lifecycle.lifecycleScope 24 import androidx.lifecycle.repeatOnLifecycle 25 import androidx.recyclerview.widget.RecyclerView 26 import com.android.wallpaper.R 27 import com.android.wallpaper.picker.category.ui.viewmodel.CategoriesViewModel 28 import kotlinx.coroutines.flow.distinctUntilChanged 29 import kotlinx.coroutines.launch 30 31 /** Binds the wallpaper categories and its meta data to the category screen */ 32 object CategoriesBinder { 33 34 fun bind( 35 categoriesPage: View, 36 viewModel: CategoriesViewModel, 37 windowWidth: Int, 38 lifecycleOwner: LifecycleOwner, 39 navigationHandler: 40 (navigationEvent: CategoriesViewModel.NavigationEvent, navLogic: (() -> Unit)?) -> Unit, 41 ) { 42 // instantiate the grid and assign its adapter and layout configuration 43 val sectionsListView = categoriesPage.requireViewById<RecyclerView>(R.id.category_grid) 44 val progressBar: ProgressBar = categoriesPage.requireViewById(R.id.loading_indicator) 45 lifecycleOwner.lifecycleScope.launch { 46 lifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) { 47 launch { 48 viewModel.isLoading.collect { isLoading -> 49 progressBar.visibility = if (isLoading) View.VISIBLE else View.GONE 50 sectionsListView.visibility = if (isLoading) View.GONE else View.VISIBLE 51 } 52 } 53 54 // bind the state for List<SectionsViewModel> 55 launch { 56 viewModel.sections.collect { sections -> 57 SectionsBinder.bind(sectionsListView, sections, windowWidth, lifecycleOwner) 58 } 59 } 60 61 launch { 62 viewModel.isConnectionObtained.distinctUntilChanged().collect { _ -> 63 // Trigger a refresh of the categories every time the network status changes 64 viewModel.refreshNetworkCategories() 65 } 66 } 67 68 launch { 69 viewModel.navigationEvents.collect { navigationEvent -> 70 when (navigationEvent) { 71 is CategoriesViewModel.NavigationEvent.NavigateToWallpaperCollection, 72 is CategoriesViewModel.NavigationEvent.NavigateToPreviewScreen, 73 is CategoriesViewModel.NavigationEvent.NavigateToThirdParty -> { 74 // Perform navigation with event.data 75 navigationHandler(navigationEvent, null) 76 } 77 is CategoriesViewModel.NavigationEvent.NavigateToPhotosPicker -> { 78 navigationHandler(navigationEvent) { 79 viewModel.updateMyPhotosCategory() 80 } 81 } 82 } 83 } 84 } 85 } 86 } 87 } 88 } 89