Skip to content

Commit

Permalink
Query initial dataset for Fragment instantiation
Browse files Browse the repository at this point in the history
  • Loading branch information
Florianisme committed Jan 31, 2022
1 parent 862c829 commit d429b97
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,21 @@ public class DeviceListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHol
private final DeviceClickedCallback deviceClickedCallback;
private List<Device> devices = new ArrayList<>();

public DeviceListAdapter(DeviceClickedCallback deviceClickedCallback) {
public DeviceListAdapter(List<Device> initialDataset, DeviceClickedCallback deviceClickedCallback) {
updateDevicesList(initialDataset);
this.deviceClickedCallback = deviceClickedCallback;
}

public void updateDataset(List<Device> 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<Device> updatedDevices) {
this.devices = Collections.unmodifiableList(updatedDevices);
}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand Down Expand Up @@ -55,7 +58,8 @@ private void registerLiveDataObserver() {
}

private void instantiateRecyclerView() {
deviceListAdapter = new DeviceListAdapter(buildDeviceClickedCallback());
List<Device> initialDataset = DatabaseInstanceManager.getInstance(getContext()).deviceDao().getAll();
deviceListAdapter = new DeviceListAdapter(initialDataset, buildDeviceClickedCallback());
deviceListAdapter.setHasStableIds(true);
RecyclerView devicesRecyclerView = binding.machineList;

Expand Down

0 comments on commit d429b97

Please sign in to comment.