prepare FOR notifications

This commit is contained in:
zoe 2022-08-15 20:31:44 +02:00
parent 4a712ffed4
commit 9e399866a6
5 changed files with 70 additions and 36 deletions

View File

@ -1 +1 @@
class NotificationModel {}

View File

@ -1,42 +1,63 @@
import 'dart:async';
import 'package:loris/business_logic/settings.dart';
import 'package:web_socket_channel/io.dart';
import '../global.dart' as global;
IOWebSocketChannel? channel;
bool connected = false;
Map<String, IOWebSocketChannel> map = {};
Map<String, Map<String, StreamController>> map = {};
Future<void> reloadWebsockets() async {
closeSockets();
for (int i = 0; i < global.settings!.identities.length; i++) {
final idName = global.settings!.identities.keys.toList()[i];
final id = global.settings!.identities[idName]!;
var query = {
"stream": "user",
"access_token": id.token,
Map<String, StreamController> idMap = {
"home": getStreamController(id, StreamType.user),
"local": getStreamController(id, StreamType.publicLocal),
"public": getStreamController(id, StreamType.publicLocal),
};
var host = id.webSocketUrl;
host = host.replaceFirstMapped("wss://", (match) => "");
host = host.replaceFirstMapped("ws://", (match) => "");
final uri = Uri(
scheme: "wss",
host: host,
path: "/api/v1/streaming",
queryParameters: query);
map.addAll(
{
idName: IOWebSocketChannel.connect(
uri,
headers: global.defaultHeaders,
),
},
{idName: idMap},
);
}
}
void closeSockets() {
for (int i = 0; i < map.length; i++) {
String key = map.keys.toList()[i];
map[key]!.sink.close();
enum StreamType {
user,
public,
publicLocal,
}
extension StreamTypeExtension on StreamType {
String get name {
switch (this) {
case StreamType.user:
return "user";
case StreamType.public:
return "public";
case StreamType.publicLocal:
return "public:local";
}
}
}
StreamController getStreamController(AccountSettings id, StreamType type) {
var query = {
"stream": type.name,
"access_token": id.token,
};
var host = id.webSocketUrl;
host = host.replaceFirstMapped("wss://", (match) => "");
host = host.replaceFirstMapped("ws://", (match) => "");
const scheme = "wss";
const path = "/api/v1/streaming";
final uri =
Uri(scheme: scheme, host: host, path: path, queryParameters: query);
final controller = StreamController.broadcast();
final socket = IOWebSocketChannel.connect(uri);
controller.addStream(socket.stream);
return controller;
}

View File

@ -1,5 +1,24 @@
import 'package:flutter/material.dart';
import '../../business_logic/websocket.dart' as websocket;
Widget notifications(context) {
return const Center(child: Text("Notifications"));
class Notifications extends StatefulWidget {
const Notifications({Key? key}) : super(key: key);
@override
State<Notifications> createState() => _NotificationsState();
}
class _NotificationsState extends State<Notifications> {
List<Widget> notifs = [];
List subscriptions = [];
@override
Widget build(BuildContext context) {
for (int i = 0; i < websocket.map.length; i++) {
final keyI = websocket.map.keys.toList()[i];
subscriptions.add(websocket.map[keyI]!["home"]!.stream.listen((event) {
print(event);
}));
}
return Text("Notifs");
}
}

View File

@ -10,7 +10,7 @@ class Timeline extends StatefulWidget {
State<Timeline> createState() => TimelineState();
}
class TimelineState extends State<Timeline> with AutomaticKeepAliveClientMixin {
class TimelineState extends State<Timeline> {
String? oldestId;
final controller = ScrollController();
List<Widget> children = [];
@ -21,6 +21,7 @@ class TimelineState extends State<Timeline> with AutomaticKeepAliveClientMixin {
@override
void initState() {
print("init");
super.initState();
children.add(const LoadingBox());
fetchMore();
@ -87,7 +88,6 @@ class TimelineState extends State<Timeline> with AutomaticKeepAliveClientMixin {
@override
Widget build(BuildContext context) {
super.build(context);
List<DropdownMenuItem> identities = [];
// add identities to dropdown menu
for (int i = 0; i < global.settings!.identities.keys.length; i++) {
@ -220,9 +220,6 @@ class TimelineState extends State<Timeline> with AutomaticKeepAliveClientMixin {
await Future.delayed(const Duration(milliseconds: 500));
}
}
@override
bool get wantKeepAlive => true;
}
class LoadingBox extends StatelessWidget {

View File

@ -20,14 +20,11 @@ class _MainScaffoldState extends State<MainScaffold> {
@override
Widget build(BuildContext context) {
final GlobalKey<tl.TimelineState> tlKey = GlobalKey<tl.TimelineState>();
websocket.reloadWebsockets();
final screens = [
Timeline(
key: tlKey,
),
const Timeline(),
chat(context),
notifications(context),
const Notifications(),
settings(context),
];
final buttons = [