Move around client holder
This commit is contained in:
parent
b1a945a501
commit
e76d5390c0
13 changed files with 143 additions and 130 deletions
39
lib/client_holder.dart
Normal file
39
lib/client_holder.dart
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
|
||||||
|
import 'package:contacts_plus_plus/clients/api_client.dart';
|
||||||
|
import 'package:contacts_plus_plus/clients/messaging_client.dart';
|
||||||
|
import 'package:contacts_plus_plus/clients/notification_client.dart';
|
||||||
|
import 'package:contacts_plus_plus/clients/settings_client.dart';
|
||||||
|
import 'package:contacts_plus_plus/models/authentication_data.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
|
class ClientHolder extends InheritedWidget {
|
||||||
|
final ApiClient apiClient;
|
||||||
|
final SettingsClient settingsClient;
|
||||||
|
late final MessagingClient messagingClient;
|
||||||
|
final NotificationClient notificationClient = NotificationClient();
|
||||||
|
|
||||||
|
ClientHolder({
|
||||||
|
super.key,
|
||||||
|
required AuthenticationData authenticationData,
|
||||||
|
required this.settingsClient,
|
||||||
|
required super.child
|
||||||
|
}) : apiClient = ApiClient(authenticationData: authenticationData) {
|
||||||
|
messagingClient = MessagingClient(apiClient: apiClient, notificationClient: notificationClient);
|
||||||
|
}
|
||||||
|
|
||||||
|
static ClientHolder? maybeOf(BuildContext context) {
|
||||||
|
return context.dependOnInheritedWidgetOfExactType<ClientHolder>();
|
||||||
|
}
|
||||||
|
|
||||||
|
static ClientHolder of(BuildContext context) {
|
||||||
|
final ClientHolder? result = maybeOf(context);
|
||||||
|
assert(result != null, 'No AuthenticatedClient found in context');
|
||||||
|
return result!;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool updateShouldNotify(covariant ClientHolder oldWidget) =>
|
||||||
|
oldWidget.apiClient != apiClient
|
||||||
|
|| oldWidget.settingsClient != settingsClient
|
||||||
|
|| oldWidget.messagingClient != messagingClient;
|
||||||
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import 'package:contacts_plus_plus/clients/messaging_client.dart';
|
import 'package:contacts_plus_plus/clients/messaging_client.dart';
|
||||||
|
import 'package:contacts_plus_plus/clients/notification_client.dart';
|
||||||
import 'package:contacts_plus_plus/clients/settings_client.dart';
|
import 'package:contacts_plus_plus/clients/settings_client.dart';
|
||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
@ -145,35 +146,3 @@ class ApiClient {
|
||||||
return http.delete(buildFullUri(path), headers: headers);
|
return http.delete(buildFullUri(path), headers: headers);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class ClientHolder extends InheritedWidget {
|
|
||||||
final ApiClient apiClient;
|
|
||||||
final SettingsClient settingsClient;
|
|
||||||
late final MessagingClient messagingClient;
|
|
||||||
final NotificationClient notificationClient = NotificationClient();
|
|
||||||
|
|
||||||
ClientHolder({
|
|
||||||
super.key,
|
|
||||||
required AuthenticationData authenticationData,
|
|
||||||
required this.settingsClient,
|
|
||||||
required super.child
|
|
||||||
}) : apiClient = ApiClient(authenticationData: authenticationData) {
|
|
||||||
messagingClient = MessagingClient(apiClient: apiClient, notificationClient: notificationClient);
|
|
||||||
}
|
|
||||||
|
|
||||||
static ClientHolder? maybeOf(BuildContext context) {
|
|
||||||
return context.dependOnInheritedWidgetOfExactType<ClientHolder>();
|
|
||||||
}
|
|
||||||
|
|
||||||
static ClientHolder of(BuildContext context) {
|
|
||||||
final ClientHolder? result = maybeOf(context);
|
|
||||||
assert(result != null, 'No AuthenticatedClient found in context');
|
|
||||||
return result!;
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
bool updateShouldNotify(covariant ClientHolder oldWidget) =>
|
|
||||||
oldWidget.apiClient != apiClient
|
|
||||||
|| oldWidget.settingsClient != settingsClient
|
|
||||||
|| oldWidget.messagingClient != messagingClient;
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,13 +1,10 @@
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
import 'package:contacts_plus_plus/apis/message_api.dart';
|
import 'package:contacts_plus_plus/apis/message_api.dart';
|
||||||
import 'package:contacts_plus_plus/auxiliary.dart';
|
import 'package:contacts_plus_plus/clients/notification_client.dart';
|
||||||
import 'package:contacts_plus_plus/models/authentication_data.dart';
|
import 'package:contacts_plus_plus/models/authentication_data.dart';
|
||||||
import 'package:contacts_plus_plus/models/friend.dart';
|
import 'package:contacts_plus_plus/models/friend.dart';
|
||||||
import 'package:contacts_plus_plus/models/session.dart';
|
|
||||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart' as fln;
|
|
||||||
import 'package:http/http.dart' as http;
|
import 'package:http/http.dart' as http;
|
||||||
import 'package:collection/collection.dart';
|
|
||||||
|
|
||||||
import 'package:contacts_plus_plus/clients/api_client.dart';
|
import 'package:contacts_plus_plus/clients/api_client.dart';
|
||||||
import 'package:contacts_plus_plus/config.dart';
|
import 'package:contacts_plus_plus/config.dart';
|
||||||
|
@ -283,89 +280,3 @@ class MessagingClient {
|
||||||
_sendData(data);
|
_sendData(data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class NotificationChannel {
|
|
||||||
final String id;
|
|
||||||
final String name;
|
|
||||||
final String description;
|
|
||||||
|
|
||||||
const NotificationChannel({required this.name, required this.id, required this.description});
|
|
||||||
}
|
|
||||||
|
|
||||||
class NotificationClient {
|
|
||||||
static const NotificationChannel _messageChannel = NotificationChannel(
|
|
||||||
id: "messages",
|
|
||||||
name: "Messages",
|
|
||||||
description: "Messages received from your friends",
|
|
||||||
);
|
|
||||||
|
|
||||||
final fln.FlutterLocalNotificationsPlugin _notifier = fln.FlutterLocalNotificationsPlugin()
|
|
||||||
..initialize(
|
|
||||||
const fln.InitializationSettings(
|
|
||||||
android: fln.AndroidInitializationSettings("ic_notification"),
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
Future<void> showUnreadMessagesNotification(Iterable<Message> messages) async {
|
|
||||||
if (messages.isEmpty) return;
|
|
||||||
|
|
||||||
final bySender = groupBy(messages, (p0) => p0.senderId);
|
|
||||||
|
|
||||||
for (final entry in bySender.entries) {
|
|
||||||
final uname = entry.key.stripUid();
|
|
||||||
await _notifier.show(
|
|
||||||
uname.hashCode,
|
|
||||||
null,
|
|
||||||
null,
|
|
||||||
fln.NotificationDetails(android: fln.AndroidNotificationDetails(
|
|
||||||
_messageChannel.id,
|
|
||||||
_messageChannel.name,
|
|
||||||
channelDescription: _messageChannel.description,
|
|
||||||
importance: fln.Importance.high,
|
|
||||||
priority: fln.Priority.max,
|
|
||||||
styleInformation: fln.MessagingStyleInformation(
|
|
||||||
fln.Person(
|
|
||||||
name: uname,
|
|
||||||
bot: false,
|
|
||||||
),
|
|
||||||
groupConversation: false,
|
|
||||||
messages: entry.value.map((message) {
|
|
||||||
String content;
|
|
||||||
switch (message.type) {
|
|
||||||
case MessageType.unknown:
|
|
||||||
content = "Unknown Message Type";
|
|
||||||
break;
|
|
||||||
case MessageType.text:
|
|
||||||
content = message.content;
|
|
||||||
break;
|
|
||||||
case MessageType.sound:
|
|
||||||
content = "Audio Message";
|
|
||||||
break;
|
|
||||||
case MessageType.sessionInvite:
|
|
||||||
try {
|
|
||||||
final session = Session.fromMap(jsonDecode(message.content));
|
|
||||||
content = "Session Invite to ${session.name}";
|
|
||||||
} catch (e) {
|
|
||||||
content = "Session Invite";
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case MessageType.object:
|
|
||||||
content = "Asset";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return fln.Message(
|
|
||||||
content,
|
|
||||||
message.sendTime,
|
|
||||||
fln.Person(
|
|
||||||
name: uname,
|
|
||||||
bot: false,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}).toList(),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
93
lib/clients/notification_client.dart
Normal file
93
lib/clients/notification_client.dart
Normal file
|
@ -0,0 +1,93 @@
|
||||||
|
import 'dart:convert';
|
||||||
|
|
||||||
|
import 'package:contacts_plus_plus/auxiliary.dart';
|
||||||
|
import 'package:contacts_plus_plus/models/message.dart';
|
||||||
|
import 'package:contacts_plus_plus/models/session.dart';
|
||||||
|
import 'package:flutter_local_notifications/flutter_local_notifications.dart' as fln;
|
||||||
|
import 'package:collection/collection.dart';
|
||||||
|
|
||||||
|
class NotificationChannel {
|
||||||
|
final String id;
|
||||||
|
final String name;
|
||||||
|
final String description;
|
||||||
|
|
||||||
|
const NotificationChannel({required this.name, required this.id, required this.description});
|
||||||
|
}
|
||||||
|
|
||||||
|
class NotificationClient {
|
||||||
|
static const NotificationChannel _messageChannel = NotificationChannel(
|
||||||
|
id: "messages",
|
||||||
|
name: "Messages",
|
||||||
|
description: "Messages received from your friends",
|
||||||
|
);
|
||||||
|
|
||||||
|
final fln.FlutterLocalNotificationsPlugin _notifier = fln.FlutterLocalNotificationsPlugin()
|
||||||
|
..initialize(
|
||||||
|
const fln.InitializationSettings(
|
||||||
|
android: fln.AndroidInitializationSettings("ic_notification"),
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
Future<void> showUnreadMessagesNotification(Iterable<Message> messages) async {
|
||||||
|
if (messages.isEmpty) return;
|
||||||
|
|
||||||
|
final bySender = groupBy(messages, (p0) => p0.senderId);
|
||||||
|
|
||||||
|
for (final entry in bySender.entries) {
|
||||||
|
final uname = entry.key.stripUid();
|
||||||
|
await _notifier.show(
|
||||||
|
uname.hashCode,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
fln.NotificationDetails(android: fln.AndroidNotificationDetails(
|
||||||
|
_messageChannel.id,
|
||||||
|
_messageChannel.name,
|
||||||
|
channelDescription: _messageChannel.description,
|
||||||
|
importance: fln.Importance.high,
|
||||||
|
priority: fln.Priority.max,
|
||||||
|
styleInformation: fln.MessagingStyleInformation(
|
||||||
|
fln.Person(
|
||||||
|
name: uname,
|
||||||
|
bot: false,
|
||||||
|
),
|
||||||
|
groupConversation: false,
|
||||||
|
messages: entry.value.map((message) {
|
||||||
|
String content;
|
||||||
|
switch (message.type) {
|
||||||
|
case MessageType.unknown:
|
||||||
|
content = "Unknown Message Type";
|
||||||
|
break;
|
||||||
|
case MessageType.text:
|
||||||
|
content = message.content;
|
||||||
|
break;
|
||||||
|
case MessageType.sound:
|
||||||
|
content = "Audio Message";
|
||||||
|
break;
|
||||||
|
case MessageType.sessionInvite:
|
||||||
|
try {
|
||||||
|
final session = Session.fromMap(jsonDecode(message.content));
|
||||||
|
content = "Session Invite to ${session.name}";
|
||||||
|
} catch (e) {
|
||||||
|
content = "Session Invite";
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case MessageType.object:
|
||||||
|
content = "Asset";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return fln.Message(
|
||||||
|
content,
|
||||||
|
message.sendTime,
|
||||||
|
fln.Person(
|
||||||
|
name: uname,
|
||||||
|
bot: false,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}).toList(),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
import 'dart:developer';
|
import 'dart:developer';
|
||||||
import 'dart:io' show Platform;
|
import 'dart:io' show Platform;
|
||||||
|
|
||||||
|
import 'package:contacts_plus_plus/client_holder.dart';
|
||||||
import 'package:contacts_plus_plus/clients/messaging_client.dart';
|
import 'package:contacts_plus_plus/clients/messaging_client.dart';
|
||||||
import 'package:contacts_plus_plus/clients/settings_client.dart';
|
import 'package:contacts_plus_plus/clients/settings_client.dart';
|
||||||
import 'package:contacts_plus_plus/widgets/friends_list.dart';
|
import 'package:contacts_plus_plus/widgets/friends_list.dart';
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
|
||||||
import 'package:contacts_plus_plus/clients/api_client.dart';
|
import 'package:contacts_plus_plus/client_holder.dart';
|
||||||
import 'package:contacts_plus_plus/apis/friend_api.dart';
|
import 'package:contacts_plus_plus/apis/friend_api.dart';
|
||||||
import 'package:contacts_plus_plus/apis/message_api.dart';
|
import 'package:contacts_plus_plus/apis/message_api.dart';
|
||||||
import 'package:contacts_plus_plus/models/friend.dart';
|
import 'package:contacts_plus_plus/models/friend.dart';
|
||||||
|
|
|
@ -2,6 +2,7 @@ import 'package:contacts_plus_plus/clients/api_client.dart';
|
||||||
import 'package:contacts_plus_plus/models/authentication_data.dart';
|
import 'package:contacts_plus_plus/models/authentication_data.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||||
|
import 'package:contacts_plus_plus/client_holder.dart';
|
||||||
|
|
||||||
class LoginScreen extends StatefulWidget {
|
class LoginScreen extends StatefulWidget {
|
||||||
const LoginScreen({this.onLoginSuccessful, this.cachedUsername, super.key});
|
const LoginScreen({this.onLoginSuccessful, this.cachedUsername, super.key});
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import 'dart:io' show Platform;
|
import 'dart:io' show Platform;
|
||||||
|
|
||||||
import 'package:contacts_plus_plus/clients/api_client.dart';
|
import 'package:contacts_plus_plus/client_holder.dart';
|
||||||
import 'package:contacts_plus_plus/auxiliary.dart';
|
import 'package:contacts_plus_plus/auxiliary.dart';
|
||||||
import 'package:contacts_plus_plus/models/message.dart';
|
import 'package:contacts_plus_plus/models/message.dart';
|
||||||
import 'package:contacts_plus_plus/widgets/messages_list.dart';
|
import 'package:contacts_plus_plus/widgets/messages_list.dart';
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
|
|
||||||
import 'package:contacts_plus_plus/clients/api_client.dart';
|
import 'package:contacts_plus_plus/client_holder.dart';
|
||||||
import 'package:contacts_plus_plus/auxiliary.dart';
|
import 'package:contacts_plus_plus/auxiliary.dart';
|
||||||
import 'package:contacts_plus_plus/models/message.dart';
|
import 'package:contacts_plus_plus/models/message.dart';
|
||||||
import 'package:contacts_plus_plus/models/session.dart';
|
import 'package:contacts_plus_plus/models/session.dart';
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
|
||||||
import 'package:cached_network_image/cached_network_image.dart';
|
import 'package:cached_network_image/cached_network_image.dart';
|
||||||
import 'package:contacts_plus_plus/clients/api_client.dart';
|
import 'package:contacts_plus_plus/client_holder.dart';
|
||||||
import 'package:contacts_plus_plus/auxiliary.dart';
|
import 'package:contacts_plus_plus/auxiliary.dart';
|
||||||
import 'package:contacts_plus_plus/models/friend.dart';
|
import 'package:contacts_plus_plus/models/friend.dart';
|
||||||
import 'package:contacts_plus_plus/models/message.dart';
|
import 'package:contacts_plus_plus/models/message.dart';
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import 'package:contacts_plus_plus/clients/api_client.dart';
|
import 'package:contacts_plus_plus/client_holder.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:url_launcher/url_launcher.dart';
|
import 'package:url_launcher/url_launcher.dart';
|
||||||
import 'package:workmanager/workmanager.dart';
|
import 'package:workmanager/workmanager.dart';
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import 'package:contacts_plus_plus/apis/user_api.dart';
|
import 'package:contacts_plus_plus/apis/user_api.dart';
|
||||||
import 'package:contacts_plus_plus/auxiliary.dart';
|
import 'package:contacts_plus_plus/auxiliary.dart';
|
||||||
import 'package:contacts_plus_plus/clients/api_client.dart';
|
import 'package:contacts_plus_plus/client_holder.dart';
|
||||||
import 'package:contacts_plus_plus/models/user.dart';
|
import 'package:contacts_plus_plus/models/user.dart';
|
||||||
import 'package:contacts_plus_plus/widgets/generic_avatar.dart';
|
import 'package:contacts_plus_plus/widgets/generic_avatar.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
|
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
|
||||||
import 'package:contacts_plus_plus/apis/user_api.dart';
|
import 'package:contacts_plus_plus/apis/user_api.dart';
|
||||||
import 'package:contacts_plus_plus/clients/api_client.dart';
|
import 'package:contacts_plus_plus/client_holder.dart';
|
||||||
import 'package:contacts_plus_plus/models/user.dart';
|
import 'package:contacts_plus_plus/models/user.dart';
|
||||||
import 'package:contacts_plus_plus/widgets/default_error_widget.dart';
|
import 'package:contacts_plus_plus/widgets/default_error_widget.dart';
|
||||||
import 'package:contacts_plus_plus/widgets/user_list_tile.dart';
|
import 'package:contacts_plus_plus/widgets/user_list_tile.dart';
|
||||||
|
|
Loading…
Reference in a new issue