Skip to content

Commit

Permalink
完善远程同步
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoyaocz committed Aug 9, 2024
1 parent 3e7f732 commit 845404e
Show file tree
Hide file tree
Showing 4 changed files with 588 additions and 70 deletions.
18 changes: 18 additions & 0 deletions simple_live_app/lib/modules/remote_sync/remote_sync_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,24 @@ class RemoteSyncPage extends StatelessWidget {
return Scaffold(
appBar: AppBar(
title: const Text("数据同步"),
actions: [
Visibility(
visible: GetPlatform.isAndroid || GetPlatform.isIOS,
child: TextButton.icon(
onPressed: () async {
var result = await Get.toNamed(RoutePath.kSyncScan);
if (result == null || result.isEmpty) {
return;
}
if (result.length == 5) {
Get.toNamed(RoutePath.kRemoteSyncRoom, arguments: result);
} else {}
},
icon: const Icon(Remix.qr_scan_line),
label: const Text("扫一扫"),
),
),
],
),
body: ListView(
padding: AppStyle.edgeInsetsA12,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
import 'dart:async';
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:qr_flutter/qr_flutter.dart';
import 'package:simple_live_app/app/app_style.dart';
import 'package:simple_live_app/app/constant.dart';
import 'package:simple_live_app/app/controller/app_settings_controller.dart';
import 'package:simple_live_app/app/controller/base_controller.dart';
import 'package:simple_live_app/app/event_bus.dart';
import 'package:simple_live_app/app/log.dart';
import 'package:simple_live_app/app/utils.dart';
import 'package:simple_live_app/models/db/follow_user.dart';
import 'package:simple_live_app/models/db/history.dart';
import 'package:simple_live_app/services/bilibili_account_service.dart';
import 'package:simple_live_app/services/db_service.dart';
import 'package:simple_live_app/services/signalr_service.dart';

class RemoteSyncRoomController extends BaseController {
Expand All @@ -15,8 +28,16 @@ class RemoteSyncRoomController extends BaseController {
}
StreamSubscription? _roomDestroyedSubscription;
StreamSubscription? _roomUserUpdatedSubscription;
StreamSubscription? _onFavoriteSubscription;
StreamSubscription? _onHistorySubscription;
StreamSubscription? _onShieldWordSubscription;
StreamSubscription? _onBiliAccountSubscription;
var currentRoomId = "--".obs;
RxList<RoomUser> roomUsers = <RoomUser>[].obs;

Timer? _timer;
var countDown = 300.obs;

@override
void onInit() {
connect();
Expand All @@ -35,6 +56,47 @@ class RemoteSyncRoomController extends BaseController {
}
}

void createRoom() async {
try {
var resp = await signalR.createRoom();
if (resp.isSuccess) {
currentRoomId.value = resp.data!;
_startTimer();
} else {
SmartDialog.showToast(resp.message);
Get.back();
}
} catch (e) {
SmartDialog.showToast("创建房间失败");
Get.back();
}
}

void _startTimer() {
// 倒计时5分钟,自动关闭页面
countDown.value = 300;
_timer = Timer.periodic(const Duration(seconds: 1), (timer) {
countDown--;
if (countDown <= 0) {
timer.cancel();
Get.back();
}
});
}

void joinRoom(String roomId) async {
try {
var resp = await signalR.joinRoom(roomId);
if (!resp.isSuccess) {
SmartDialog.showToast(resp.message);
Get.back();
}
} catch (e) {
SmartDialog.showToast("加入房间失败");
Get.back();
}
}

void listenSignalR() {
_roomDestroyedSubscription = signalR.onRoomDestroyedStream.listen((roomId) {
SmartDialog.showToast("房间已被销毁");
Expand All @@ -45,36 +107,262 @@ class RemoteSyncRoomController extends BaseController {
this.roomUsers.assignAll(roomUsers);
},
);
_onFavoriteSubscription = signalR.onFavoriteStream.listen((data) {
onReceiveFavorite(data.$1, data.$2);
});
_onHistorySubscription = signalR.onHistoryStream.listen((data) {
onReceiveHistory(data.$1, data.$2);
});
_onShieldWordSubscription = signalR.onShieldWordStream.listen((data) {
onReceiveShieldWord(data.$1, data.$2);
});
_onBiliAccountSubscription = signalR.onBiliAccountStream.listen((data) {
onReceiveBiliAccount(data.$1, data.$2);
});
}

void createRoom() async {
void onReceiveFavorite(bool overlay, String data) async {
try {
var resp = await signalR.createRoom();
var jsonBody = json.decode(data);
if (overlay) {
await DBService.instance.followBox.clear();
}
for (var item in jsonBody) {
var user = FollowUser.fromJson(item);
await DBService.instance.followBox.put(user.id, user);
}
SmartDialog.showToast('已同步关注用户列表');
EventBus.instance.emit(Constant.kUpdateFollow, 0);
SmartDialog.showToast("已同步关注列表");
} catch (e) {
SmartDialog.showToast("同步失败:$e");
Log.logPrint(e);
}
}

void onReceiveHistory(bool overlay, String data) async {
try {
var jsonBody = json.decode(data);
if (overlay) {
await DBService.instance.historyBox.clear();
}
for (var item in jsonBody) {
var history = History.fromJson(item);
if (DBService.instance.historyBox.containsKey(history.id)) {
var old = DBService.instance.historyBox.get(history.id);
//如果本地的更新时间比较新,就不更新
if (old!.updateTime.isAfter(history.updateTime)) {
continue;
}
}
await DBService.instance.addOrUpdateHistory(history);
}
SmartDialog.showToast('已同步历史记录');
EventBus.instance.emit(Constant.kUpdateHistory, 0);
} catch (e) {
SmartDialog.showToast("同步失败:$e");
Log.logPrint(e);
}
}

void onReceiveShieldWord(bool overlay, String data) async {
try {
var jsonBody = json.decode(data);
if (overlay) {
AppSettingsController.instance.shieldList.clear();
}
for (var item in jsonBody) {
AppSettingsController.instance.shieldList.add(item);
}
SmartDialog.showToast('已同步屏蔽词');
} catch (e) {
SmartDialog.showToast("同步失败:$e");
Log.logPrint(e);
}
}

void onReceiveBiliAccount(bool overlay, String data) async {
try {
var jsonBody = json.decode(data);
var cookie = jsonBody['cookie'];
BiliBiliAccountService.instance.setCookie(cookie);
BiliBiliAccountService.instance.loadUserInfo();
SmartDialog.showToast('已同步哔哩哔哩账号');
} catch (e) {
SmartDialog.showToast("同步失败:$e");
Log.logPrint(e);
}
}

Future<bool> showOverlayDialog() async {
var overlay = await Utils.showAlertDialog(
"是否覆盖远端数据?",
title: "数据覆盖",
confirm: "覆盖",
cancel: "不覆盖",
);
return overlay;
}

void syncFollow() async {
try {
if (roomUsers.length <= 1) {
SmartDialog.showToast("无设备连接");
return;
}

var overlay = await showOverlayDialog();
SmartDialog.showLoading(msg: "发送中...");
var users = DBService.instance.getFollowList();
var data = json.encode(users.map((e) => e.toJson()).toList());

var resp = await signalR.sendContent(
roomName: currentRoomId.value,
action: "SendFavorite",
overlay: overlay,
content: data,
);
if (resp.isSuccess) {
currentRoomId.value = resp.data!;
SmartDialog.showToast("已发送关注列表");
} else {
SmartDialog.showToast(resp.message);
SmartDialog.showToast("发送失败:${resp.message}");
}
} catch (e) {
SmartDialog.showToast("创建房间失败");
SmartDialog.showToast("发送失败:$e");
Log.logPrint(e);
} finally {
SmartDialog.dismiss();
}
}

void joinRoom(String roomId) async {
void syncHistory() async {
try {
var resp = await signalR.joinRoom(roomId);
if (!resp.isSuccess) {
SmartDialog.showToast(resp.message);
if (roomUsers.length <= 1) {
SmartDialog.showToast("无设备连接");
return;
}
var overlay = await showOverlayDialog();
SmartDialog.showLoading(msg: "发送中...");
var histores = DBService.instance.getHistores();
var data = json.encode(histores.map((e) => e.toJson()).toList());
var resp = await signalR.sendContent(
roomName: currentRoomId.value,
action: "SendHistory",
overlay: overlay,
content: data,
);
if (resp.isSuccess) {
SmartDialog.showToast("已发送历史记录");
} else {
SmartDialog.showToast("发送失败:${resp.message}");
}
} catch (e) {
SmartDialog.showToast("加入房间失败");
SmartDialog.showToast("发送失败:$e");
Log.logPrint(e);
} finally {
SmartDialog.dismiss();
}
}

void syncBlockedWord() async {
try {
if (roomUsers.length <= 1) {
SmartDialog.showToast("无设备连接");
return;
}
var overlay = await showOverlayDialog();
SmartDialog.showLoading(msg: "发送中...");
var shieldList = AppSettingsController.instance.shieldList;
var data = json.encode(shieldList.toList());

var resp = await signalR.sendContent(
roomName: currentRoomId.value,
action: "SendShieldWord",
overlay: overlay,
content: data,
);
if (resp.isSuccess) {
SmartDialog.showToast("已发送屏蔽词");
} else {
SmartDialog.showToast("发送失败:${resp.message}");
}
} catch (e) {
SmartDialog.showToast("发送失败:$e");
Log.logPrint(e);
} finally {
SmartDialog.dismiss();
}
}

void syncBiliAccount() async {
try {
if (roomUsers.length <= 1) {
SmartDialog.showToast("无设备连接");
return;
}
if (!BiliBiliAccountService.instance.logined.value) {
SmartDialog.showToast("未登录哔哩哔哩");
return;
}
SmartDialog.showLoading(msg: "发送中...");

var resp = await signalR.sendContent(
roomName: currentRoomId.value,
action: "SendBiliAccount",
overlay: true,
content: json.encode({
"cookie": BiliBiliAccountService.instance.cookie,
}),
);
if (resp.isSuccess) {
SmartDialog.showToast("已发送哔哩哔哩账号");
} else {
SmartDialog.showToast("发送失败:${resp.message}");
}
} catch (e) {
SmartDialog.showToast("同步失败:$e");
Log.logPrint(e);
} finally {
SmartDialog.dismiss();
}
}

void showQRInfo() {
Utils.showBottomSheet(
title: "房间信息",
child: Column(
children: [
QrImageView(
data: currentRoomId.value,
version: QrVersions.auto,
backgroundColor: Colors.white,
padding: AppStyle.edgeInsetsA12,
size: 200,
),
AppStyle.vGap24,
Text(
currentRoomId.value,
textAlign: TextAlign.center,
style: Get.textTheme.titleLarge,
),
const Text(
"请使用其他Simple Live客户端扫描上方二维码\n建立连接后可选择需要同步的数据",
textAlign: TextAlign.center,
),
],
),
);
}

@override
void onClose() {
_timer?.cancel();
_roomDestroyedSubscription?.cancel();
_roomUserUpdatedSubscription?.cancel();
_onFavoriteSubscription?.cancel();
_onHistorySubscription?.cancel();
_onShieldWordSubscription?.cancel();
_onBiliAccountSubscription?.cancel();
signalR.dispose();
super.onClose();
}
Expand Down
Loading

0 comments on commit 845404e

Please sign in to comment.