Skip to content

Commit

Permalink
add download path config
Browse files Browse the repository at this point in the history
  • Loading branch information
ogios committed May 15, 2024
1 parent 43a67ab commit bc4a1e3
Show file tree
Hide file tree
Showing 8 changed files with 221 additions and 43 deletions.
10 changes: 3 additions & 7 deletions lib/api/dtserv.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:ogios_sutils/in.dart';
import 'package:ogios_sutils/out.dart';
import 'package:path_provider/path_provider.dart';
import 'package:transfer_client/api/proxy.dart';
import 'package:transfer_client/page/home/config/download_path/download_path.dart';
import 'package:transfer_client/page/home/config/page.dart';

import '../page/home/download/dfile.dart';
Expand Down Expand Up @@ -55,13 +56,8 @@ class DTServ {
}

static Future<File> _getFile(String filename) async {
Directory? ddir = await getDownloadsDirectory();
if (ddir == null) {
throw Exception("Download dir not found!");
}
Directory dir = Directory(ddir.absolute.path + base_path);
if (!dir.existsSync()) dir.createSync();
File file = File(ddir.absolute.path + base_path + "/$filename");
var path = DownloadPath.getDownloadPath();
File file = File("$path/$filename");
if (!file.existsSync()) file.createSync();
return file;
}
Expand Down
6 changes: 5 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:transfer_client/api/proxy.dart';
import 'package:transfer_client/desktop.dart';
import 'package:transfer_client/page/home/config/dns/d_enable.dart';
import 'package:transfer_client/page/home/config/download_path/download_path.dart';
import 'package:transfer_client/page/home/config/page.dart';
import 'package:transfer_client/page/home/config/proxy/p_enable.dart';
import 'package:transfer_client/page/home/config/proxy/p_host.dart';
Expand All @@ -17,7 +20,6 @@ import 'package:transfer_client/receive_share.dart';
GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();

Future<void> initConfig() async {
print("Gobalconfig: ${GlobalConfig.toString()}");
var prefs = await SharedPreferences.getInstance();
CHost.initConfig(GlobalConfig, prefs);
CPort.initConfig(GlobalConfig, prefs);
Expand All @@ -27,7 +29,9 @@ Future<void> initConfig() async {
PEnable.initConfig(GlobalConfig, prefs);
PKey.initConfig(GlobalConfig, prefs);
DEnable.initConfig(GlobalConfig, prefs);
DownloadPath.initConfig(GlobalConfig, prefs);
GlobalConfig.done = true;
log("Gobalconfig: ${GlobalConfig.toString()}");
}

Future<void>? init() async {
Expand Down
112 changes: 112 additions & 0 deletions lib/page/home/config/download_path/download_path.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import 'dart:async';
import 'dart:developer';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:path_provider/path_provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:transfer_client/main.dart';
import 'package:transfer_client/page/home/config/page.dart';

class DownloadPath extends StatelessWidget implements ConfigWiget {
DownloadPath({super.key}) {
textEditingController = TextEditingController();
textEditingController.text = GlobalConfig.download_path.toString();
fToast = FToast();
fToast.init(navigatorKey.currentContext!);
}

Config global = GlobalConfig;
static const String PrefKey = "config.download_path";
late FToast fToast;

static void setConfig(Config global, String val) {
log("donwload path: $val");
global.download_path = val;
}

Widget toast = Container(
padding: const EdgeInsets.symmetric(horizontal: 24.0, vertical: 12.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(0.0),
color: Colors.greenAccent,
),
child: const Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.check),
SizedBox(
width: 12.0,
),
Text("Download path saved"),
],
),
);

late TextEditingController textEditingController;
Timer timer = Timer(const Duration(microseconds: 0), () {});
void onHostCommit(String p) async {
timer?.cancel();
timer = Timer(const Duration(seconds: 1), () async {
setConfig(global, p);
(await SharedPreferences.getInstance()).setString(PrefKey, p);
fToast.showToast(child: toast, gravity: ToastGravity.TOP_RIGHT);
});
}

@override
Widget build(BuildContext context) {
return ListTile(
textColor: Colors.white,
leading: const Icon(
Icons.lens_blur,
size: 40,
color: Colors.white,
),
title: const Text("Download Path"),
subtitle: TextField(
// keyboardType: TextInputType.text,
controller: textEditingController,
// inputFormatters: <TextInputFormatter>[
// FilteringTextInputFormatter.digitsOnly
// ],
onSubmitted: onHostCommit,
onChanged: onHostCommit,
cursorColor: Colors.white,
decoration: const InputDecoration(
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.white)),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.white))),
),
);
}

@override
static Future<void> initConfig(Config global, SharedPreferences prefs) async {
String a;
try {
a = prefs.getString(PrefKey)!;
} catch (err) {
var ddir = await getDownloadsDirectory();
if (ddir == null) {
throw Exception("Download dir not found!");
}
var dir = Directory("${ddir.absolute.path}/transfer_client");
if (!dir.existsSync()) dir.createSync();
a = dir.absolute.path;
}
setConfig(global, a);
}

static String getDownloadPath() {
var path = GlobalConfig.download_path;
if (path == "") {
throw Exception("Download path not set!");
}
var dir = Directory(path);
if (!dir.existsSync()) dir.createSync();
return GlobalConfig.download_path;
}
}
29 changes: 29 additions & 0 deletions lib/page/home/config/download_path/sec.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import 'package:flutter/material.dart';
import 'package:transfer_client/page/home/custom_component.dart';
import 'package:transfer_client/page/home/homepage.dart';

