Skip to content

Commit f974b9e

Browse files
committed
feature:complete support manage github
1 parent 770b5bb commit f974b9e

File tree

4 files changed

+84
-40
lines changed

4 files changed

+84
-40
lines changed

lib/components/manage_item.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ class ManageItem extends StatelessWidget {
9090
child: Container(
9191
width: 10,
9292
height: 10,
93-
child: CircularProgressIndicator(),
93+
child: CircularProgressIndicator(
94+
strokeWidth: 3,
95+
),
9496
),
9597
),
9698
errorWidget: (context, url, error) {

lib/model/github_content.dart

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import 'package:flutter_picgo/components/manage_item.dart';
2+
13
class GithubContent {
2-
GithubContentType type;
4+
FileContentType type;
35
String encoding;
46
int size;
57
String name;
@@ -26,7 +28,7 @@ class GithubContent {
2628
});
2729

2830
GithubContent.fromJson(Map<String, dynamic> json) {
29-
type = json['type'] == 'file' ? GithubContentType.FILE : GithubContentType.DIR;
31+
type = json['type'] == 'file' ? FileContentType.FILE : FileContentType.DIR;
3032
encoding = json['encoding'];
3133
size = json['size'];
3234
name = json['name'];
@@ -41,7 +43,7 @@ class GithubContent {
4143

4244
Map<String, dynamic> toJson() {
4345
final Map<String, dynamic> data = new Map<String, dynamic>();
44-
data['type'] = this.type == GithubContentType.FILE ? 'file' : 'dir';
46+
data['type'] = this.type == FileContentType.FILE ? 'file' : 'dir';
4547
data['encoding'] = this.encoding;
4648
data['size'] = this.size;
4749
data['name'] = this.name;
@@ -55,12 +57,3 @@ class GithubContent {
5557
return data;
5658
}
5759
}
58-
59-
/// 文件类型
60-
enum GithubContentType {
61-
/// 文件
62-
FILE,
63-
64-
/// 文件夹
65-
DIR,
66-
}

lib/views/manage_page/github_page/github_repo_page.dart

Lines changed: 48 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import 'package:fluro/fluro.dart';
22
import 'package:flutter/material.dart';
3+
import 'package:flutter_picgo/components/loading.dart';
4+
import 'package:flutter_picgo/components/manage_item.dart';
35
import 'package:flutter_picgo/model/github_content.dart';
46
import 'package:flutter_picgo/routers/application.dart';
57
import 'package:flutter_picgo/routers/routers.dart';
@@ -8,6 +10,7 @@ import 'package:flutter_picgo/views/manage_page/github_page/github_repo_page_pre
810
import 'package:path/path.dart' as pathlib;
911
import 'package:flutter/services.dart';
1012
import 'package:toast/toast.dart';
13+
import 'package:url_launcher/url_launcher.dart';
1114

1215
class GithubRepoPage extends StatefulWidget {
1316
final String path;
@@ -129,33 +132,51 @@ class _GithubRepoPageState extends BaseLoadingPageState<GithubRepoPage>
129132
return ListView.builder(
130133
itemCount: contents.length,
131134
itemBuilder: (context, index) {
132-
return Container(
133-
decoration: BoxDecoration(
134-
border: Border(
135-
bottom: BorderSide(width: 0.5, color: Colors.grey[400])),
136-
),
137-
child: ListTile(
138-
title: Text(contents[index].name,
139-
textWidthBasis: TextWidthBasis.longestLine,
140-
maxLines: 1,
141-
overflow: TextOverflow.ellipsis),
142-
leading: Icon(contents[index].type == GithubContentType.FILE
143-
? IconData(0xe654, fontFamily: 'iconfont')
144-
: IconData(0xe63f, fontFamily: 'iconfont')),
145-
onTap: () {
146-
if (contents[index].type == GithubContentType.DIR) {
147-
var prePathParam = pathlib
148-
.joinAll([_prePath ?? '', _path == '/' ? '' : _path]);
149-
Application.router.navigateTo(context,
150-
'${Routes.settingPbGitubRepo}?path=${Uri.encodeComponent(contents[index].name)}&prePath=${Uri.encodeComponent(prePathParam)}',
151-
transition: TransitionType.cupertino);
152-
} else {
153-
Clipboard.setData(
154-
ClipboardData(text: contents[index].downloadUrl));
155-
Toast.show('已获取下载链接到剪切板', context);
156-
}
157-
},
158-
),
135+
return ManageItem(
136+
Key('$index'),
137+
contents[index].downloadUrl,
138+
contents[index].name,
139+
'${contents[index].size}k',
140+
contents[index].type,
141+
onTap: () {
142+
if (contents[index].type == FileContentType.DIR) {
143+
var prePathParam = pathlib
144+
.joinAll([_prePath ?? '', _path == '/' ? '' : _path]);
145+
Application.router.navigateTo(context,
146+
'${Routes.settingPbGitubRepo}?path=${Uri.encodeComponent(contents[index].name)}&prePath=${Uri.encodeComponent(prePathParam)}',
147+
transition: TransitionType.cupertino);
148+
} else {
149+
launch(contents[index].downloadUrl);
150+
}
151+
},
152+
confirmDismiss: (direction) async {
153+
if (contents[index].type == FileContentType.DIR) {
154+
Toast.show('暂不支持删除文件夹', context);
155+
return false;
156+
}
157+
bool result = await showDialog(
158+
context: context,
159+
builder: (BuildContext context) {
160+
return AlertDialog(
161+
title: Text('确定删除吗'),
162+
content: Text('删除后无法恢复'),
163+
actions: <Widget>[
164+
FlatButton(
165+
child: Text('确定'),
166+
onPressed: () {
167+
Navigator.pop(context, true);
168+
}),
169+
],
170+
);
171+
});
172+
if (!(result ?? false)) {
173+
return false;
174+
}
175+
176+
var del = await _presenter.doDeleteContents(
177+
_path, _prePath, contents[index].name, contents[index].sha);
178+
return del;
179+
},
159180
);
160181
});
161182
}

lib/views/manage_page/github_page/github_repo_page_presenter.dart

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,32 @@ class GithubRepoPagePresenter {
4545
_view.loadError('$e');
4646
}
4747
}
48+
49+
Future<bool> doDeleteContents(
50+
String path, String prePath, String name, String sha) async {
51+
try {
52+
String configStr = await ImageUploadUtils.getPBConfig(PBTypeKeys.github);
53+
GithubConfig config = GithubConfig.fromJson(json.decode(configStr));
54+
if (isBlank(config.repo) || isBlank(config.token)) {
55+
_view.loadError('读取配置错误!');
56+
return false;
57+
}
58+
String url = pathutil.joinAll([
59+
'repos',
60+
config.repo,
61+
'contents',
62+
prePath ?? '',
63+
path == '/' ? '' : path,
64+
name
65+
]);
66+
await GithubApi.deleteContent(url, {
67+
"message": 'DELETE BY Flutter-PicGo',
68+
"sha": sha,
69+
"branch": config.branch
70+
});
71+
return true;
72+
} catch (e) {
73+
return false;
74+
}
75+
}
4876
}

0 commit comments

Comments
 (0)