From d429b97cdb4fbd23dabe52e016571e79ccafd1fb Mon Sep 17 00:00:00 2001 From: Florianisme Date: Mon, 31 Jan 2022 20:42:46 +0100 Subject: [PATCH] Query initial dataset for Fragment instantiation --- .../wakeonlan/home/list/DeviceListAdapter.java | 9 +++++++-- .../wakeonlan/home/list/DeviceListFragment.java | 6 +++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/de/florianisme/wakeonlan/home/list/DeviceListAdapter.java b/app/src/main/java/de/florianisme/wakeonlan/home/list/DeviceListAdapter.java index 4b7099d..53246ff 100644 --- a/app/src/main/java/de/florianisme/wakeonlan/home/list/DeviceListAdapter.java +++ b/app/src/main/java/de/florianisme/wakeonlan/home/list/DeviceListAdapter.java @@ -23,16 +23,21 @@ public class DeviceListAdapter extends RecyclerView.Adapter devices = new ArrayList<>(); - public DeviceListAdapter(DeviceClickedCallback deviceClickedCallback) { + public DeviceListAdapter(List initialDataset, DeviceClickedCallback deviceClickedCallback) { + updateDevicesList(initialDataset); this.deviceClickedCallback = deviceClickedCallback; } public void updateDataset(List updatedDevices) { DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new DeviceDiffCallback(updatedDevices, this.devices)); - this.devices = Collections.unmodifiableList(updatedDevices); + updateDevicesList(updatedDevices); diffResult.dispatchUpdatesTo(this); } + private void updateDevicesList(List updatedDevices) { + this.devices = Collections.unmodifiableList(updatedDevices); + } + @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) { diff --git a/app/src/main/java/de/florianisme/wakeonlan/home/list/DeviceListFragment.java b/app/src/main/java/de/florianisme/wakeonlan/home/list/DeviceListFragment.java index 0fd1839..7519136 100644 --- a/app/src/main/java/de/florianisme/wakeonlan/home/list/DeviceListFragment.java +++ b/app/src/main/java/de/florianisme/wakeonlan/home/list/DeviceListFragment.java @@ -13,9 +13,12 @@ import com.google.android.material.snackbar.Snackbar; +import java.util.List; + import de.florianisme.wakeonlan.R; import de.florianisme.wakeonlan.databinding.FragmentListDevicesBinding; import de.florianisme.wakeonlan.persistence.DatabaseInstanceManager; +import de.florianisme.wakeonlan.persistence.entities.Device; import de.florianisme.wakeonlan.wear.WearClient; @@ -55,7 +58,8 @@ private void registerLiveDataObserver() { } private void instantiateRecyclerView() { - deviceListAdapter = new DeviceListAdapter(buildDeviceClickedCallback()); + List initialDataset = DatabaseInstanceManager.getInstance(getContext()).deviceDao().getAll(); + deviceListAdapter = new DeviceListAdapter(initialDataset, buildDeviceClickedCallback()); deviceListAdapter.setHasStableIds(true); RecyclerView devicesRecyclerView = binding.machineList;