feature:完成Github图床配置页面的配置存储以及读取

This commit is contained in:
hackycy
2020-06-17 16:55:22 +08:00
parent 0f996d293e
commit 82f02f02d4
7 changed files with 278 additions and 14 deletions

Binary file not shown.

View File

@@ -0,0 +1,32 @@
class GithubConfig {
String repositoryName;
String branchName;
String token;
String storagePath;
String customDomain;
GithubConfig(
{this.repositoryName,
this.branchName,
this.token,
this.storagePath,
this.customDomain});
GithubConfig.fromJson(Map<String, dynamic> json) {
repositoryName = json['repositoryName'];
branchName = json['branchName'];
token = json['token'];
storagePath = json['storagePath'];
customDomain = json['customDomain'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['repositoryName'] = this.repositoryName;
data['branchName'] = this.branchName;
data['token'] = this.token;
data['storagePath'] = this.storagePath;
data['customDomain'] = this.customDomain;
return data;
}
}

View File

@@ -1,7 +1,7 @@
import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:flutter_picgo/views/app_page/app_page.dart';
import 'package:flutter_picgo/views/home.dart';
import 'package:flutter_picgo/views/album_page/album_page.dart';
import 'package:flutter_picgo/views/pb_setting_page/pb_setting_page.dart';
import 'package:flutter_picgo/views/pb_setting_page/github_page/github_page.dart';

View File

@@ -12,14 +12,16 @@ class Sql extends BaseModel {
return await this.query(tableName);
}
String getTableName() {
return tableName;
Future<List> getBySql(String where, List<dynamic> whereArgs) async {
return await this.query(tableName, where: where, whereArgs: whereArgs);
}
Future<int> delete(String value, String key) async {
return await this
.db
.delete(tableName, where: '$key = ?', whereArgs: [value]);
Future<int> rawUpdate(String sql, [List<dynamic> arguments]) async {
return await this.db.rawUpdate('UPDATE $tableName SET $sql', arguments);
}
String getTableName() {
return tableName;
}
Future<int> deleteAll() async {

View File

@@ -1,7 +1,7 @@
import 'package:flutter/material.dart';
import '../album_page/album_page.dart';
import '../setting_page/setting_page.dart';
import 'album_page/album_page.dart';
import 'setting_page/setting_page.dart';
class AppPage extends StatefulWidget {

View File

@@ -1,22 +1,203 @@
import 'package:flutter/material.dart';
import 'package:flutter_picgo/model/github_config.dart';
import 'package:flutter_picgo/views/pb_setting_page/github_page/github_page_presenter.dart';
import 'package:toast/toast.dart';
class GithubPage extends StatefulWidget {
@override
_GithubPageState createState() => _GithubPageState();
}
class _GithubPageState extends State<GithubPage> {
class _GithubPageState extends State<GithubPage> implements GithubPageContract {
BuildContext _ctx;
GithubConfig _config;
GithubPagePresenter _presenter;
TextEditingController _repositoryNameController,
_branchNameController,
_tokenController,
_storagePathController,
_customDomainController;
_GithubPageState() {
_presenter = GithubPagePresenter(this);
}
final _formKey = GlobalKey<FormState>();
@override
void initState() {
super.initState();
_presenter.doLoadConfig();
}
@override
Widget build(BuildContext context) {
_ctx = context;
_repositoryNameController =
TextEditingController(text: _config?.repositoryName ?? '');
_branchNameController =
TextEditingController(text: _config?.branchName ?? '');
_tokenController = TextEditingController(text: _config?.token ?? '');
_storagePathController =
TextEditingController(text: _config?.storagePath ?? '');
_customDomainController =
TextEditingController(text: _config?.customDomain ?? '');
return Scaffold(
appBar: AppBar(
title: Text('Github图床'),
),
body: ListView(
children: <Widget>[
actions: <Widget>[
IconButton(
icon: Icon(IconData(0xe62a, fontFamily: 'iconfont')),
onPressed: () {
_testConfig();
},
),
],
),
body: Padding(
padding: EdgeInsets.all(10.0),
child: ListView(
children: <Widget>[
Form(
key: _formKey,
child: Column(
children: <Widget>[
TextFormField(
controller: _repositoryNameController,
decoration: new InputDecoration(
labelText: "设定仓库名",
hintText: "例如 hackycy/picture-bed",
),
keyboardType: TextInputType.text,
validator: (value) {
if (value == null || value == '') {
return '仓库名不能为空';
}
return null;
},
),
SizedBox(height: 5),
TextFormField(
controller: _branchNameController,
decoration: new InputDecoration(
labelText: "设定分支名",
hintText: "例如 master",
),
keyboardType: TextInputType.text,
validator: (value) {
if (value == null || value == '') {
return '分支名不能为空';
}
return null;
},
),
SizedBox(height: 5),
TextFormField(
controller: _tokenController,
obscureText: true,
decoration: new InputDecoration(
labelText: "设定 Token",
),
keyboardType: TextInputType.text,
validator: (value) {
if (value == null || value == '') {
return 'Token不能为空';
}
return null;
},
),
SizedBox(height: 5),
TextFormField(
controller: _storagePathController,
obscureText: true,
decoration: new InputDecoration(
labelText: "指定存储路径",
),
keyboardType: TextInputType.text,
),
SizedBox(height: 5),
TextFormField(
controller: _customDomainController,
obscureText: true,
decoration: new InputDecoration(
labelText: "设置自定义域名",
),
keyboardType: TextInputType.text,
)
],
),
),
SizedBox(height: 10),
Padding(
padding: EdgeInsets.all(10.0),
child: Row(
children: <Widget>[
Expanded(
child: RaisedButton(
color: Theme.of(context).accentColor,
textColor: Colors.white,
child: Text('保存'),
onPressed: () {
_saveConfig();
},
),
),
SizedBox(width: 5.0),
Expanded(
child: RaisedButton(
color: Colors.greenAccent,
textColor: Colors.white,
child: Text('设为默认图床'),
onPressed: () {
_setDefaultPB();
},
),
),
],
),
),
],
),
),
);
}
void _testConfig() {
if (_formKey.currentState.validate()) {}
}
void _saveConfig() {
if (_formKey.currentState.validate()) {
var config = GithubConfig(
repositoryName: _repositoryNameController.text,
branchName: _branchNameController.text,
token: _tokenController.text,
storagePath: _storagePathController.text,
customDomain: _customDomainController.text);
_presenter.doSaveConfig(config);
}
}
void _setDefaultPB() {
if (_formKey.currentState.validate()) {}
}
@override
loadConfig(GithubConfig config) {
setState(() {
this._config = config;
});
}
@override
saveConfigSuccess() {
Toast.show("保存成功", context, duration: Toast.LENGTH_SHORT, gravity: Toast.BOTTOM);
}
@override
showError(String errorMsg) {
Toast.show(errorMsg, context, duration: Toast.LENGTH_LONG, gravity: Toast.BOTTOM);
}
}

View File

@@ -0,0 +1,49 @@
import 'dart:convert';
import 'package:flutter_picgo/model/github_config.dart';
import 'package:flutter_picgo/utils/sql.dart';
abstract class GithubPageContract {
loadConfig(GithubConfig config);
saveConfigSuccess();
showError(String errorMsg);
}
class GithubPagePresenter {
GithubPageContract _view;
GithubPagePresenter(this._view);
doLoadConfig() async {
try {
var sql = Sql.setTable('pb_setting');
var pbsettingRow = (await sql.getBySql('type = ?', ['github']))?.first;
if (pbsettingRow != null &&
(pbsettingRow["config"] != null || pbsettingRow["config"] != '')) {
GithubConfig config = GithubConfig.fromJson(json.decode(pbsettingRow["config"]));
_view.loadConfig(config);
}
} catch (e) {
_view.showError('$e');
}
}
doSaveConfig(GithubConfig config) async {
if (config != null) {
try {
String jsondata = json.encode(config);
var sql = Sql.setTable('pb_setting');
int raw = await sql
.rawUpdate('config = ? WHERE type = ?', [jsondata, 'github']);
if (raw == 1) {
_view.saveConfigSuccess();
} else {
_view.showError('保存失败!请重试!');
}
} catch (e) {
_view.showError('$e');
}
}
}
}