Skip to content
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

Merged
merged 17 commits into from
Jul 3, 2024
Merged

feat: Added the BLE state machine to handle BLE state transition gracefully. #962

merged 17 commits into from
Jul 3, 2024

Conversation

Jhalakupadhyay
Copy link
Contributor

@Jhalakupadhyay Jhalakupadhyay commented Jul 2, 2024

#959

  1. Added the BLE state machine to handle different states to data transfer.
  2. Added the Screen Util package to make the UI adaptable to all screen sizes.
  3. Added function inside the checkAndConnect function that will check wether Bluetooth is on or not if not then ask to turn it on automatically in android and ask the IOS user to turn it on manually.
  4. Added the test for Byte_Array_Utils and converter class logic.
  5. Added the vectors that can be shown to the badge.

@Jhalakupadhyay Jhalakupadhyay requested a review from adityastic July 2, 2024 07:23
Copy link
Collaborator

@adityastic adityastic left a 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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
void successToast(String message) {
void showToast(String message) {

Comment on lines 41 to 72
//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,
),
);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
//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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
class BleStateToast {
class ToastUtils {

You can shift this to /lib/bademagic_module/utils

Comment on lines 62 to 73
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
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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();
}

Copy link
Collaborator

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');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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...');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
toast.successToast('Turning on Bluetooth...');
toast.showToast('Turning on Bluetooth...');

Same for all usage

Comment on lines 13 to 17
if (isSuccess) {
toast.successToast(message);
}
toast.failureToast(message);
return null;
Copy link
Collaborator

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

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 {
Copy link
Collaborator

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");
Copy link
Collaborator

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');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
toast.failureToast('Failed to connect retry');
toast.failureToast('Failed to connect after $attempt retries');

@Jhalakupadhyay Jhalakupadhyay requested a review from adityastic July 3, 2024 06:28
@adityastic adityastic merged commit 3145f21 into fossasia:flutter_app Jul 3, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants