Skip to content

Commit

Permalink
Feat: bt play
Browse files Browse the repository at this point in the history
  • Loading branch information
MiaoMint committed Aug 29, 2023
1 parent 460b1c4 commit 6fb4bc5
Show file tree
Hide file tree
Showing 25 changed files with 540 additions and 25 deletions.
35 changes: 35 additions & 0 deletions lib/api/bt_server.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import 'dart:typed_data';

import 'package:dio/dio.dart';

class BTServerApi {
static const baseApi = "http://localhost:3000";
static final dio = Dio(BaseOptions(
baseUrl: baseApi,
));
static Future<String> getVersion() async {
return (await dio.get<String>("/version")).data!;
}

static Future<String> addTorrent(Uint8List torrent) async {
return (await dio.post<Map<String, dynamic>>("/torrent",
data: torrent,
options: Options(
headers: {
"Content-Type": "application/x-bittorrent",
"Content-Length": torrent.length,
},
)))
.data!["infoHash"];
}

static Future<String> removeTorrent(String infoHash) async {
return (await dio.delete<String>("/torrent/$infoHash")).data!;
}

static Future<List<String>> getFileList(String infoHash) async {
final fileList = (await dio.get<Map<String, dynamic>>("/torrent/$infoHash"))
.data!['files'];
return List<String>.from(fileList);
}
}
2 changes: 1 addition & 1 deletion lib/models/extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ part 'extension.g.dart';

enum ExtensionType { manga, bangumi, fikushon }

enum ExtensionWatchBangumiType { hls, mp4 }
enum ExtensionWatchBangumiType { hls, mp4, torrent }

enum ExtensionLogLevel {
info,
Expand Down
1 change: 1 addition & 0 deletions lib/models/extension.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions lib/pages/bt_dialog/controller.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:get/get.dart';
import 'package:miru_app/pages/main/controller.dart';
import 'package:miru_app/utils/bt_server.dart';
import 'package:miru_app/widgets/messenger.dart';

class BTDialogController extends GetxController {
final isInstalled = false.obs;
final isDownloading = false.obs;
final progress = 0.0.obs;

final _mainController = Get.find<MainController>();
late final isRuning = _mainController.btServerisRunning;
late final version = _mainController.btServerVersion;

@override
void onInit() {
super.onInit();
SchedulerBinding.instance.addPostFrameCallback((_) async {
isInstalled.value = await BTServerUtils.isInstalled();
});
}

downloadOrUpgradeServer(BuildContext context) async {
isDownloading.value = true;
try {
await BTServerUtils.downloadLatestBTServer(
onReceiveProgress: (p0, p1) {
progress.value = p0 / p1;
print(progress.value);
},
);
} catch (e) {
context.mounted &&
showPlatformSnackbar(
context: context,
content: e.toString(),
);
} finally {
isDownloading.value = false;
}
isInstalled.value = await BTServerUtils.isInstalled();
}
}
102 changes: 102 additions & 0 deletions lib/pages/bt_dialog/view.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import 'package:flutter/material.dart';
import 'package:fluent_ui/fluent_ui.dart' as fluent;
import 'package:get/get.dart';
import 'package:miru_app/pages/bt_dialog/controller.dart';
import 'package:miru_app/utils/bt_server.dart';
import 'package:miru_app/widgets/button.dart';
import 'package:miru_app/widgets/platform_widget.dart';
import 'package:miru_app/widgets/progress.dart';

class BTDialog extends StatefulWidget {
const BTDialog({Key? key}) : super(key: key);

@override
State<BTDialog> createState() => _BTDialogState();
}

class _BTDialogState extends State<BTDialog> {
final BTDialogController c = Get.put(BTDialogController());

Widget _buildContent(BuildContext context) {
return Obx(() {
if (!c.isInstalled.value) {
return Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text("BT-Server is not installed"),
const SizedBox(height: 16),
if (c.isDownloading.value)
SizedBox(
width: double.infinity,
child: ProgressBar(value: c.progress.value),
)
else
PlatformFilledButton(
child: const Text("Download"),
onPressed: () {
c.downloadOrUpgradeServer(context);
},
),
],
);
}

return Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text("BT-Server is ${c.isRuning.value ? "running" : "stop"} "),
const SizedBox(height: 16),
if (c.isRuning.value)
Text("Version: ${c.version.value}")
else
PlatformFilledButton(
child: const Text("Start"),
onPressed: () {
BTServerUtils.startServer();
},
),
],
);
});
}

