-
Notifications
You must be signed in to change notification settings - Fork 816
feat: ported thermometer screen. #2761
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: flutter
Are you sure you want to change the base?
Conversation
Reviewer's GuideThis PR ports the thermometer instrument to the Flutter app by implementing Android native temperature sensor integration with MethodChannel and EventChannel in MainActivity, exposing a Flutter TemperatureService, adding a state provider to track and chart readings, creating new UI screens and widgets for real-time temperature display, and integrating navigation routes and resource constants. Sequence diagram for temperature sensor data flow from Android to FluttersequenceDiagram
participant User as actor User
participant FlutterApp as Flutter (ThermometerScreen)
participant TempService as TemperatureService (Flutter)
participant MainActivity as MainActivity (Android)
participant Sensor as Android SensorManager
User->>FlutterApp: Navigates to ThermometerScreen
FlutterApp->>TempService: initializeSensors()
TempService->>MainActivity: MethodChannel: isTemperatureSensorAvailable
MainActivity->>Sensor: getDefaultSensor(TYPE_AMBIENT_TEMPERATURE)
MainActivity-->>TempService: true/false
TempService->>MainActivity: MethodChannel: startTemperatureUpdates
MainActivity->>Sensor: registerListener
MainActivity-->>TempService: true
TempService->>MainActivity: MethodChannel: getCurrentTemperature
MainActivity-->>TempService: currentTemperature
MainActivity-->>TempService: EventChannel: temperature_stream (continuous)
loop Every sensor update
MainActivity-->>TempService: EventChannel: temperature value
TempService-->>FlutterApp: notifyListeners() (UI updates)
end
Class diagram for thermometer state management and service integrationclassDiagram
class ThermometerStateProvider {
- double _currentTemperature
- Timer? _timeTimer
- StreamSubscription<double>? _temperatureSubscription
- List<double> _temperatureData
- List<double> _timeData
- List<FlSpot> temperatureChartData
- double _startTime
- double _currentTime
- int _maxLength
- double _temperatureMin
- double _temperatureMax
- double _temperatureSum
- int _dataCount
- bool _isSensorAvailable
- bool _isInitialized
+ Future<void> initializeSensors()
+ void disposeSensors()
+ double getCurrentTemperature()
+ double getMinTemperature()
+ double getMaxTemperature()
+ double getAverageTemperature()
+ List<FlSpot> getTemperatureChartData()
+ int getDataLength()
+ double getCurrentTime()
+ double getMaxTime()
+ double getMinTime()
+ bool isSensorAvailable()
+ bool isInitialized()
+ double getTimeInterval()
}
class TemperatureService {
<<static>>
+ Future<bool> isTemperatureSensorAvailable()
+ Future<double> getCurrentTemperature()
+ Future<bool> startTemperatureUpdates()
+ Future<void> stopTemperatureUpdates()
+ Stream<double> temperatureStream
+ void dispose()
}
ThermometerStateProvider --> TemperatureService : uses
Class diagram for Android MainActivity sensor integrationclassDiagram
class MainActivity {
- SensorManager sensorManager
- Sensor temperatureSensor
- MethodChannel temperatureChannel
- EventChannel temperatureEventChannel
- EventChannel.EventSink temperatureEventSink
- boolean isListening
- float currentTemperature
+ void configureFlutterEngine(FlutterEngine)
+ void onSensorChanged(SensorEvent)
+ void onAccuracyChanged(Sensor, int)
+ void onDestroy()
+ void onPause()
+ void onResume()
}
MainActivity ..> SensorManager : uses
MainActivity ..> Sensor : uses
MainActivity ..> MethodChannel : exposes
MainActivity ..> EventChannel : exposes
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @Yugesh-Kumar-S - I've reviewed your changes - here's some feedback:
- In
_showSensorErrorSnackbar
, replace the hard-coded Colors.grey and white text color with the newly addedsnackBarBackgroundColor
andsnackBarContentColor
constants to keep styling consistent. - When the temperature sensor is unavailable or on initialization error, cancel the periodic
_timeTimer
immediately to avoid running an idle timer unnecessarily. - You’re calling
notifyListeners()
both in the periodic timer and again on each sensor update—consider batching those state changes so you only rebuild once per cycle and reduce redundant widget rebuilds.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In `_showSensorErrorSnackbar`, replace the hard-coded Colors.grey and white text color with the newly added `snackBarBackgroundColor` and `snackBarContentColor` constants to keep styling consistent.
- When the temperature sensor is unavailable or on initialization error, cancel the periodic `_timeTimer` immediately to avoid running an idle timer unnecessarily.
- You’re calling `notifyListeners()` both in the periodic timer and again on each sensor update—consider batching those state changes so you only rebuild once per cycle and reduce redundant widget rebuilds.
## Individual Comments
### Comment 1
<location> `lib/constants.dart:306` </location>
<code_context>
String privacyPolicyMenu = 'Privacy Policy';
+String thermometerTitle = 'Thermometer';
+String thermometerIntro =
+ 'Thermometer instrument is used to measure ambient temprature. It can be measured using inbuilt ambient temprature sensor or through SHT21.';
+String celsius = '°C';
+String temperatureSensorError = 'Temperature sensor error:';
</code_context>
<issue_to_address>
Typo in 'temprature' should be 'temperature'.
Please update all occurrences of 'temprature' in thermometerIntro to 'temperature'.
</issue_to_address>
<suggested_fix>
<<<<<<< SEARCH
String thermometerIntro =
'Thermometer instrument is used to measure ambient temprature. It can be measured using inbuilt ambient temprature sensor or through SHT21.';
=======
String thermometerIntro =
'Thermometer instrument is used to measure ambient temperature. It can be measured using inbuilt ambient temperature sensor or through SHT21.';
>>>>>>> REPLACE
</suggested_fix>
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Build successful. APKs to test: https://github.com/fossasia/pslab-android/actions/runs/16126624905/artifacts/3480352004 |
0eeda59
to
4195b96
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @Yugesh-Kumar-S - I've reviewed your changes - here's some feedback:
- Consider extracting the Android sensor logic out of MainActivity into a dedicated plugin or helper class to keep your Flutter host activity clean and reusable.
- Make sure to call TemperatureService.dispose (or otherwise close the StreamController) when the screen or app is torn down to avoid leaking the broadcast stream.
- You’ve duplicated temperature‐validation logic in both Java and Dart—extract it into a shared utility or at least centralize the rules to prevent drift over time.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider extracting the Android sensor logic out of MainActivity into a dedicated plugin or helper class to keep your Flutter host activity clean and reusable.
- Make sure to call TemperatureService.dispose (or otherwise close the StreamController) when the screen or app is torn down to avoid leaking the broadcast stream.
- You’ve duplicated temperature‐validation logic in both Java and Dart—extract it into a shared utility or at least centralize the rules to prevent drift over time.
## Individual Comments
### Comment 1
<location> `lib/constants.dart:371` </location>
<code_context>
String privacyPolicyMenu = 'Privacy Policy';
+String thermometerTitle = 'Thermometer';
+String thermometerIntro =
+ 'Thermometer instrument is used to measure ambient temprature. It can be measured using inbuilt ambient temprature sensor or through SHT21.';
+String celsius = '°C';
+String temperatureSensorError = 'Temperature sensor error:';
</code_context>
<issue_to_address>
Typo in 'temprature' should be 'temperature'.
Please correct all instances of 'temprature' to 'temperature' in the thermometerIntro string to avoid user confusion.
</issue_to_address>
<suggested_fix>
<<<<<<< SEARCH
String thermometerIntro =
'Thermometer instrument is used to measure ambient temprature. It can be measured using inbuilt ambient temprature sensor or through SHT21.';
=======
String thermometerIntro =
'Thermometer instrument is used to measure ambient temperature. It can be measured using inbuilt ambient temperature sensor or through SHT21.';
>>>>>>> REPLACE
</suggested_fix>
### Comment 2
<location> `lib/view/widgets/thermometer_card.dart:23` </location>
<code_context>
+ ThermometerStateProvider provider =
+ Provider.of<ThermometerStateProvider>(context);
+ double currentTemp = provider.getCurrentTemperature();
+ double minTemp = provider.getMinTemperature();
+ double maxTemp = provider.getMaxTemperature();
+ double avgTemp = provider.getAverageTemperature();
+ final cardMargin = screenWidth < 400 ? 8.0 : 12.0;
</code_context>
<issue_to_address>
Initial min/max temperature values may be misleading.
Returning 0 by default may cause confusion; consider using null or a sentinel value to indicate no data, and update the UI accordingly.
Suggested implementation:
```
double? minTemp = provider.getMinTemperature();
double? maxTemp = provider.getMaxTemperature();
```
You will also need to:
1. Update `ThermometerStateProvider.getMinTemperature()` and `getMaxTemperature()` to return `double?` and return `null` (or a sentinel value) when there is no data.
2. Update any UI code that displays `minTemp` and `maxTemp` to check for `null` and display a placeholder (e.g., "—" or "N/A") if the value is `null`. For example:
```dart
Text(minTemp != null ? minTemp.toStringAsFixed(1) : "—")
```
</issue_to_address>
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
String thermometerIntro = | ||
'Thermometer instrument is used to measure ambient temprature. It can be measured using inbuilt ambient temprature sensor or through SHT21.'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (typo): Typo in 'temprature' should be 'temperature'.
Please correct all instances of 'temprature' to 'temperature' in the thermometerIntro string to avoid user confusion.
String thermometerIntro = | |
'Thermometer instrument is used to measure ambient temprature. It can be measured using inbuilt ambient temprature sensor or through SHT21.'; | |
String thermometerIntro = | |
'Thermometer instrument is used to measure ambient temperature. It can be measured using inbuilt ambient temperature sensor or through SHT21.'; |
double minTemp = provider.getMinTemperature(); | ||
double maxTemp = provider.getMaxTemperature(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Initial min/max temperature values may be misleading.
Returning 0 by default may cause confusion; consider using null or a sentinel value to indicate no data, and update the UI accordingly.
Suggested implementation:
double? minTemp = provider.getMinTemperature();
double? maxTemp = provider.getMaxTemperature();
You will also need to:
- Update
ThermometerStateProvider.getMinTemperature()
andgetMaxTemperature()
to returndouble?
and returnnull
(or a sentinel value) when there is no data. - Update any UI code that displays
minTemp
andmaxTemp
to check fornull
and display a placeholder (e.g., "—" or "N/A") if the value isnull
. For example:
Text(minTemp != null ? minTemp.toStringAsFixed(1) : "—")
Fixes #2760
Changes
Screenshots / Recordings
simulated using virtual sensor
Screen_recording_20250708_013433.webm
Checklist:
strings.xml
,dimens.xml
andcolors.xml
without hard coding any value.strings.xml
,dimens.xml
orcolors.xml
.Summary by Sourcery
Port the thermometer instrument into the Flutter app by adding its UI, state management, sensor integration, and navigation.
New Features:
Enhancements:
Chores:
Summary by Sourcery
Port the thermometer instrument to the Flutter application by adding its UI, state management, sensor integration, and navigation support
New Features:
Enhancements:
Build:
Chores: