Skip to content

Commit d5d8076

Browse files
author
skybird23333
committed
Feat Plugins Browser: Add loading text
1 parent d4b54f8 commit d5d8076

File tree

1 file changed

+74
-44
lines changed

1 file changed

+74
-44
lines changed

lib/screens/settings/plugins_browser.dart

Lines changed: 74 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'package:http/http.dart' as http;
99
import 'package:http/http.dart';
1010
import 'package:github/github.dart';
1111
import 'package:archive/archive.dart';
12+
1213
class PluginsBrowser extends StatefulWidget {
1314
static const routeName = "/Settings/PluginsManager/";
1415

@@ -22,35 +23,39 @@ class PluginsItem {
2223
String name, author, repo, version, iconUrl, desc, identifier;
2324
bool isInstalled = false;
2425
bool isProcessing = false;
26+
2527
String get folderName {
2628
return identifier + "@" + version;
2729
}
28-
PluginsItem(this.name, this.author, this.repo, this.version, this.iconUrl, this.desc, this.identifier, this.isInstalled);
30+
31+
PluginsItem(this.name, this.author, this.repo, this.version, this.iconUrl,
32+
this.desc, this.identifier, this.isInstalled);
2933
}
3034

3135
class PluginsBrowserState extends State<PluginsBrowser> {
3236
List<PluginsItem> items = [];
3337
JsonDecoder decoder = const JsonDecoder();
3438
GitHub github = GitHub(); // Create an anonymous github client
39+
bool _isLoadingVisible = false;
3540

36-
Future<bool> uninstallPlugins(PluginsItem item)async{
41+
Future<bool> uninstallPlugins(PluginsItem item) async {
3742
debugPrint("Uninstalling ${item.identifier}");
3843
setState(() {
3944
item.isProcessing = true;
4045
});
4146
JsModule? module;
42-
for(var m in ModulesManager.externalModules){
43-
if(m.identifier == item.identifier){
47+
for (var m in ModulesManager.externalModules) {
48+
if (m.identifier == item.identifier) {
4449
module = m;
4550
}
4651
}
47-
if(module != null){
52+
if (module != null) {
4853
debugPrint("Found module. deleting files...");
4954
var path = module.moduleFolder;
5055
await Directory(path).delete(recursive: true);
5156
ModulesManager.externalModules.remove(module);
5257
item.isInstalled = false;
53-
}else{
58+
} else {
5459
return false;
5560
}
5661
setState(() {
@@ -59,23 +64,26 @@ class PluginsBrowserState extends State<PluginsBrowser> {
5964
return true;
6065
}
6166

62-
Future<bool> installPlugins(PluginsItem item)async{
67+
Future<bool> installPlugins(PluginsItem item) async {
6368
setState(() {
6469
item.isProcessing = true;
6570
});
66-
if(! await PluginsManager.checkPluginsExist(item.identifier,item.version)) {
71+
if (!await PluginsManager.checkPluginsExist(
72+
item.identifier, item.version)) {
6773
try {
6874
Repository repo = await github.repositories.getRepository(
6975
RepositorySlug.full(
7076
item.repo.replaceAll("https://github.com/", "")));
71-
List<Release> releases = await github.repositories.listReleases(
72-
repo.slug()).toList();
77+
List<Release> releases =
78+
await github.repositories.listReleases(repo.slug()).toList();
7379

7480
if (repo.hasDownloads) {
7581
var r = releases.first;
7682
if (r.zipballUrl != null) {
7783
var bytes = (await http.get(Uri.parse(r.zipballUrl!))).bodyBytes;
78-
var path = await PluginsManager.pluginsPath + item.folderName + Platform.pathSeparator;
84+
var path = await PluginsManager.pluginsPath +
85+
item.folderName +
86+
Platform.pathSeparator;
7987
var archive = ZipDecoder().decodeBytes(bytes);
8088

8189
// Create the folder if not exists
@@ -103,17 +111,17 @@ class PluginsBrowserState extends State<PluginsBrowser> {
103111
"[Plugins Manager] Error: Plugins have no downloads available");
104112
}
105113
} on GitHubError catch (err) {
106-
debugPrint("[Plugins Manager] Can't find plugins repository ${item
107-
.identifier}");
114+
debugPrint(
115+
"[Plugins Manager] Can't find plugins repository ${item.identifier}");
108116
return false;
109117
}
110-
}else{
118+
} else {
111119
debugPrint("[PluginsManager] The plugins is already installed");
112120
item.isInstalled = true;
113121
return false;
114122
}
115123
/* Do Something with repo */
116-
if(item.isInstalled) return false;
124+
if (item.isInstalled) return false;
117125

118126
setState(() {
119127
item.isProcessing = false;
@@ -123,23 +131,31 @@ class PluginsBrowserState extends State<PluginsBrowser> {
123131

124132
void reloadPlugins({String query = ""}) async {
125133
items.clear();
134+
_isLoadingVisible = true;
126135
Response resp = await http.get(Uri.parse(
127-
"https://raw.githubusercontent.com/CoreCoder-Devs/corecoder_plugins/main/plugins.json"));
136+
"https://raw.githubusercontent.com/CoreCoder-Devs/corecoder_plugins/main/plugins.json")
137+
);
138+
_isLoadingVisible = false;
128139
if (resp.statusCode == 200) {
129140
// OK
130141
Map obj = decoder.convert(resp.body);
131142
for (var key in obj.keys) {
132143
var isInstalled = false;
133144
var identifier = obj[key]["identifier"];
134-
for(Module m in ModulesManager.externalModules){
135-
if(m.identifier == identifier){
145+
for (Module m in ModulesManager.externalModules) {
146+
if (m.identifier == identifier) {
136147
isInstalled = true;
137148
}
138149
}
139-
items.add(PluginsItem(obj[key]["name"], obj[key]["author"],
140-
obj[key]["repo"], obj[key]["version"], obj[key]["icon"], obj[key]["desc"],
141-
obj[key]["identifier"], isInstalled
142-
));
150+
items.add(PluginsItem(
151+
obj[key]["name"],
152+
obj[key]["author"],
153+
obj[key]["repo"],
154+
obj[key]["version"],
155+
obj[key]["icon"],
156+
obj[key]["desc"],
157+
obj[key]["identifier"],
158+
isInstalled));
143159
setState(() {}); // refresh
144160
}
145161
}
@@ -154,27 +170,41 @@ class PluginsBrowserState extends State<PluginsBrowser> {
154170
@override
155171
Widget build(BuildContext context) {
156172
return Scaffold(
157-
appBar: AppBar(title: const Text("Install Plugins"),),
158-
body: Column(
159-
children: List.generate(items.length, (index) {
160-
var item = items[index];
161-
return ListTile(
162-
title: Text(item.name),
163-
leading: Image.network(item.iconUrl),
164-
subtitle: Text(item.desc),
165-
trailing:
166-
(item.isProcessing)?
167-
const CircularProgressIndicator(value: null,)
168-
:
169-
(item.isInstalled)?
170-
ElevatedButton(
171-
onPressed: () {uninstallPlugins(item);},
172-
child: const Text("Uninstall"),
173-
):
174-
ElevatedButton(onPressed: (){installPlugins(item);}, child: const Text("Install")),
175-
onTap: (){},
176-
);
177-
}),
178-
));
173+
appBar: AppBar(
174+
title: const Text("Install Plugins"),
175+
),
176+
body: Column(children: [
177+
Visibility(
178+
visible: _isLoadingVisible,
179+
child: const Text('Loading...')
180+
),
181+
Column(
182+
children: List.generate(items.length, (index) {
183+
var item = items[index];
184+
return ListTile(
185+
title: Text(item.name),
186+
leading: Image.network(item.iconUrl),
187+
subtitle: Text(item.desc),
188+
trailing: (item.isProcessing)
189+
? const CircularProgressIndicator(
190+
value: null,
191+
)
192+
: (item.isInstalled)
193+
? ElevatedButton(
194+
onPressed: () {
195+
uninstallPlugins(item);
196+
},
197+
child: const Text("Uninstall"),
198+
)
199+
: ElevatedButton(
200+
onPressed: () {
201+
installPlugins(item);
202+
},
203+
child: const Text("Install")),
204+
onTap: () {},
205+
);
206+
}),
207+
)
208+
]));
179209
}
180210
}

0 commit comments

Comments
 (0)