Skip to content

Commit

Permalink
feat: added websockets to receive job status
Browse files Browse the repository at this point in the history
  • Loading branch information
SGI-CAPP-AT2 committed Jan 24, 2025
1 parent 49bb285 commit 35d7b2a
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 6 deletions.
79 changes: 73 additions & 6 deletions lib/app/modules/home/controllers/home_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@ import 'package:taskwarrior/app/tour/home_page_tour.dart';
import 'package:taskwarrior/app/utils/constants/taskwarrior_colors.dart';
import 'package:taskwarrior/app/utils/language/supported_language.dart';
import 'package:taskwarrior/app/utils/taskchampion/credentials_storage.dart';
import 'package:taskwarrior/app/utils/taskchampion/websocket.dart';
import 'package:taskwarrior/app/utils/taskfunctions/comparator.dart';
import 'package:taskwarrior/app/utils/taskfunctions/projects.dart';
import 'package:taskwarrior/app/utils/taskfunctions/query.dart';
import 'package:taskwarrior/app/utils/taskfunctions/tags.dart';
import 'package:taskwarrior/app/utils/app_settings/app_settings.dart';
import 'package:tutorial_coach_mark/tutorial_coach_mark.dart';
import 'package:web_socket_channel/status.dart';
import 'package:web_socket_channel/web_socket_channel.dart';

class HomeController extends GetxController {
final SplashController splashController = Get.find<SplashController>();
Expand All @@ -55,6 +58,7 @@ class HomeController extends GetxController {
final ScrollController scrollController = ScrollController();
final RxBool showbtn = false.obs;
late TaskDatabase taskdb;
WebSocketChannel? wsChannel;
var tasks = <Tasks>[].obs;

@override
Expand All @@ -74,25 +78,32 @@ class HomeController extends GetxController {
taskdb.open();
getUniqueProjects();
_loadTaskChampion();
ever(taskchampion, (bool value) async {
if (value) {
wsChannel = await initCCSyncUpdatesWs();
} else {
if (wsChannel != null) wsChannel?.sink.close(goingAway);
}
});
if (Platform.isAndroid) {
handleHomeWidgetClicked();
}
fetchTasksFromDB();
everAll([
everAll([
pendingFilter,
waitingFilter,
projectFilter,
tagUnion,
selectedSort,
selectedTags,
], (_) {
if (Platform.isAndroid) {
if (Platform.isAndroid) {
WidgetController widgetController =
Get.put(WidgetController());
widgetController.fetchAllData();
widgetController.fetchAllData();

widgetController.update();
}
widgetController.update();
}
});
}

Expand Down Expand Up @@ -518,7 +529,7 @@ class HomeController extends GetxController {


];
RxString priority = 'X'.obs;
RxString priority = 'None'.obs;

final tagcontroller = TextEditingController();
RxList<String> tags = <String>[].obs;
Expand Down Expand Up @@ -717,4 +728,60 @@ class HomeController extends GetxController {
Widget showDialog = taskchampion.value ? AddTaskToTaskcBottomSheet(homeController: this) : AddTaskBottomSheet(homeController: this);
Get.dialog(showDialog);
}

Future<WebSocketChannel> initCCSyncUpdatesWs() async {
Map<String, String> successMessages = {
"Add Task": "Task added successfully",
"Edit Task": "Task edited successfully",
"Complete Task": "Task completed successfully",
"Delete Task": "Task deleted successfully",
};
Map<String, String> failureMessages = {
"Add Task": "Task addition failed",
"Edit Task": "Task edit failed",
"Complete Task": "Task completion failed",
"Delete Task": "Task deletion failed",
};
String? clientId = await CredentialsStorage.getClientId();
return listenForTaskUpdates(getWsUrl(baseUrl, clientId),
(TaskUpdate update) {
debugPrint("Success: ${update.job} ${successMessages[update.job]!}");
if (successMessages.containsKey(update.job)) {
ScaffoldMessenger.of(Get.context!).showSnackBar(SnackBar(
content: Text(
'${successMessages[update.job]}',
style: TextStyle(
color: AppSettings.isDarkMode
? TaskWarriorColors.kprimaryTextColor
: TaskWarriorColors.kLightPrimaryTextColor,
),
),
backgroundColor: AppSettings.isDarkMode
? TaskWarriorColors.ksecondaryBackgroundColor
: TaskWarriorColors.kLightSecondaryBackgroundColor,
duration: const Duration(seconds: 2)));
}
}, (TaskUpdate update) {
ScaffoldMessenger.of(Get.context!).showSnackBar(SnackBar(
content: Text(
'${failureMessages[update.job]}',
style: TextStyle(
color: AppSettings.isDarkMode
? TaskWarriorColors.kprimaryTextColor
: TaskWarriorColors.kLightPrimaryTextColor,
),
),
backgroundColor: AppSettings.isDarkMode
? TaskWarriorColors.ksecondaryBackgroundColor
: TaskWarriorColors.kLightSecondaryBackgroundColor,
duration: const Duration(seconds: 2)));
});
}

@override
void onClose() {
super.onClose();
taskdb.close();
if (wsChannel != null) wsChannel?.sink.close();
}
}
40 changes: 40 additions & 0 deletions lib/app/utils/taskchampion/websocket.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:web_socket_channel/web_socket_channel.dart';

class TaskUpdate {
final String status;
final String job;
const TaskUpdate(this.status, this.job);
TaskUpdate.fromJson(Map<String, dynamic> json)
: status = json['status'],
job = json['job'];
Map<String, dynamic> toJson() => {
'status': status,
'job': job,
};
}

Future<WebSocketChannel> listenForTaskUpdates(url, onSuccess, onFailure) async {
final webSocketChannel = WebSocketChannel.connect(
Uri.parse(url),
);
await webSocketChannel.ready;
webSocketChannel.stream.listen((message) {
debugPrint(message);
TaskUpdate update =
TaskUpdate.fromJson(jsonDecode(message) as Map<String, dynamic>);
if (update.status == 'success') {
onSuccess(update);
}
if (update.status == 'failure') {
onFailure(update);
}
});
return webSocketChannel;
}

String getWsUrl(url, clientId) {
return url.replaceFirst('http', 'ws') + '/ws?clientID=' + clientId;
}
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ dependencies:
url_launcher: ^6.1.14
uuid: ^4.2.2
built_collection: ^5.1.1
web_socket_channel: ^2.4.0

dev_dependencies:
build_runner: null
Expand Down

0 comments on commit 35d7b2a

Please sign in to comment.