|
|
@@ -0,0 +1,92 @@ |
|
|
|
package com.example.greenwatch.adapters; |
|
|
|
|
|
|
|
import android.graphics.Color; |
|
|
|
import android.view.LayoutInflater; |
|
|
|
import android.view.View; |
|
|
|
import android.view.ViewGroup; |
|
|
|
import android.widget.TextView; |
|
|
|
|
|
|
|
import androidx.annotation.NonNull; |
|
|
|
import androidx.recyclerview.widget.RecyclerView; |
|
|
|
|
|
|
|
import com.example.greenwatch.R; |
|
|
|
import com.example.greenwatch.models.Device; |
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.Collections; |
|
|
|
import java.util.Comparator; |
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
public class DeviceListAdapter extends RecyclerView.Adapter<DeviceListAdapter.DeviceListHolder> { |
|
|
|
private List<Device> devices = new ArrayList<>(); |
|
|
|
|
|
|
|
|
|
|
|
@NonNull |
|
|
|
@Override |
|
|
|
public DeviceListHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { |
|
|
|
View itemView = LayoutInflater.from(parent.getContext()) |
|
|
|
.inflate(R.layout.device_item, parent, false); |
|
|
|
return new DeviceListHolder(itemView); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onBindViewHolder(@NonNull DeviceListHolder holder, int position) { |
|
|
|
Device currentDevice = devices.get(position); |
|
|
|
holder.textViewSensorType.setText(holder.itemView.getContext().getString(R.string.sensor_type_placeholder, currentDevice.getSensorType())); |
|
|
|
holder.textViewDeviceID.setText(holder.itemView.getContext().getString(R.string.deviceID_placeholder, currentDevice.getDeviceID())); |
|
|
|
holder.textViewTimeStamp.setText(holder.itemView.getContext().getString(R.string.sensor_time_stamp_placeholder, currentDevice.getTimeStamp())); |
|
|
|
holder.textViewSensorMessage.setText(holder.itemView.getContext().getString(R.string.sensor_message_placeholder, currentDevice.getSensorMassage())); |
|
|
|
holder.setDeviceSensorStatus(currentDevice); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public int getItemCount() { |
|
|
|
return devices.size(); |
|
|
|
} |
|
|
|
|
|
|
|
public void setDevices(List<Device> devices) { |
|
|
|
this.devices = devices; |
|
|
|
Collections.sort(this.devices, new Comparator<Device>() { |
|
|
|
|
|
|
|
@Override |
|
|
|
public int compare(Device device1, Device device2) { |
|
|
|
if (device1.getSensorStatus() && !device2.getSensorStatus()) { |
|
|
|
return -1; |
|
|
|
} else if (!device1.getSensorStatus() && device2.getSensorStatus()) { |
|
|
|
return 1; |
|
|
|
} else { |
|
|
|
return 0; |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
notifyDataSetChanged(); |
|
|
|
} |
|
|
|
|
|
|
|
class DeviceListHolder extends RecyclerView.ViewHolder { |
|
|
|
private TextView textViewSensorType; |
|
|
|
private TextView textViewSensorStatus; |
|
|
|
private TextView textViewDeviceID; |
|
|
|
private TextView textViewTimeStamp; |
|
|
|
private TextView textViewSensorMessage; |
|
|
|
|
|
|
|
public DeviceListHolder(View itemView) { |
|
|
|
super(itemView); |
|
|
|
textViewSensorType = (TextView) itemView.findViewById(R.id.tvDeviceSensorType); |
|
|
|
textViewSensorStatus = (TextView) itemView.findViewById(R.id.tvDeviceSensorStatus); |
|
|
|
textViewDeviceID = (TextView) itemView.findViewById(R.id.tvDeviceID); |
|
|
|
textViewTimeStamp = (TextView) itemView.findViewById(R.id.tvDeviceTimeStamp); |
|
|
|
textViewSensorMessage = (TextView) itemView.findViewById(R.id.tvDeviceSensorMassage); |
|
|
|
} |
|
|
|
|
|
|
|
public void setDeviceSensorStatus(Device device) { |
|
|
|
if (device.getSensorStatus()) { |
|
|
|
textViewSensorStatus.setText(itemView.getContext().getString(R.string.sensor_status_placeholder, "AN")); |
|
|
|
itemView.setBackgroundColor(Color.RED); |
|
|
|
} |
|
|
|
else { |
|
|
|
textViewSensorStatus.setText(itemView.getContext().getString(R.string.sensor_status_placeholder, "AUS")); |
|
|
|
itemView.setBackgroundColor(Color.WHITE); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |