summaryrefslogtreecommitdiff
path: root/app/src/main/java/com/javispedro/rempe/DeviceViewHolder.java
blob: 9731471bf39f61f05e0eb2b30d5159409c6a4cb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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);
        });
    }
}