Professional Documents
Culture Documents
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:local_notifier/local_notifier.dart';
import 'package:rxdart/subjects.dart';
import 'package:workcake/common/focus_inputbox_manager.dart';
import 'package:workcake/common/utils.dart';
import 'package:workcake/hive/direct/direct.model.dart';
import 'package:flutter/services.dart';
import 'package:workcake/isar/message_conversation/service.dart';
import 'dart:convert';
import 'package:workcake/providers/providers.dart';
import '../generated/l10n.dart';
@override
_NotificationMacOSState createState() => _NotificationMacOSState();
}
final id;
final title;
final body;
final payload;
}
Timer(Duration(seconds: 2), () {
final channel = Provider.of<Auth>(context, listen: false).channel;
if (Platform.isMacOS){
pushNotiMacOS(title, body, jsonEncode(messageDecrypted["message"]));
} else {
LocalNotification notification = LocalNotification(
title: title,
body: body
);
notification.show();
notification.onClick = () {
onGotoDirect(conversationId, jsonEncode(messageDecrypted["message"]));
};
}
}
} catch (e, trace) {
print("catch: $e $trace");
}
}
onClearBadge(channelId) async {
if (this.mounted) {
await Provider.of<Channels>(context, listen: false).clearBadge(channelId,
null, false);
await Utils.updateBadge(context);
}
}
setUpNotify() {
WidgetsFlutterBinding.ensureInitialized();
MacOSInitializationSettings initializationSettingsMacOS =
MacOSInitializationSettings(
requestAlertPermission: true,
requestBadgePermission: true,
requestSoundPermission: true
);
final InitializationSettings initializationSettings =
InitializationSettings(
macOS: initializationSettingsMacOS
);
flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: (payload) async {
var newPayload = jsonDecode(payload!);
var channelId = newPayload["channel_id"];
var workspaceId = newPayload["workspace_id"];
var conversationId = newPayload["conversation_id"];
if (conversationId != null) {
onGotoDirect(conversationId, newPayload);
} else {
onChangeWorkspace(workspaceId, channelId, newPayload);
}
selectNotificationSubject.add(payload);
}
);
pushNotiMacOS("", "", null, isDefault: true);
}
if (attachment != null) {
final string = attachment["type"] == "mention" ? attachment["data"].map((e) {
if (e["type"] == "text" ) return e["value"];
return "${e["trigger"] ?? "@"}${e["name"] ?? ""} ";
}).toList().join() :
attachment["type"] == "delete" ? "${attachment['delete_user_name']} was
kicked from this channel." :
attachment["type"] == "bot" ? "Sent an attachment" :
attachment["type"] == "change_name" ? "${attachment["user_name"]} has
changed channel name to ${attachment["params"]["name"]}" :
attachment["type"] == "invite" ? "${attachment["invited_user"]} has join a
channel" :
attachment["type"] == "leave_channel" ? "${attachment["user"]} has leave
the channel" :
attachment["type"] == "change_topic" ? "${attachment["user_name"]} has
changed channel topic to ${attachment["params"]["topic"]}" :
attachment["type"] == "change_private" ? "${attachment["user_name"]} has
changed channel private to ${attachment["params"]["is_private"] ? "private" :
"public"}" :
attachment["type"] == "sticker" ? S.current.sticker1 :
attachment["mime_type"] == "image" ? "Sent a photo" : "Sent an attachment";
return string;
} else {
return "Error payload";
}
}
pushNotify(payload) {
final onFocusApp = Provider.of<Auth>(context, listen: false).onFocusApp;
final tab = Provider.of<Workspaces>(context, listen: false).tab;
final data = Provider.of<Channels>(context, listen: false).data;
final currentChannel = Provider.of<Channels>(context, listen:
false).currentChannel;
final currentUser = Provider.of<User>(context, listen: false).currentUser;
final channelId = payload["channel_id"];
final message = payload["message"];
final userId = payload["user_id"];
final attachments = payload["attachments"] ?? [];
int index = data.indexWhere((e) => e["id"] == channelId);
if (userId != currentUser["id"]) {
if (index != -1) {
if (((tab == 0 || currentChannel["id"] != channelId) || !onFocusApp) ) {
Provider.of<Channels>(context, listen:
false).updateLastMessageReaded(channelId, payload["id"]);
}
checkInMention(att) {
final currentUser = Provider.of<User>(context, listen: false).currentUser;
final mentions = att.where((e) => e["type"] == "mention").toList();
bool check = false;
if (mentions.length > 0) {
for (var mention in mentions) {
final data = mention["data"];
if (data != null) {
final indexAll = data.indexWhere((e) => (e["type"] == "user" && e["name"]
== "all") || e["type"] == "all");
final indexUser = data.indexWhere((e) => e["type"] == "user" &&
e["value"] == currentUser["id"]);
return check;
}
getTitleNotification(DirectModel dm){
// hoi thoai 1-1, nameDm la ten nguoi gui
// trong cac truong hop con lai thi tra ve ten hoac list danh sach thanh vien
if (dm.user.length == 2){
var yourId = Provider.of<Auth>(context, listen: false).userId;
var otherIndex = dm.user.indexWhere((element) => element["user_id"] !=
yourId);
if (otherIndex == -1) return dm.name != "" ? dm.name : dm.user.map((e) =>
e["full_name"]).join(", ");
return dm.user[otherIndex]["full_name"];
}
return dm.name != "" ? dm.name : dm.user.map((e) => e["full_name"]).join(", ");
}
checkNewBadgeCount() {
final channels = Provider.of<Channels>(context, listen: false).data;
final data = Provider.of<DirectMessage>(context, listen: false).data;
final currentWorkspace = Provider.of<Workspaces>(context, listen:
false).currentWorkspace;
final dataThreads = Provider.of<Threads>(context, listen: false).dataThreads;
num count = 0;
if (indexThread != -1) {
final threads = dataThreads[indexThread]["threads"];
return count;
}
await channel.push(
event: "join_channel",
payload: {"channel_id": channelId, "workspace_id": workspaceId}
);
if (payload["channel_thread_id"] != null) {
final data = Provider.of<Messages>(context, listen: false).data;
int index = data.indexWhere((e) => e["channelId"] == channelId);
if (index != -1) {
List messages = data[index]["messages"];
int indexMessage = messages.indexWhere((e) => e["id"] ==
payload["channel_thread_id"]);
if (indexMessage != -1) {
final message = messages[indexMessage];
Map parentMessage = {
"id": message["id"],
"message": message["message"],
"avatarUrl": message["avatar_url"],
"lastEditedAt": message["last_edited_at"],
"isUnsent": message["is_unsent"],
"fullName": message["full_name"],
"insertedAt": message["inserted_at"],
"attachments": message["attachments"],
"userId": message["user_id"],
"workspaceId": workspaceId,
"channelId": channelId,
"isChannel": true
};
Utils.updateBadge(context);
}
if (index != -1) {
DirectModel directMessage = list[index];
await channel.push(event: "join_direct", payload: {"direct_id":
conversationId});
Provider.of<Workspaces>(context, listen: false).tab = 0;
Provider.of<Workspaces>(context, listen: false).changeToMessageView(true);
await Provider.of<DirectMessage>(context, listen:
false).onChangeSelectedFriend(false);
await Provider.of<DirectMessage>(context, listen:
false).setSelectedDM(directMessage, token);
await Provider.of<DirectMessage>(context, listen:
false).getMessageFromApiDown(conversationId, true, token, userId);
FocusInputStream.instance.focusToMessage();
if (payload["parent_id"] != null) {
final data = Provider.of<DirectMessage>(context, listen:
false).dataDMMessages;
final index = data.indexWhere((e) => e["conversation_id"] ==
conversationId);
if (index != -1) {
var messageOnIsar = await
MessageConversationServices.getListMessageById(payload["parent_id"],
conversationId);
if (messageOnIsar != null) {
final message = messageOnIsar;
final directMessageSelected = Provider.of<DirectMessage>(context,
listen: false).directMessageSelected;
List users = directMessageSelected.user;
final indexUser = users.indexWhere((e) => e["user_id"] ==
message["user_id"]);
if (indexUser != -1) {
final user = users[indexUser];
Map parentMessage = {
"id": message["id"],
"message": message["message"],
"avatarUrl": user["avatar_url"],
"insertedAt": message["time_create"],
"fullName": user["full_name"],
"attachments": message["attachments"],
"userId": message["user_id"],
"conversationId": conversationId,
"isChannel": false,
};
Provider.of<Channels>(context, listen:
false).openChannelSetting(false);
await Provider.of<Messages>(context, listen:
false).openThreadMessage(true, parentMessage);
FocusInputStream.instance.focusToThread();
}
}
}
}
}
Utils.updateBadge(context);
}
@override
Widget build(BuildContext context) {
return Container(
child: Container(width: 0),
);
}
}