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

Abtract backend stuff #2711

Open
wants to merge 3 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 67 additions & 66 deletions lib/app/layouts/chat_creator/chat_creator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class ChatCreatorState extends OptimizedState<ChatCreator> {

final messageNode = FocusNode();

bool canCreateGroupChats = ss.canCreateGroupChatSync();
bool canCreateGroupChats = backend.canCreateGroupChats;

@override
void initState() {
Expand Down Expand Up @@ -147,8 +147,7 @@ class ChatCreatorState extends OptimizedState<ChatCreator> {
void addSelected(SelectedContact c) async {
selectedContacts.add(c);
try {
final response = await http.handleiMessageState(c.address);
c.iMessage.value = response.data["data"]["available"];
c.iMessage.value = await backend.handleiMessageState(c.address);
} catch (_) {}
addressController.text = "";
findExistingChat();
Expand Down Expand Up @@ -450,59 +449,60 @@ class ChatCreatorState extends OptimizedState<ChatCreator> {
],
),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 15.0).add(const EdgeInsets.only(bottom: 5.0)),
child: ToggleButtons(
constraints: BoxConstraints(minWidth: (ns.width(context) - 35) / 2),
fillColor: context.theme.colorScheme.bubble(context, iMessage).withOpacity(0.2),
splashColor: context.theme.colorScheme.bubble(context, iMessage).withOpacity(0.2),
children: [
const Row(
children: [
Padding(
padding: EdgeInsets.all(8.0),
child: Text("iMessage"),
),
Icon(CupertinoIcons.chat_bubble, size: 16),
],
),
const Row(
children: [
Padding(
padding: EdgeInsets.all(8.0),
child: Text("SMS Forwarding"),
),
Icon(Icons.messenger_outline, size: 16),
],
),
],
borderRadius: BorderRadius.circular(20),
selectedBorderColor: context.theme.colorScheme.bubble(context, iMessage),
selectedColor: context.theme.colorScheme.bubble(context, iMessage),
isSelected: [iMessage, sms],
onPressed: (index) async {
selectedContacts.clear();
addressController.text = "";
if (index == 0) {
setState(() {
iMessage = true;
sms = false;
filteredChats = List<Chat>.from(existingChats.where((e) => e.isIMessage));
});
await cm.setAllInactive();
fakeController.value = null;
} else {
setState(() {
iMessage = false;
sms = true;
filteredChats = List<Chat>.from(existingChats.where((e) => !e.isIMessage));
});
await cm.setAllInactive();
fakeController.value = null;
}
},
if (backend.supportsSmsForwarding)
Padding(
padding: const EdgeInsets.symmetric(horizontal: 15.0).add(const EdgeInsets.only(bottom: 5.0)),
child: ToggleButtons(
constraints: BoxConstraints(minWidth: (ns.width(context) - 35) / 2),
fillColor: context.theme.colorScheme.bubble(context, iMessage).withOpacity(0.2),
splashColor: context.theme.colorScheme.bubble(context, iMessage).withOpacity(0.2),
children: [
const Row(
children: [
Padding(
padding: EdgeInsets.all(8.0),
child: Text("iMessage"),
),
Icon(CupertinoIcons.chat_bubble, size: 16),
],
),
const Row(
children: [
Padding(
padding: EdgeInsets.all(8.0),
child: Text("SMS Forwarding"),
),
Icon(Icons.messenger_outline, size: 16),
],
),
],
borderRadius: BorderRadius.circular(20),
selectedBorderColor: context.theme.colorScheme.bubble(context, iMessage),
selectedColor: context.theme.colorScheme.bubble(context, iMessage),
isSelected: [iMessage, sms],
onPressed: (index) async {
selectedContacts.clear();
addressController.text = "";
if (index == 0) {
setState(() {
iMessage = true;
sms = false;
filteredChats = List<Chat>.from(existingChats.where((e) => e.isIMessage));
});
await cm.setAllInactive();
fakeController.value = null;
} else {
setState(() {
iMessage = false;
sms = true;
filteredChats = List<Chat>.from(existingChats.where((e) => !e.isIMessage));
});
await cm.setAllInactive();
fakeController.value = null;
}
},
),
tneotia marked this conversation as resolved.
Show resolved Hide resolved
),
),
Expanded(
child: Theme(
data: context.theme.copyWith(
Expand Down Expand Up @@ -668,13 +668,14 @@ class ChatCreatorState extends OptimizedState<ChatCreator> {
sendMessage: ({String? effect}) async {
addressOnSubmitted();
final chat = fakeController.value?.chat ?? await findExistingChat(checkDeleted: true, update: false);
bool existsOnServer = false;
if (chat != null) {
bool existsOnServer = true; // if there is no remote, we exist on the "server"
if (chat != null && backend.remoteService != null) {
// if we don't error, then the chat exists
try {
await http.singleChat(chat.guid);
existsOnServer = true;
} catch (_) {}
await backend.remoteService!.singleChat(chat.guid);
} catch (_) {
existsOnServer = false;
}
}
if (chat != null && existsOnServer) {
ns.pushAndRemoveUntil(
Expand Down Expand Up @@ -721,6 +722,7 @@ class ChatCreatorState extends OptimizedState<ChatCreator> {
final method = iMessage ? "iMessage" : "SMS";
showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return AlertDialog(
backgroundColor: context.theme.colorScheme.properSurface,
Expand All @@ -739,9 +741,8 @@ class ChatCreatorState extends OptimizedState<ChatCreator> {
),
);
});
http.createChat(participants, textController.text, method).then((response) async {
backend.createChat(participants, textController.text, method).then((newChat) async {
// Load the chat data and save it to the DB
Chat newChat = Chat.fromMap(response.data["data"]);
newChat = newChat.save();

// Fetch the newly saved chat data from the DB
Expand All @@ -756,8 +757,8 @@ class ChatCreatorState extends OptimizedState<ChatCreator> {
chats.updateChat(newChat);

// Fetch the last message for the chat and save it.
final messageRes = await http.chatMessages(newChat.guid, limit: 1);
if (messageRes.data["data"].length > 0) {
final messageRes = await backend.remoteService?.chatMessages(newChat.guid, limit: 1);
if (messageRes != null && messageRes.data["data"].length > 0) {
tneotia marked this conversation as resolved.
Show resolved Hide resolved
final messages = (messageRes.data["data"] as List<dynamic>).map((e) => Message.fromMap(e)).toList();
await Chat.bulkSyncMessages(newChat, messages);
}
Expand All @@ -771,7 +772,7 @@ class ChatCreatorState extends OptimizedState<ChatCreator> {
createCompleter?.complete();

// Navigate to the new chat
Navigator.of(context).pop();
Get.back(closeOverlays: true);
ns.pushAndRemoveUntil(
Get.context!,
ConversationView(chat: newChat),
Expand All @@ -787,7 +788,7 @@ class ChatCreatorState extends OptimizedState<ChatCreator> {
),
);
}).catchError((error) {
Navigator.of(context).pop();
Get.back(closeOverlays: true);
showDialog(
barrierDismissible: false,
context: context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ class _ConversationDetailsState extends OptimizedState<ConversationDetails> with
);
}, childCount: clippedParticipants.length + 2),
),
if (chat.participants.length > 2 && ss.settings.enablePrivateAPI.value && ss.serverDetailsSync().item4 >= 226)
if (chat.participants.length > 2 && ss.settings.enablePrivateAPI.value && backend.canLeaveChat)
SliverToBoxAdapter(
child: Builder(
builder: (context) {
Expand Down Expand Up @@ -321,8 +321,8 @@ class _ConversationDetailsState extends OptimizedState<ConversationDetails> with
);
}
);
final response = await http.leaveChat(chat.guid);
if (response.statusCode == 200) {
final response = await backend.leaveChat(chat);
if (response) {
Get.back();
showSnackbar("Notice", "Left chat successfully!");
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ void showAddParticipant(BuildContext context, Chat chat) {
);
}
);
final response = await http.chatParticipant("add", chat.guid, participantController.text);
if (response.statusCode == 200) {
final response = await backend.chatParticipant(ParticipantOp.Add, chat, participantController.text);
if (response) {
Get.back();
Get.back();
showSnackbar("Notice", "Added ${participantController.text} successfully!");
Expand Down
4 changes: 2 additions & 2 deletions lib/app/layouts/conversation_details/dialogs/change_name.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ void showChangeName(Chat chat, String method, BuildContext context) {
);
}
);
final response = await http.updateChat(chat.guid, controller.text);
if (response.statusCode == 200) {
final response = await backend.renameChat(chat, controller.text);
if (response) {
Get.back();
Get.back();
chat.changeName(controller.text);
Expand Down
24 changes: 10 additions & 14 deletions lib/app/layouts/conversation_details/widgets/chat_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class _ChatInfoState extends OptimizedState<ChatInfo> {
children: [
if (ss.settings.enablePrivateAPI.value && chat.isIMessage)
Text(
"Local - Changes only apply to this device.\nPrivate API - Changes will apply to everyone's devices.",
"Local - Changes only apply to this device.\nEveryone - Changes will apply to everyone's devices.",
style: context.theme.textTheme.bodyLarge
),
],
Expand All @@ -61,7 +61,7 @@ class _ChatInfoState extends OptimizedState<ChatInfo> {
),
TextButton(
child: Text(
"Private API",
"Everyone",
style: context.theme.textTheme.bodyLarge!.copyWith(color: context.theme.colorScheme.primary)
),
onPressed: () {
Expand All @@ -88,7 +88,7 @@ class _ChatInfoState extends OptimizedState<ChatInfo> {
if (result != null) {
chat.customAvatarPath = result;
}
if (papi && ss.settings.enablePrivateAPI.value && result != null && (await ss.isMinBigSur) && ss.serverDetailsSync().item4 >= 226) {
if (papi && ss.settings.enablePrivateAPI.value && result != null && await backend.canUploadGroupPhotos()) {
showDialog(
context: context,
builder: (BuildContext context) {
Expand All @@ -110,8 +110,8 @@ class _ChatInfoState extends OptimizedState<ChatInfo> {
);
}
);
final response = await http.setChatIcon(chat.guid, chat.customAvatarPath!);
if (response.statusCode == 200) {
final response = await backend.setChatIcon(chat);
if (response) {
Get.back();
showSnackbar("Notice", "Updated group photo successfully!");
} else {
Expand All @@ -127,15 +127,11 @@ class _ChatInfoState extends OptimizedState<ChatInfo> {
papi = await showMethodDialog("Group Icon Deletion Method");
}
if (papi == null) return;
try {
File file = File(chat.customAvatarPath!);
file.delete();
} catch (_) {}
chat.customAvatarPath = null;
chat.removeProfilePhoto();
tneotia marked this conversation as resolved.
Show resolved Hide resolved
chat.save(updateCustomAvatarPath: true);
if (papi && ss.settings.enablePrivateAPI.value && (await ss.isMinBigSur) && ss.serverDetailsSync().item4 >= 226) {
final response = await http.deleteChatIcon(chat.guid);
if (response.statusCode == 200) {
if (papi && ss.settings.enablePrivateAPI.value && await backend.canUploadGroupPhotos()) {
final response = await backend.deleteChatIcon(chat);
if (response) {
showSnackbar("Notice", "Deleted group photo successfully!");
} else {
showSnackbar("Error", "Failed to delete group photo!");
Expand Down Expand Up @@ -328,7 +324,7 @@ class _ChatInfoState extends OptimizedState<ChatInfo> {
child: Row(
mainAxisAlignment: kIsWeb || kIsDesktop ? MainAxisAlignment.center : MainAxisAlignment.spaceBetween,
children: intersperse(const SizedBox(width: 5), [
if (!kIsWeb && !kIsDesktop && !chat.chatIdentifier!.startsWith("urn:biz")
if (!kIsWeb && !kIsDesktop && !(chat.chatIdentifier?.startsWith("urn:biz") ?? false)
TaeHagen marked this conversation as resolved.
Show resolved Hide resolved
&& ((chat.participants.first.contact?.phones.isNotEmpty ?? false)
|| !chat.participants.first.address.contains("@")))
Expanded(
Expand Down
Loading