Skip to content

Commit bc4a1e3

Browse files
committed
add download path config
1 parent 43a67ab commit bc4a1e3

File tree

8 files changed

+221
-43
lines changed

8 files changed

+221
-43
lines changed

lib/api/dtserv.dart

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'package:ogios_sutils/in.dart';
77
import 'package:ogios_sutils/out.dart';
88
import 'package:path_provider/path_provider.dart';
99
import 'package:transfer_client/api/proxy.dart';
10+
import 'package:transfer_client/page/home/config/download_path/download_path.dart';
1011
import 'package:transfer_client/page/home/config/page.dart';
1112

1213
import '../page/home/download/dfile.dart';
@@ -55,13 +56,8 @@ class DTServ {
5556
}
5657

5758
static Future<File> _getFile(String filename) async {
58-
Directory? ddir = await getDownloadsDirectory();
59-
if (ddir == null) {
60-
throw Exception("Download dir not found!");
61-
}
62-
Directory dir = Directory(ddir.absolute.path + base_path);
63-
if (!dir.existsSync()) dir.createSync();
64-
File file = File(ddir.absolute.path + base_path + "/$filename");
59+
var path = DownloadPath.getDownloadPath();
60+
File file = File("$path/$filename");
6561
if (!file.existsSync()) file.createSync();
6662
return file;
6763
}

lib/main.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import 'dart:developer';
2+
13
import 'package:flutter/material.dart';
24
import 'package:shared_preferences/shared_preferences.dart';
35
import 'package:transfer_client/api/proxy.dart';
46
import 'package:transfer_client/desktop.dart';
57
import 'package:transfer_client/page/home/config/dns/d_enable.dart';
8+
import 'package:transfer_client/page/home/config/download_path/download_path.dart';
69
import 'package:transfer_client/page/home/config/page.dart';
710
import 'package:transfer_client/page/home/config/proxy/p_enable.dart';
811
import 'package:transfer_client/page/home/config/proxy/p_host.dart';
@@ -17,7 +20,6 @@ import 'package:transfer_client/receive_share.dart';
1720
GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
1821

