|
| 1 | +/* |
| 2 | + * Copyright 2024 Google LLC |
| 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.google.firebase.dataconnect.minimaldemo |
| 17 | + |
| 18 | +import android.os.Bundle |
| 19 | +import android.view.LayoutInflater |
| 20 | +import android.view.View |
| 21 | +import android.view.ViewGroup |
| 22 | +import androidx.activity.viewModels |
| 23 | +import androidx.appcompat.app.AppCompatActivity |
| 24 | +import androidx.lifecycle.flowWithLifecycle |
| 25 | +import androidx.lifecycle.lifecycleScope |
| 26 | +import androidx.recyclerview.widget.DividerItemDecoration |
| 27 | +import androidx.recyclerview.widget.LinearLayoutManager |
| 28 | +import androidx.recyclerview.widget.RecyclerView |
| 29 | +import com.google.firebase.dataconnect.minimaldemo.connector.GetAllItemsQuery |
| 30 | +import com.google.firebase.dataconnect.minimaldemo.databinding.ActivityListItemsBinding |
| 31 | +import com.google.firebase.dataconnect.minimaldemo.databinding.ListItemBinding |
| 32 | +import kotlinx.coroutines.flow.collectLatest |
| 33 | +import kotlinx.coroutines.launch |
| 34 | + |
| 35 | +class ListItemsActivity : AppCompatActivity() { |
| 36 | + |
| 37 | + private lateinit var myApplication: MyApplication |
| 38 | + private lateinit var viewBinding: ActivityListItemsBinding |
| 39 | + private val viewModel: ListItemsViewModel by viewModels { ListItemsViewModel.Factory } |
| 40 | + |
| 41 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 42 | + super.onCreate(savedInstanceState) |
| 43 | + myApplication = application as MyApplication |
| 44 | + |
| 45 | + viewBinding = ActivityListItemsBinding.inflate(layoutInflater) |
| 46 | + viewBinding.recyclerView.also { |
| 47 | + val linearLayoutManager = LinearLayoutManager(this) |
| 48 | + it.layoutManager = linearLayoutManager |
| 49 | + val dividerItemDecoration = DividerItemDecoration(this, linearLayoutManager.layoutDirection) |
| 50 | + it.addItemDecoration(dividerItemDecoration) |
| 51 | + } |
| 52 | + setContentView(viewBinding.root) |
| 53 | + |
| 54 | + lifecycleScope.launch { |
| 55 | + if (viewModel.loadingState == ListItemsViewModel.LoadingState.NotStarted) { |
| 56 | + viewModel.getItems() |
| 57 | + } |
| 58 | + viewModel.stateSequenceNumber.flowWithLifecycle(lifecycle).collectLatest { |
| 59 | + onViewModelStateChange() |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private fun onViewModelStateChange() { |
| 65 | + val items = viewModel.result?.getOrNull() |
| 66 | + val exception = viewModel.result?.exceptionOrNull() |
| 67 | + val loadingState = viewModel.loadingState |
| 68 | + |
| 69 | + if (loadingState == ListItemsViewModel.LoadingState.InProgress) { |
| 70 | + viewBinding.statusText.text = "Loading Items..." |
| 71 | + viewBinding.statusText.visibility = View.VISIBLE |
| 72 | + viewBinding.recyclerView.visibility = View.GONE |
| 73 | + viewBinding.recyclerView.adapter = null |
| 74 | + } else if (items !== null) { |
| 75 | + viewBinding.statusText.text = null |
| 76 | + viewBinding.statusText.visibility = View.GONE |
| 77 | + viewBinding.recyclerView.visibility = View.VISIBLE |
| 78 | + val oldAdapter = viewBinding.recyclerView.adapter as? RecyclerViewAdapterImpl |
| 79 | + if (oldAdapter === null || oldAdapter.items !== items) { |
| 80 | + viewBinding.recyclerView.adapter = RecyclerViewAdapterImpl(items) |
| 81 | + } |
| 82 | + } else if (exception !== null) { |
| 83 | + viewBinding.statusText.text = "Loading items FAILED: $exception" |
| 84 | + viewBinding.statusText.visibility = View.VISIBLE |
| 85 | + viewBinding.recyclerView.visibility = View.GONE |
| 86 | + viewBinding.recyclerView.adapter = null |
| 87 | + } else { |
| 88 | + viewBinding.statusText.text = null |
| 89 | + viewBinding.statusText.visibility = View.GONE |
| 90 | + viewBinding.recyclerView.visibility = View.GONE |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private class RecyclerViewAdapterImpl(val items: List<GetAllItemsQuery.Data.ItemsItem>) : |
| 95 | + RecyclerView.Adapter<RecyclerViewViewHolderImpl>() { |
| 96 | + |
| 97 | + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewViewHolderImpl { |
| 98 | + val binding = ListItemBinding.inflate(LayoutInflater.from(parent.context), parent, false) |
| 99 | + return RecyclerViewViewHolderImpl(binding) |
| 100 | + } |
| 101 | + |
| 102 | + override fun getItemCount() = items.size |
| 103 | + |
| 104 | + override fun onBindViewHolder(holder: RecyclerViewViewHolderImpl, position: Int) { |
| 105 | + holder.bindTo(items[position]) |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + private class RecyclerViewViewHolderImpl(private val binding: ListItemBinding) : |
| 110 | + RecyclerView.ViewHolder(binding.root) { |
| 111 | + |
| 112 | + fun bindTo(item: GetAllItemsQuery.Data.ItemsItem) { |
| 113 | + binding.id.text = item.id.toString() |
| 114 | + binding.name.text = item.string |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments