-
Notifications
You must be signed in to change notification settings - Fork 219
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
feat: Added the BLE state machine to handle BLE state transition gracefully. #962
Conversation
… sec average to 8 sec average.
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.
Let me create a PR to your branch with suggestions
CardProvider contextProvider = GetIt.instance<CardProvider>(); | ||
|
||
//create a toast message for success state of BLE | ||
void successToast(String message) { |
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.
void successToast(String message) { | |
void showToast(String message) { |
//show toast for failure state of BLE | ||
void failureToast(String message) { | ||
ScaffoldMessenger.of(contextProvider.getContext()!).showSnackBar( | ||
SnackBar( | ||
margin: const EdgeInsets.symmetric(horizontal: 20, vertical: 10), | ||
elevation: 10, | ||
duration: const Duration(seconds: 1), | ||
content: Row( | ||
mainAxisAlignment: MainAxisAlignment.center, | ||
children: [ | ||
const Image( | ||
image: AssetImage('assets/icons/icon.png'), | ||
height: 20, | ||
), | ||
const SizedBox( | ||
width: 10, | ||
), | ||
Text( | ||
message, | ||
style: const TextStyle(color: Colors.black), | ||
) | ||
], | ||
), | ||
backgroundColor: Colors.white, | ||
behavior: SnackBarBehavior.floating, | ||
shape: RoundedRectangleBorder( | ||
borderRadius: BorderRadius.circular(10), | ||
), | ||
dismissDirection: DismissDirection.startToEnd, | ||
), | ||
); | ||
} |
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.
//show toast for failure state of BLE | |
void failureToast(String message) { | |
ScaffoldMessenger.of(contextProvider.getContext()!).showSnackBar( | |
SnackBar( | |
margin: const EdgeInsets.symmetric(horizontal: 20, vertical: 10), | |
elevation: 10, | |
duration: const Duration(seconds: 1), | |
content: Row( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
const Image( | |
image: AssetImage('assets/icons/icon.png'), | |
height: 20, | |
), | |
const SizedBox( | |
width: 10, | |
), | |
Text( | |
message, | |
style: const TextStyle(color: Colors.black), | |
) | |
], | |
), | |
backgroundColor: Colors.white, | |
behavior: SnackBarBehavior.floating, | |
shape: RoundedRectangleBorder( | |
borderRadius: BorderRadius.circular(10), | |
), | |
dismissDirection: DismissDirection.startToEnd, | |
), | |
); | |
} | |
// Create a error toast | |
void showErrorToast(String message) { | |
showToast('Error: $message'); | |
} |
import 'package:flutter/material.dart'; | ||
import 'package:get_it/get_it.dart'; | ||
|
||
class BleStateToast { |
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.
class BleStateToast { | |
class ToastUtils { |
You can shift this to /lib/bademagic_module/utils
BleState state = ScanState(); | ||
while (state is! CompletedState) { | ||
BleState? nextState = await state.processState(); | ||
if (nextState != null) { | ||
state = nextState; | ||
} else { | ||
break; | ||
} | ||
} | ||
if (state is CompletedState) { | ||
await state.processState(); // Ensure the toast is shown | ||
} |
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.
BleState state = ScanState(); | |
while (state is! CompletedState) { | |
BleState? nextState = await state.processState(); | |
if (nextState != null) { | |
state = nextState; | |
} else { | |
break; | |
} | |
} | |
if (state is CompletedState) { | |
await state.processState(); // Ensure the toast is shown | |
} | |
BleState? state = ScanState(); | |
while (state != null) { | |
state = await state.processState(); | |
} | |
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.
You should never return null unless it is completed state. Every other state should return a state, for failures it should shift to CompletedState
"${data.messages.length} message : ${data.messages[0].text} Flash : ${data.messages[0].flash} Marquee : ${data.messages[0].marquee} Mode : ${data.messages[0].mode}"); | ||
Future<void> checkAndTransfer() async { | ||
if (await FlutterBluePlus.isSupported == false) { | ||
toast.failureToast('Bluetooth is not supported by the device'); |
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.
toast.failureToast('Bluetooth is not supported by the device'); | |
toast.showErrorToast('Bluetooth is not supported by the device'); |
This can be done for all error toasts
await transferData(); | ||
} else { | ||
if (Platform.isAndroid) { | ||
toast.successToast('Turning on Bluetooth...'); |
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.
toast.successToast('Turning on Bluetooth...'); | |
toast.showToast('Turning on Bluetooth...'); |
Same for all usage
if (isSuccess) { | ||
toast.successToast(message); | ||
} | ||
toast.failureToast(message); | ||
return null; |
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.
This will post both success and error for an instance of CompletedState with isSuccess true
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.
if (isSuccess) { | |
toast.successToast(message); | |
} | |
toast.failureToast(message); | |
return null; | |
if (isSuccess) { | |
toast.showToast(message); | |
} else { | |
toast.showErrorToast(message); | |
} | |
return null; |
@@ -0,0 +1,3 @@ | |||
abstract class BleState { |
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.
Not an interface anymore, file name should indicate this
toast.successToast('Device connected successfully.'); | ||
return WriteState(device: scanResult.device); | ||
} else { | ||
logger.e("Failed to connect to the device"); |
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.
Throw an error rather than logging
const Duration(seconds: 2)); // Wait before retrying | ||
} else { | ||
logger.e("Max retries reached. Connection failed."); | ||
toast.failureToast('Failed to connect retry'); |
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.
toast.failureToast('Failed to connect retry'); | |
toast.failureToast('Failed to connect after $attempt retries'); |
chore: suggestions
…e app and reduced the data transfer time from 25sec average to 4-5 sec average.
#959