import 'download_path.dart';

class DownloadConfig extends StatelessWidget {
List<Widget> getDownloadConfig() {
List<Widget> b = [];
var temp = <Widget>[
DownloadPath(),
];
for (Widget a in temp) {
// b.add(Card(color: actionColor, child: SizedBox.expand(child: a)));
b.add(Card(color: actionColor, child: a));
}
return b;
}

@override
Widget build(BuildContext context) {
return CustomConfigSec(
context,
"Download Config",
Column(
children: getDownloadConfig(),
));
}
}
3 changes: 3 additions & 0 deletions lib/page/home/config/page.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:transfer_client/page/home/config/dns/sec.dart';
import 'package:transfer_client/page/home/config/download_path/sec.dart';
import 'package:transfer_client/page/home/config/proxy/sec.dart';
import 'package:transfer_client/page/home/config/tserv/sec.dart';
import 'package:transfer_client/page/home/config/udp/sec.dart';
Expand All @@ -17,6 +18,7 @@ class Config {
String p_key = "";
bool p_enable = false;
bool d_enable = false;
String download_path = "";

@override
String toString() {
Expand Down Expand Up @@ -50,6 +52,7 @@ class _ConfigPage extends State<ConfigPage> {
UDPConfig(),
ProxyConfig(),
DnsConfig(),
DownloadConfig(),
];

@override
Expand Down
18 changes: 13 additions & 5 deletions lib/page/home/download/downlaod_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import 'dart:io';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'package:share_plus/share_plus.dart';
import 'package:transfer_client/page/home/config/download_path/download_path.dart';
import 'package:transfer_client/page/home/config/page.dart';
import 'package:transfer_client/page/home/custom_component.dart';
import 'package:transfer_client/page/home/download/dfile.dart';
import 'package:transfer_client/page/home/download/page.dart';
Expand Down Expand Up @@ -120,7 +122,10 @@ class _DownloadItem extends State<DownloadItem> {
const EdgeInsets.only(top: 25, left: 25, right: 25, bottom: 10),
titleTextStyle: Theme.of(context).textTheme.titleMedium,
backgroundColor: actionColor,
content: Text("Sure?", style: Theme.of(context).textTheme.labelMedium,),
content: Text(
"Sure?",
style: Theme.of(context).textTheme.labelMedium,
),
contentPadding: const EdgeInsets.only(left: 25, right: 25, bottom: 10),
contentTextStyle: const TextStyle(color: Colors.black54, fontSize: 14),
actionsPadding: const EdgeInsets.only(left: 25, right: 25, bottom: 10),
Expand Down Expand Up @@ -156,9 +161,11 @@ class _DownloadItem extends State<DownloadItem> {
},
onDoubleTap: () async {
try {
Directory? sys_path = await getDownloadsDirectory();
String path =
"${sys_path!.path}${GlobalDownloadList.base}/${this.widget.dFile.filename}";
// Directory? sys_path = await getDownloadsDirectory();
// String path =
// "${sys_path!.path}${GlobalDownloadList.base}/${this.widget.dFile.filename}";
var path =
"${DownloadPath.getDownloadPath()}/${this.widget.dFile.filename}";
await Share.shareFiles([path], text: "File from transfer-client");
} catch (err) {
GlobalFtoast.error("Share File ERROR: $err", context);
Expand All @@ -167,7 +174,8 @@ class _DownloadItem extends State<DownloadItem> {
child: Column(
children: [
CustomCard(ListTile(
title: Text(getTitle(), style: Theme.of(context).textTheme.titleMedium),
title: Text(getTitle(),
style: Theme.of(context).textTheme.titleMedium),
subtitle: getProgress(),
leading: getIcon(),
)),
Expand Down
20 changes: 11 additions & 9 deletions lib/page/home/download/page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import 'package:synchronized/synchronized.dart';
import 'package:transfer_client/api/dtserv.dart';
import 'package:transfer_client/page/home/config/download_path/download_path.dart';
import 'package:transfer_client/page/home/config/page.dart';
import 'package:transfer_client/page/home/download/downlaod_item.dart';
import 'package:transfer_client/page/home/custom_component.dart';

Expand Down Expand Up @@ -47,13 +49,7 @@ class DownloadList {
}

Future<Directory> getDir() async {
Directory? ddir = await getDownloadsDirectory();
if (ddir == null) {
throw Exception("Download dir not found!");
}
Directory dir = Directory(ddir.absolute.path + base);
if (!dir.existsSync()) dir.createSync();
return dir;
return Directory(DownloadPath.getDownloadPath());
}

void scan() async {
Expand Down Expand Up @@ -145,11 +141,17 @@ class _DownloadPage extends State<DownloadPage> {
Widget build(BuildContext context) {
return Scaffold(
appBar: CustomBar(
Text("Download", style: Theme.of(context).textTheme.titleLarge,),
Text(
"Download",
style: Theme.of(context).textTheme.titleLarge,
),
[
IconButton(
onPressed: GlobalDownloadList.clear,
icon: const Icon(Icons.delete_forever, color: Colors.white,))
icon: const Icon(
Icons.delete_forever,
color: Colors.white,
))
],
),
body: ListView.builder(
Expand Down
Loading

0 comments on commit bc4a1e3

Please sign in to comment.