Skip to content

Commit

Permalink
Chore/108 cleanup code flutter 2 (#128)
Browse files Browse the repository at this point in the history
* Correct typo in location and change login to signin #108

* correct name for device data widget #108

* Made streamcontrollers private, to properly follow bloc architecture #108
  • Loading branch information
Julielsu authored Nov 14, 2023
1 parent 5621dd0 commit 528edb6
Show file tree
Hide file tree
Showing 24 changed files with 67 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ import 'package:rxdart/rxdart.dart';
*/

class DeviceNameBloc {
final deviceNameController = BehaviorSubject<String>();
final _deviceNameController = BehaviorSubject<String>();

Stream<String> get deviceNameStream => deviceNameController.stream;
Stream<String> get deviceNameStream => _deviceNameController.stream;

Sink<String> get deviceNameSink => deviceNameController.sink;
Sink<String> get deviceNameSink => _deviceNameController.sink;

void addDeviceName(String deviceName) {
deviceNameSink.add(deviceName);
}

void dispose() {
deviceNameController.close();
_deviceNameController.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ import 'package:sensors_plus/sensors_plus.dart';
*/

class AccelerometerBloc {
final accelerometerController = BehaviorSubject<AccelerometerEvent>();
final _accelerometerController = BehaviorSubject<AccelerometerEvent>();

Stream<AccelerometerEvent> get accelerometerStream =>
accelerometerController.stream;
_accelerometerController.stream;

Sink<AccelerometerEvent> get accelerometerSink =>
accelerometerController.sink;
_accelerometerController.sink;

void addAccelerometer(AccelerometerEvent acc) {
accelerometerSink.add(acc);
}

void dispose() {
accelerometerController.close();
_accelerometerController.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ import 'package:rxdart/rxdart.dart';
*/

class LocationBloc {
final locationController = BehaviorSubject<Position>();
final _locationController = BehaviorSubject<Position>();

Stream<Position> get accelerometerStream => locationController.stream;
Stream<Position> get locationStream => _locationController.stream;

Sink<Position> get accelerometerSink => locationController.sink;
Sink<Position> get locationSink => _locationController.sink;

void addLocation(Position loc) {
accelerometerSink.add(loc);
locationSink.add(loc);
}

void dispose() {
locationController.close();
_locationController.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ import 'package:rxdart/rxdart.dart';
*/

class LuxBloc {
final luxController = BehaviorSubject<double>();
final _luxController = BehaviorSubject<double>();

Stream<double> get luxStream => luxController.stream;
Stream<double> get luxStream => _luxController.stream;

Sink<double> get luxSink => luxController.sink;
Sink<double> get luxSink => _luxController.sink;

void addLux(double lux) {
luxSink.add(lux);
}

void dispose() {
luxController.close();
_luxController.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ import 'package:rxdart/rxdart.dart';
*/

class NoiseBloc {
final noiseController = BehaviorSubject<double>();
final _noiseController = BehaviorSubject<double>();

Stream<double> get noiseStream => noiseController.stream;
Stream<double> get noiseStream => _noiseController.stream;

Sink<double> get noiseSink => noiseController.sink;
Sink<double> get noiseSink => _noiseController.sink;

void addNoise(double noise) {
noiseSink.add(noise);
}

void dispose() {
noiseController.close();
_noiseController.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import 'package:rxdart/rxdart.dart';
*/

class StartStopBloc {
final startStopController = BehaviorSubject<bool>.seeded(false);
final _startStopController = BehaviorSubject<bool>.seeded(false);

Stream<bool> get startStopStream => startStopController.stream;
Stream<bool> get startStopStream => _startStopController.stream;

Sink<bool> get startStopSink => startStopController.sink;
Sink<bool> get startStopSink => _startStopController.sink;

void switchState(bool b) {
startStopSink.add(b);
}

void dispose() {
startStopController.close();
_startStopController.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ import 'package:rxdart/rxdart.dart';
*/

class TokenBloc {
final tokenController = BehaviorSubject<String>();
final _tokenController = BehaviorSubject<String>();

Stream<String> get tokenStream => tokenController.stream;
Stream<String> get tokenStream => _tokenController.stream;

Sink<String> get tokenSink => tokenController.sink;
Sink<String> get tokenSink => _tokenController.sink;

void addToken(String token) {
tokenSink.add(token);
}

void dispose() {
tokenController.close();
_tokenController.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ import 'package:rxdart/rxdart.dart';
*/

class UsernameBloc {
final usernameController = BehaviorSubject<String>();
final _usernameController = BehaviorSubject<String>();

Stream<String> get usernameStream => usernameController.stream;
Stream<String> get usernameStream => _usernameController.stream;

Sink<String> get usernameSink => usernameController.sink;
Sink<String> get usernameSink => _usernameController.sink;

void addUsername(String username) {
usernameSink.add(username);
}

void dispose() {
usernameController.close();
_usernameController.close();
}
}
6 changes: 3 additions & 3 deletions norbit-mobileapp/flutter_application_1/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_application_1/screens/signin_screen.dart';
import 'package:flutter_application_1/services/login_service.dart';
import 'package:flutter_application_1/services/signin_service.dart';
import 'package:flutter_application_1/services/mqtt_service.dart';
import 'package:flutter_application_1/services/sensors/location_service.dart';
import 'package:flutter_application_1/services/sensors/noise_service.dart';
Expand Down Expand Up @@ -111,9 +111,9 @@ class MyApp extends StatelessWidget {
);
},
),
Provider<LogInService>(
Provider<SignInService>(
create: (context) {
return LogInService(
return SignInService(
usernameBloc: Provider.of<UsernameBloc>(context, listen: false),
tokenBloc: Provider.of<TokenBloc>(context, listen: false),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class HomeScreen extends StatelessWidget {
Provider.of<AccelerometerService>(context, listen: false);

return StreamBuilder<bool>(
stream: startStopBloc.startStopController,
stream: startStopBloc.startStopStream,
builder: (context, snapshot) {
if (snapshot.hasData) {
return Scaffold(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class MqttService {
client.secure = true;
client.onConnected = onConnected;

final usernameData = await usernameBloc.usernameController.stream.first;
final usernameData = await usernameBloc.usernameStream.first;
final MqttConnectMessage connMess =
MqttConnectMessage().withClientIdentifier(usernameData).startClean();
client.connectionMessage = connMess;
Expand All @@ -173,10 +173,10 @@ class MqttService {
Future<String> getTopic() async {
String usernameValue = '';
String deviceNameValue = '';
final usernameData = await usernameBloc.usernameController.stream.first;
final usernameData = await usernameBloc.usernameStream.first;
usernameValue = usernameData;
final deviceNameData =
await deviceNameBloc.deviceNameController.stream.first;
await deviceNameBloc.deviceNameStream.first;
deviceNameValue = deviceNameData;
return "$usernameValue/$deviceNameValue";
}
Expand All @@ -188,7 +188,7 @@ class MqttService {
}
String topic = await getTopic();
final luxTopic = '$topic/lux';
luxSubscription = luxBloc.luxController.stream.listen((luxData) {
luxSubscription = luxBloc.luxStream.listen((luxData) {
if (!luxEnable) {
return;
}
Expand All @@ -207,7 +207,7 @@ class MqttService {
Future<void> publishNoiseData() async {
String topic = await getTopic();
final noiseTopic = '$topic/noise';
noiseSubscription = noiseBloc.noiseController.stream.listen((noiseData) {
noiseSubscription = noiseBloc.noiseStream.listen((noiseData) {
if (!soundEnable) {
return;
}
Expand All @@ -227,7 +227,7 @@ class MqttService {
List<String> accelerometerList = [];
String topic = await getTopic();
final accelerometerTopic = '$topic/accelerometer';
accelerometerSubscription = accelerometerBloc.accelerometerController.stream
accelerometerSubscription = accelerometerBloc.accelerometerStream
.listen((accelerometerData) {
if (!accelerometerEnable) {
return;
Expand All @@ -253,7 +253,7 @@ class MqttService {
Future<void> publishLocationData() async {
String topic = await getTopic();
final locationTopic = '$topic/location';
locationBloc.locationController.stream.listen((locationData) {
locationBloc.locationStream.listen((locationData) {
if (!gpsEnable) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ import '../blocs/token_bloc.dart';
import '../blocs/username_bloc.dart';

/*
LogInService, manages user authentication and login operations using the Amplify library.
SignInService, manages user authentication and login operations using the Amplify library.
It provides methods for configuring Amplify, signing in with a web-based user interface, and signing out.
It interacts with the Amplify Auth category and communicates with the TokenBloc and UsernameBloc to manage user data.
*/

class LogInService {
class SignInService {
String? idToken;
String? username;
final UsernameBloc usernameBloc;
final TokenBloc tokenBloc;

LogInService({required this.usernameBloc, required this.tokenBloc}) {
SignInService({required this.usernameBloc, required this.tokenBloc}) {
_configureAmplify();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ DeviceData displays basic data about the device.
Per now it is only device name, however other important device data shouls be added here.
*/

class DeviceData extends StatelessWidget {
class DeviceDataWidget extends StatelessWidget {
final DeviceNameBloc deviceNameBloc;

const DeviceData({
const DeviceDataWidget({
super.key,
required this.deviceNameBloc,
});

@override
Widget build(BuildContext context) {
return StreamBuilder<String>(
stream: deviceNameBloc.deviceNameController,
stream: deviceNameBloc.deviceNameStream,
builder: (context, snapshotDeviceName) {
String deviceName = snapshotDeviceName.data ?? "N/A";
return Dialog(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ class RegisterDevicePopupWrapper extends StatelessWidget {
@override
Widget build(BuildContext context) {
return StreamBuilder<String>(
stream: usernameBloc.usernameController,
stream: usernameBloc.usernameStream,
builder: (context, snapshotUsername) {
return StreamBuilder<String>(
stream: tokenBloc.tokenController,
stream: tokenBloc.tokenStream,
builder: (context, snapshotToken) {
return StreamBuilder<String>(
stream: deviceNameBloc.deviceNameStream,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class AccelerometerWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return StreamBuilder<AccelerometerEvent>(
stream: accelerometerBloc.accelerometerController,
stream: accelerometerBloc.accelerometerStream,
builder: (context, snapshot) {
if (snapshot.hasData) {
return Column(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class LocationWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return StreamBuilder<Position>(
stream: locationBloc.locationController,
stream: locationBloc.locationStream,
builder: (context, snapshot) {
if (snapshot.hasData) {
return Column(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class LuxWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return StreamBuilder<double>(
stream: luxBloc.luxController,
stream: luxBloc.luxStream,
builder: (context, snapshot) {
if (snapshot.hasData) {
return Column(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class NoiseWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return StreamBuilder<double>(
stream: noiseBloc.noiseController,
stream: noiseBloc.noiseStream,
builder: (context, snapshot) {
if (snapshot.hasData) {
return Column(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import 'package:flutter/material.dart';
import 'package:flutter_application_1/blocs/device_name_bloc.dart';
import 'package:flutter_application_1/screens/signin_screen.dart';
import 'package:provider/provider.dart';
import '../services/login_service.dart';
import 'device_data.dart';
import '../services/signin_service.dart';
import 'device_data_widget.dart';

/*
The sidebar is present on the homescreen.
Expand All @@ -15,7 +15,7 @@ class SidebarWidget extends StatelessWidget {

@override
Widget build(BuildContext context) {
final logInService = Provider.of<LogInService>(context, listen: false);
final logInService = Provider.of<SignInService>(context, listen: false);
return Drawer(
child: ListView(
padding: EdgeInsets.zero,
Expand Down Expand Up @@ -55,7 +55,7 @@ class SidebarWidget extends StatelessWidget {
showDialog(
context: context,
builder: (BuildContext context) {
return DeviceData(
return DeviceDataWidget(
deviceNameBloc: Provider.of<DeviceNameBloc>(context),
);
},
Expand Down
Loading

0 comments on commit 528edb6

Please sign in to comment.