-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from adityastic/jhalak-sm
chore: suggestions
- Loading branch information
Showing
10 changed files
with
163 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import 'package:badgemagic/bademagic_module/bluetooth/completed_state.dart'; | ||
import 'package:badgemagic/bademagic_module/utils/toast_utils.dart'; | ||
import 'package:logger/logger.dart'; | ||
|
||
abstract class BleState { | ||
Future<BleState?> process(); | ||
} | ||
|
||
abstract class NormalBleState extends BleState { | ||
final logger = Logger(); | ||
final toast = ToastUtils(); | ||
|
||
Future<BleState?> processState(); | ||
|
||
@override | ||
Future<BleState?> process() async { | ||
try { | ||
return await processState(); | ||
} on Exception catch (e) { | ||
return CompletedState(isSuccess: false, message: e.toString()); | ||
} | ||
} | ||
} | ||
|
||
abstract class RetryBleState extends BleState { | ||
final logger = Logger(); | ||
final toast = ToastUtils(); | ||
|
||
final _maxRetries = 3; | ||
|
||
Future<BleState?> processState(); | ||
|
||
@override | ||
Future<BleState?> process() async { | ||
int attempt = 0; | ||
Exception? lastException; | ||
|
||
while (attempt < _maxRetries) { | ||
try { | ||
return await processState(); | ||
} on Exception catch (e) { | ||
logger.e(e); | ||
lastException = e; | ||
attempt++; | ||
if (attempt < _maxRetries) { | ||
logger.d("Retrying ($attempt/$_maxRetries)..."); | ||
await Future.delayed( | ||
const Duration(seconds: 2)); // Wait before retrying | ||
} else { | ||
logger.e("Max retries reached. Last exception: $lastException"); | ||
lastException = | ||
Exception("Max retries reached. Last exception: $lastException"); | ||
} | ||
} | ||
} | ||
|
||
// After max retries, return a CompletedState indicating failure. | ||
return CompletedState( | ||
isSuccess: false, | ||
message: lastException?.toString() ?? "Unknown error"); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import 'package:badgemagic/bademagic_module/bluetooth/base_ble_state.dart'; | ||
|
||
class CompletedState extends NormalBleState { | ||
final bool isSuccess; | ||
final String message; | ||
|
||
CompletedState({required this.isSuccess, required this.message}); | ||
|
||
@override | ||
Future<BleState?> processState() async { | ||
if (isSuccess) { | ||
toast.showToast(message); | ||
} else { | ||
toast.showErrorToast(message); | ||
} | ||
return null; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import 'package:badgemagic/bademagic_module/bluetooth/write_state.dart'; | ||
import 'package:flutter_blue_plus/flutter_blue_plus.dart'; | ||
import 'base_ble_state.dart'; | ||
|
||
class ConnectState extends RetryBleState { | ||
final ScanResult scanResult; | ||
|
||
ConnectState({required this.scanResult}); | ||
|
||
@override | ||
Future<BleState?> processState() async { | ||
bool connected = false; | ||
|
||
try { | ||
await scanResult.device.connect(autoConnect: false); | ||
BluetoothConnectionState connectionState = | ||
await scanResult.device.connectionState.first; | ||
|
||
if (connectionState == BluetoothConnectionState.connected) { | ||
connected = true; | ||
|
||
logger.d("Device connected"); | ||
toast.showToast('Device connected successfully.'); | ||
|
||
return WriteState(device: scanResult.device); | ||
} else { | ||
throw Exception("Failed to connect to the device"); | ||
} | ||
} catch (e) { | ||
toast.showErrorToast('Failed to connect retry'); | ||
rethrow; | ||
} finally { | ||
if (!connected) { | ||
await scanResult.device.disconnect(); | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.