some work on background downloading
This commit is contained in:
parent
8b56745ccf
commit
b2c71b1403
|
|
@ -1,16 +1,72 @@
|
|||
import 'dart:io';
|
||||
|
||||
import 'package:extera_next/generated/l10n/l10n.dart';
|
||||
import 'package:extera_next/pages/download_manager/download_manager_view.dart';
|
||||
import 'package:extera_next/widgets/matrix.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
|
||||
class DownloadManager extends StatefulWidget {
|
||||
@override
|
||||
State<StatefulWidget> createState() => DownloadManagerController();
|
||||
class Download {
|
||||
final String url;
|
||||
final String name;
|
||||
final BuildContext context;
|
||||
late Future<Response<dynamic>> response;
|
||||
late int receivedBytes = 0;
|
||||
late int totalBytes = 1;
|
||||
late double progress = 0;
|
||||
Download(this.context, this.url, this.name);
|
||||
|
||||
void start() async {
|
||||
try {
|
||||
final mx = Matrix.of(context).client;
|
||||
final directory = await getExternalStorageDirectory();
|
||||
final downloadPath =
|
||||
directory != null ? "${directory.path}/Download" : null;
|
||||
|
||||
if (downloadPath != null) {
|
||||
// Create Dio instance
|
||||
final dio = Dio();
|
||||
// Progress status variables
|
||||
|
||||
// Download the file
|
||||
response = dio.download(
|
||||
url,
|
||||
"$downloadPath/$name",
|
||||
onReceiveProgress: (received, total) {
|
||||
receivedBytes = received;
|
||||
totalBytes = total;
|
||||
progress = (receivedBytes / totalBytes) * 100;
|
||||
print("Download progress: $progress%");
|
||||
},
|
||||
options: Options(
|
||||
responseType: ResponseType.bytes,
|
||||
headers: {'authorization': "Bearer ${mx.accessToken}"}),
|
||||
);
|
||||
print("Download completed and saved to $downloadPath/$name");
|
||||
}
|
||||
} catch (e) {
|
||||
print("Error during download: $e");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class DownloadManagerController extends State<DownloadManager> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
|
||||
}
|
||||
class DownloadManager extends StatefulWidget {
|
||||
final BuildContext context;
|
||||
|
||||
}
|
||||
const DownloadManager(this.context);
|
||||
|
||||
@override
|
||||
State<StatefulWidget> createState() =>
|
||||
Provider.of<DownloadManagerController>(this.context);
|
||||
}
|
||||
|
||||
class DownloadManagerController extends State<DownloadManager>
|
||||
with ChangeNotifier {
|
||||
@override
|
||||
Widget build(BuildContext context) => DownloadManagerView(this);
|
||||
|
||||
final List<Download> downloads = [];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,27 @@ class DownloadManagerView extends StatelessWidget {
|
|||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
|
||||
return Scaffold(
|
||||
body: ListView.builder(
|
||||
itemCount: controller.downloads.length,
|
||||
itemBuilder: (context, index) {
|
||||
final download = controller.downloads[index];
|
||||
return ListTile(
|
||||
title: Text(download.name),
|
||||
subtitle: LinearProgressIndicator(
|
||||
value: download.progress,
|
||||
backgroundColor: Colors.grey[200],
|
||||
valueColor: AlwaysStoppedAnimation<Color>(Colors.blue),
|
||||
),
|
||||
trailing: ElevatedButton(
|
||||
onPressed: () {
|
||||
// "Cancel" button action can be added here
|
||||
},
|
||||
child: Text("Cancel"),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,10 @@
|
|||
import 'package:extera_next/pages/download_manager/download_manager.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:extera_next/generated/l10n/l10n.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:matrix/matrix.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import 'package:extera_next/config/routes.dart';
|
||||
|
|
@ -42,12 +44,14 @@ class FluffyChatApp extends StatelessWidget {
|
|||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ThemeBuilder(
|
||||
builder: (context, themeMode, primaryColor, pureBlack) => MaterialApp.router(
|
||||
builder: (context, themeMode, primaryColor, pureBlack) =>
|
||||
MaterialApp.router(
|
||||
title: AppConfig.applicationName,
|
||||
themeMode: themeMode,
|
||||
theme: FluffyThemes.buildTheme(context, Brightness.light, primaryColor, pureBlack),
|
||||
darkTheme:
|
||||
FluffyThemes.buildTheme(context, Brightness.dark, primaryColor, pureBlack),
|
||||
theme: FluffyThemes.buildTheme(
|
||||
context, Brightness.light, primaryColor, pureBlack),
|
||||
darkTheme: FluffyThemes.buildTheme(
|
||||
context, Brightness.dark, primaryColor, pureBlack),
|
||||
scrollBehavior: CustomScrollBehavior(),
|
||||
localizationsDelegates: L10n.localizationsDelegates,
|
||||
supportedLocales: L10n.supportedLocales,
|
||||
|
|
@ -57,10 +61,13 @@ class FluffyChatApp extends StatelessWidget {
|
|||
clients: clients,
|
||||
// Need a navigator above the Matrix widget for
|
||||
// displaying dialogs
|
||||
child: Matrix(
|
||||
clients: clients,
|
||||
store: store,
|
||||
child: testWidget ?? child,
|
||||
child: ChangeNotifierProvider(
|
||||
create: (context) => DownloadManagerController(),
|
||||
child: Matrix(
|
||||
clients: clients,
|
||||
store: store,
|
||||
child: testWidget ?? child,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
|
|
|||
16
pubspec.lock
16
pubspec.lock
|
|
@ -329,6 +329,22 @@ packages:
|
|||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "7.0.2"
|
||||
dio:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: dio
|
||||
sha256: d90ee57923d1828ac14e492ca49440f65477f4bb1263575900be731a3dac66a9
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "5.9.0"
|
||||
dio_web_adapter:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: dio_web_adapter
|
||||
sha256: "7586e476d70caecaf1686d21eee7247ea43ef5c345eab9e0cc3583ff13378d78"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "2.1.1"
|
||||
dynamic_color:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
|
|
|
|||
|
|
@ -97,6 +97,7 @@ dependencies:
|
|||
vodozemac: ^0.2.0
|
||||
wakelock_plus: ^1.2.2
|
||||
webrtc_interface: ^1.0.13
|
||||
dio: ^5.9.0
|
||||
|
||||
dev_dependencies:
|
||||
flutter_lints: ^3.0.0
|
||||
|
|
|
|||
Loading…
Reference in New Issue