Widget _buildAndroid(BuildContext context) {
return AlertDialog(
title: const Text("BT-Server"),
content: _buildContent(context),
actions: [
PlatformTextButton(
child: const Text("Close"),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
}

Widget _buildDesktop(BuildContext context) {
return fluent.ContentDialog(
title: const Text("BT-Server"),
content: _buildContent(context),
actions: [
fluent.Button(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text("Close"),
),
],
);
}

@override
Widget build(BuildContext context) {
return PlatformBuildWidget(
androidBuilder: _buildAndroid,
desktopBuilder: _buildDesktop,
);
}
}
2 changes: 1 addition & 1 deletion lib/pages/detail/view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import 'package:miru_app/utils/layout.dart';
import 'package:miru_app/widgets/cache_network_image.dart';
import 'package:miru_app/widgets/card_tile.dart';
import 'package:miru_app/widgets/platform_widget.dart';
import 'package:miru_app/widgets/progress_ring.dart';
import 'package:miru_app/widgets/progress.dart';
import 'package:url_launcher/url_launcher.dart';

class DetailPage extends StatefulWidget {
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/extension_repo/view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'package:miru_app/pages/extension_repo/widgets/extension_card.dart';
import 'package:miru_app/utils/i18n.dart';
import 'package:miru_app/widgets/button.dart';
import 'package:miru_app/widgets/platform_widget.dart';
import 'package:miru_app/widgets/progress_ring.dart';
import 'package:miru_app/widgets/progress.dart';

class ExtensionRepoPage extends StatefulWidget {
const ExtensionRepoPage({Key? key}) : super(key: key);
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/extension_repo/widgets/extension_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:miru_app/utils/i18n.dart';
import 'package:miru_app/utils/miru_storage.dart';
import 'package:miru_app/widgets/cache_network_image.dart';
import 'package:miru_app/widgets/platform_widget.dart';
import 'package:miru_app/widgets/progress_ring.dart';
import 'package:miru_app/widgets/progress.dart';

class ExtensionCard extends StatefulWidget {
const ExtensionCard({
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/extension_settings/view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import 'package:miru_app/widgets/cache_network_image.dart';
import 'package:miru_app/widgets/card_tile.dart';
import 'package:miru_app/widgets/messenger.dart';
import 'package:miru_app/widgets/platform_widget.dart';
import 'package:miru_app/widgets/progress_ring.dart';
import 'package:miru_app/widgets/progress.dart';
import 'package:miru_app/widgets/settings_input_tile.dart';
import 'package:miru_app/widgets/settings_radios_tile.dart';
import 'package:miru_app/widgets/settings_switch_tile.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/home/pages/favorites_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:miru_app/utils/extension.dart';
import 'package:miru_app/utils/i18n.dart';
import 'package:miru_app/widgets/extension_item_card.dart';
import 'package:miru_app/widgets/platform_widget.dart';
import 'package:miru_app/widgets/progress_ring.dart';
import 'package:miru_app/widgets/progress.dart';

class FavoritesPage extends fluent.StatefulWidget {
const FavoritesPage({Key? key, required this.type}) : super(key: key);
Expand Down
17 changes: 17 additions & 0 deletions lib/pages/main/controller.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import 'package:fluent_ui/fluent_ui.dart';
import 'package:flutter/scheduler.dart';
import 'package:get/get.dart';
import 'package:miru_app/utils/bt_server.dart';

// 全局 Controller
class MainController extends GetxController {
final selectedTab = 0.obs;

Expand All @@ -17,4 +19,19 @@ class MainController extends GetxController {
actions.addAll(list);
});
}

@override
void onReady() {
super.onReady();
SchedulerBinding.instance.addPersistentFrameCallback((_) async {
// 判断 bt_server 是否已经安装
final isInstalled = await BTServerUtils.isInstalled();
if (isInstalled) {
BTServerUtils.checkServer();
}
});
}

final btServerVersion = "".obs;
final btServerisRunning = false.obs;
}
1 change: 1 addition & 0 deletions lib/pages/search/pages/search_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class _SearchExtensionPageState extends fluent.State<SearchExtensionPage> {
content: e.toString(),
severity: fluent.InfoBarSeverity.error,
);
rethrow;
} finally {
_isLoading = false;
if (mounted) {
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/search/widgets/search_all_tile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:miru_app/pages/search/controller.dart';
import 'package:miru_app/utils/i18n.dart';
import 'package:miru_app/widgets/extension_item_card.dart';
import 'package:miru_app/widgets/horizontal_list.dart';
import 'package:miru_app/widgets/progress_ring.dart';
import 'package:miru_app/widgets/progress.dart';

class SearchAllTile extends StatefulWidget {
const SearchAllTile({
Expand Down
29 changes: 29 additions & 0 deletions lib/pages/settings/view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import 'package:flutter_i18n/flutter_i18n.dart';
import 'package:get/get.dart';
import 'package:miru_app/api/tmdb.dart';
import 'package:miru_app/controller.dart';
import 'package:miru_app/pages/bt_dialog/view.dart';
import 'package:miru_app/pages/extension_repo/controller.dart';
import 'package:miru_app/pages/main/controller.dart';
import 'package:miru_app/pages/settings/controller.dart';
import 'package:miru_app/utils/bt_server.dart';
import 'package:miru_app/widgets/settings_input_tile.dart';
import 'package:miru_app/widgets/settings_radios_tile.dart';
import 'package:miru_app/widgets/settings_switch_tile.dart';
Expand Down Expand Up @@ -94,6 +97,32 @@ class _SettingsPageState extends State<SettingsPage> {
text: MiruStorage.getSetting(SettingKey.tmdbKay),
),
const SizedBox(height: 8),
SettingsTile(
title: 'BT-SERVER'.i18n,
buildSubtitle: () =>
Get.find<MainController>().btServerisRunning.value.toString(),
trailing: PlatformWidget(
androidWidget: TextButton(
onPressed: () {
showDialog(
context: context,
builder: (context) => const BTDialog(),
);
},
child: Text('管理'.i18n),
),
desktopWidget: fluent.FilledButton(
onPressed: () {
fluent.showDialog(
context: context,
builder: (context) => const BTDialog(),
);
},
child: Text('管理'.i18n),
),
),
),
const SizedBox(height: 8),
SettingsTile(
icon: const PlatformWidget(
androidWidget: Icon(Icons.update),
Expand Down
Loading

0 comments on commit 6fb4bc5

Please sign in to comment.