1922
Future<void> initConfig() async {
20-
print("Gobalconfig: ${GlobalConfig.toString()}");
2123
var prefs = await SharedPreferences.getInstance();
2224
CHost.initConfig(GlobalConfig, prefs);
2325
CPort.initConfig(GlobalConfig, prefs);
@@ -27,7 +29,9 @@ Future<void> initConfig() async {
2729
PEnable.initConfig(GlobalConfig, prefs);
2830
PKey.initConfig(GlobalConfig, prefs);
2931
DEnable.initConfig(GlobalConfig, prefs);
32+
DownloadPath.initConfig(GlobalConfig, prefs);
3033
GlobalConfig.done = true;
34+
log("Gobalconfig: ${GlobalConfig.toString()}");
3135
}
3236

3337
Future<void>? init() async {
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import 'dart:async';
2+
import 'dart:developer';
3+
import 'dart:io';
4+
5+
import 'package:flutter/material.dart';
6+
import 'package:fluttertoast/fluttertoast.dart';
7+
import 'package:path_provider/path_provider.dart';
8+
import 'package:shared_preferences/shared_preferences.dart';
9+
import 'package:transfer_client/main.dart';
10+
import 'package:transfer_client/page/home/config/page.dart';
11+
12+
class DownloadPath extends StatelessWidget implements ConfigWiget {
13+
DownloadPath({super.key}) {
14+
textEditingController = TextEditingController();
15+
textEditingController.text = GlobalConfig.download_path.toString();
16+
fToast = FToast();
17+
fToast.init(navigatorKey.currentContext!);
18+
}
19+
20+
Config global = GlobalConfig;
21+
static const String PrefKey = "config.download_path";
22+
late FToast fToast;
23+
24+
static void setConfig(Config global, String val) {
25+
log("donwload path: $val");
26+
global.download_path = val;
27+
}
28+
29+
Widget toast = Container(
30+
padding: const EdgeInsets.symmetric(horizontal: 24.0, vertical: 12.0),
31+
decoration: BoxDecoration(
32+
borderRadius: BorderRadius.circular(0.0),
33+
color: Colors.greenAccent,
34+
),
35+
child: const Row(
36+
mainAxisSize: MainAxisSize.min,
37+
children: [
38+
Icon(Icons.check),
39+
SizedBox(
40+
width: 12.0,
41+
),
42+
Text("Download path saved"),
43+
],
44+
),
45+
);
46+
47+
late TextEditingController textEditingController;
48+
Timer timer = Timer(const Duration(microseconds: 0), () {});
49+
void onHostCommit(String p) async {
50+
timer?.cancel();
51+
timer = Timer(const Duration(seconds: 1), () async {
52+
setConfig(global, p);
53+
(await SharedPreferences.getInstance()).setString(PrefKey, p);
54+
fToast.showToast(child: toast, gravity: ToastGravity.TOP_RIGHT);
55+
});
56+
}
57+
58+
@override
59+
Widget build(BuildContext context) {
60+
return ListTile(
61+
textColor: Colors.white,
62+
leading: const Icon(
63+
Icons.lens_blur,
64+
size: 40,
65+
color: Colors.white,
66+
),
67+
title: const Text("Download Path"),
68+
subtitle: TextField(
69+
// keyboardType: TextInputType.text,
70+
controller: textEditingController,
71+
// inputFormatters: <TextInputFormatter>[
72+
// FilteringTextInputFormatter.digitsOnly
73+
// ],
74+
onSubmitted: onHostCommit,
75+
onChanged: onHostCommit,
76+
cursorColor: Colors.white,
77+
decoration: const InputDecoration(
78+
enabledBorder: UnderlineInputBorder(
79+
borderSide: BorderSide(color: Colors.white)),
80+
focusedBorder: UnderlineInputBorder(
81+
borderSide: BorderSide(color: Colors.white))),
82+
),
83+
);
84+
}
85+
86+
@override
87+
static Future<void> initConfig(Config global, SharedPreferences prefs) async {
88+
String a;
89+
try {
90+
a = prefs.getString(PrefKey)!;
91+
} catch (err) {
92+
var ddir = await getDownloadsDirectory();
93+
if (ddir == null) {
94+
throw Exception("Download dir not found!");
95+
}
96+
var dir = Directory("${ddir.absolute.path}/transfer_client");
97+
if (!dir.existsSync()) dir.createSync();
98+
a = dir.absolute.path;
99+
}
100+
setConfig(global, a);
101+
}
102+
103+
static String getDownloadPath() {
104+
var path = GlobalConfig.download_path;
105+
if (path == "") {
106+
throw Exception("Download path not set!");
107+
}
108+
var dir = Directory(path);
109+
if (!dir.existsSync()) dir.createSync();
110+
return GlobalConfig.download_path;
111+
}
112+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:transfer_client/page/home/custom_component.dart';
3+
import 'package:transfer_client/page/home/homepage.dart';
4+
5+
import 'download_path.dart';
6+
7+
class DownloadConfig extends StatelessWidget {
8+
List<Widget> getDownloadConfig() {
9+
List<Widget> b = [];
10+
var temp = <Widget>[
11+
DownloadPath(),
12+
];
13+
for (Widget a in temp) {
14+
// b.add(Card(color: actionColor, child: SizedBox.expand(child: a)));
15+
b.add(Card(color: actionColor, child: a));
16+
}
17+
return b;
18+
}
19+
20+
@override
21+
Widget build(BuildContext context) {
22+
return CustomConfigSec(
23+
context,
24+
"Download Config",
25+
Column(
26+
children: getDownloadConfig(),
27+
));
28+
}
29+
}

lib/page/home/config/page.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'package:flutter/material.dart';
22
import 'package:shared_preferences/shared_preferences.dart';
33
import 'package:transfer_client/page/home/config/dns/sec.dart';
4+
import 'package:transfer_client/page/home/config/download_path/sec.dart';
45
import 'package:transfer_client/page/home/config/proxy/sec.dart';
56
import 'package:transfer_client/page/home/config/tserv/sec.dart';
67
import 'package:transfer_client/page/home/config/udp/sec.dart';
@@ -17,6 +18,7 @@ class Config {
1718
String p_key = "";
1819
bool p_enable = false;
1920
bool d_enable = false;
21+
String download_path = "";
2022

2123
@override
2224
String toString() {
@@ -50,6 +52,7 @@ class _ConfigPage extends State<ConfigPage> {
5052
UDPConfig(),
5153
ProxyConfig(),
5254
DnsConfig(),
55+
DownloadConfig(),
5356
];
5457

5558
@override

lib/page/home/download/downlaod_item.dart

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import 'dart:io';
33
import 'package:flutter/material.dart';
44
import 'package:path_provider/path_provider.dart';
55
import 'package:share_plus/share_plus.dart';
6+
import 'package:transfer_client/page/home/config/download_path/download_path.dart';
7+
import 'package:transfer_client/page/home/config/page.dart';
68
import 'package:transfer_client/page/home/custom_component.dart';
79
import 'package:transfer_client/page/home/download/dfile.dart';
810
import 'package:transfer_client/page/home/download/page.dart';
@@ -120,7 +122,10 @@ class _DownloadItem extends State<DownloadItem> {
120122
const EdgeInsets.only(top: 25, left: 25, right: 25, bottom: 10),
121123
titleTextStyle: Theme.of(context).textTheme.titleMedium,
122124
backgroundColor: actionColor,
123-
content: Text("Sure?", style: Theme.of(context).textTheme.labelMedium,),
125+
content: Text(
126+
"Sure?",
127+
style: Theme.of(context).textTheme.labelMedium,
128+
),
124129
contentPadding: const EdgeInsets.only(left: 25, right: 25, bottom: 10),
125130
contentTextStyle: const TextStyle(color: Colors.black54, fontSize: 14),
126131
actionsPadding: const EdgeInsets.only(left: 25, right: 25, bottom: 10),
@@ -156,9 +161,11 @@ class _DownloadItem extends State<DownloadItem> {
156161
},
157162
onDoubleTap: () async {
158163
try {
159-
Directory? sys_path = await getDownloadsDirectory();
160-
String path =
161-
"${sys_path!.path}${GlobalDownloadList.base}/${this.widget.dFile.filename}";
164+
// Directory? sys_path = await getDownloadsDirectory();
165+
// String path =
166+
// "${sys_path!.path}${GlobalDownloadList.base}/${this.widget.dFile.filename}";
167+
var path =
168+
"${DownloadPath.getDownloadPath()}/${this.widget.dFile.filename}";
162169
await Share.shareFiles([path], text: "File from transfer-client");
163170
} catch (err) {
164171
GlobalFtoast.error("Share File ERROR: $err", context);
@@ -167,7 +174,8 @@ class _DownloadItem extends State<DownloadItem> {
167174
child: Column(
168175
children: [
169176
CustomCard(ListTile(
170-
title: Text(getTitle(), style: Theme.of(context).textTheme.titleMedium),
177+
title: Text(getTitle(),
178+
style: Theme.of(context).textTheme.titleMedium),
171179
subtitle: getProgress(),
172180
leading: getIcon(),
173181
)),

lib/page/home/download/page.dart

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import 'package:path/path.dart';
55
import 'package:path_provider/path_provider.dart';
66
import 'package:synchronized/synchronized.dart';
77
import 'package:transfer_client/api/dtserv.dart';
8+
import 'package:transfer_client/page/home/config/download_path/download_path.dart';
9+
import 'package:transfer_client/page/home/config/page.dart';
810
import 'package:transfer_client/page/home/download/downlaod_item.dart';
911
import 'package:transfer_client/page/home/custom_component.dart';
1012

@@ -47,13 +49,7 @@ class DownloadList {
4749
}
4850

4951
Future<Directory> getDir() async {
50-
Directory? ddir = await getDownloadsDirectory();
51-
if (ddir == null) {
52-
throw Exception("Download dir not found!");
53-
}
54-
Directory dir = Directory(ddir.absolute.path + base);
55-
if (!dir.existsSync()) dir.createSync();
56-
return dir;
52+
return Directory(DownloadPath.getDownloadPath());
5753
}
5854

5955
void scan() async {
@@ -145,11 +141,17 @@ class _DownloadPage extends State<DownloadPage> {
145141
Widget build(BuildContext context) {
146142
return Scaffold(
147143
appBar: CustomBar(
148-
Text("Download", style: Theme.of(context).textTheme.titleLarge,),
144+
Text(
145+
"Download",
146+
style: Theme.of(context).textTheme.titleLarge,
147+
),
149148
[
150149
IconButton(
151150
onPressed: GlobalDownloadList.clear,
152-
icon: const Icon(Icons.delete_forever, color: Colors.white,))
151+
icon: const Icon(
152+
Icons.delete_forever,
153+
color: Colors.white,
154+
))
153155
],
154156
),
155157
body: ListView.builder(

0 commit comments

Comments
 (0)