package com.javispedro.rempe; import android.content.Context; import android.view.View; import android.widget.ProgressBar; import android.widget.TextView; import androidx.recyclerview.widget.RecyclerView; import com.dsi.ant.plugins.antplus.pcc.defines.RequestAccessResult; import java.math.BigDecimal; public class DeviceViewHolder extends RecyclerView.ViewHolder implements Device.DeviceObserver { private static final String TAG = "DeviceViewHolder"; private final View mView; private final Context mContext; private final TextView mTemperatureView; private final TextView mMinTemperatureView; private final TextView mMaxTemperatureView; private final TextView mNameView; private final TextView mStatusView; private final ProgressBar mSignalBar; private final TextView mSignalLabel; private Device mDevice; public DeviceViewHolder(View view) { super(view); mView = view; mContext = view.getContext(); mNameView = view.findViewById(R.id.nameView); mStatusView = view.findViewById(R.id.statusView); mTemperatureView = view.findViewById(R.id.temperatureView); mMinTemperatureView = view.findViewById(R.id.minTemperatureView); mMaxTemperatureView = view.findViewById(R.id.maxTemperatureView); mSignalBar = view.findViewById(R.id.signalBar); mSignalLabel = view.findViewById(R.id.signalLabel); } @Override public String toString() { return super.toString() + " '" + mNameView.getText() + "'"; } public void setDevice(Device device) { resetDisplay(); if (mDevice != null) { mDevice.setObserver(null); } mDevice = device; if (mDevice != null) { mNameView.setText(mDevice.getDeviceName()); mDevice.setObserver(this); } } private void runOnUiThread(Runnable r) { mView.post(r); } private void resetDisplay() { mTemperatureView.setText(mContext.getString(R.string.temperature_nothing)); mMinTemperatureView.setText(mContext.getString(R.string.temperature_nothing)); mMaxTemperatureView.setText(mContext.getString(R.string.temperature_nothing)); mNameView.setText(""); mStatusView.setText(""); mSignalBar.setProgress(0); mSignalLabel.setVisibility(View.INVISIBLE); } private String formatTemperature(BigDecimal temp) { final String value = temp.setScale(1, BigDecimal.ROUND_HALF_EVEN).toPlainString(); return mContext.getString(R.string.temperature_celsius, value); } @Override public void onDeviceInfoChanged() { runOnUiThread(() -> { mNameView.setText(mDevice.getDeviceName()); }); } @Override public void onDeviceStateChanged() { runOnUiThread(() -> { if (mDevice.getConnectResult() != RequestAccessResult.SUCCESS) { mStatusView.setText(Device.connectionRequestAccessResultToString(mContext, mDevice.getConnectResult())); } else { mStatusView.setText(Device.deviceStateToString(mContext, mDevice.getCurrentDeviceState())); } }); } @Override public void onDeviceNewReading() { runOnUiThread(() -> { final Reading reading = mDevice.getLastReading(); mTemperatureView.setText(formatTemperature(reading.temperature)); mMinTemperatureView.setText(formatTemperature(reading.lowLast24Hours)); mMaxTemperatureView.setText(formatTemperature(reading.highLast24Hours)); }); } private int rssiToMeterValue(float rssi) { final float minThreshold = -100; final float maxThreshold = 0; final float step = (maxThreshold - minThreshold) / 100; return Math.round((rssi - minThreshold) / step); } @Override public void onDeviceRssiChanged() { runOnUiThread(() -> { mSignalBar.setProgress(rssiToMeterValue(mDevice.getLastRssi())); mSignalLabel.setVisibility(View.VISIBLE); }); } }