wip: me panel.

This commit is contained in:
2022-01-07 18:04:59 +08:00
parent 048c54e080
commit 85a7d36fda
45 changed files with 2776 additions and 67 deletions
+18 -4
View File
@@ -6,6 +6,7 @@ import 'package:dde_gesture_manager/extensions.dart';
import 'package:dde_gesture_manager/models/scheme.dart' as AppScheme;
import 'package:dde_gesture_manager/utils/helper.dart';
import 'package:dde_gesture_manager/utils/notificator.dart';
import 'package:dde_gesture_manager/widgets/me.dart';
import 'package:dde_gesture_manager_api/apis.dart';
import 'package:dde_gesture_manager_api/models.dart';
import 'package:http/http.dart' as http;
@@ -85,7 +86,7 @@ class Api {
if (ignoreErrorHandle)
throw e;
else
return _handleHttpError(e);
_handleHttpError(e);
},
);
@@ -116,7 +117,7 @@ class Api {
if (ignoreErrorHandle)
throw e;
else
return _handleHttpError(e);
_handleHttpError(e);
},
);
@@ -158,6 +159,19 @@ class Api {
),
).then((value) => value == HttpStatus.noContent);
static Future<List<Scheme>> userUploads() =>
_get(Apis.scheme.userUploads, listRespBuilderWrap(SchemeSerializer.fromMap));
static Future<List<SimpleSchemeTransMetaData>> userSchemes({required SchemeListType type}) =>
_get(Apis.scheme.user(type: type.name.param), listRespBuilderWrap(SimpleSchemeTransMetaDataSerializer.fromMap));
static Future<bool> likeScheme({required String schemeId, required bool isLike}) => _get(
Apis.scheme.like(schemeId: schemeId.param, isLike: StringParam(isLike ? 'like' : 'unlike')),
getStatusCodeFunc)
.then((value) {
123.sout();
return value == HttpStatus.noContent;
});
static Future<SchemeForDownload> downloadScheme({required String schemeId}) => _get(
Apis.scheme.download(schemeId: schemeId.param),
SchemeForDownloadSerializer.fromMap,
);
}
+14 -9
View File
@@ -11,6 +11,7 @@ import 'package:dde_gesture_manager/themes/dark.dart';
import 'package:dde_gesture_manager/themes/light.dart';
import 'package:dde_gesture_manager/utils/helper.dart';
import 'package:dde_gesture_manager/utils/init.dart';
import 'package:dde_gesture_manager/utils/simple_throttle.dart';
import 'package:flutter/material.dart';
import 'pages/home.dart';
@@ -71,15 +72,7 @@ class MyApp extends StatelessWidget {
firstChild: Builder(builder: (context) {
Future.microtask(() {
initEvents(context);
if (H().lastCheckAuthStatusTime != null &&
H().lastCheckAuthStatusTime!.difference(DateTime.now()) < Duration(minutes: 10)) return;
if (context.read<ConfigsProvider>().accessToken.notNull) {
Api.checkAuthStatus().then((value) {
if (!value) context.read<ConfigsProvider>().setProps(email: '', accessToken: '');
});
} else {
H().lastCheckAuthStatusTime = DateTime.now();
}
SimpleThrottle.throttledFunc(_checkAuthStatus, timeout: const Duration(minutes: 5))?.call(context);
});
return Container();
}),
@@ -91,3 +84,15 @@ class MyApp extends StatelessWidget {
);
}
}
void _checkAuthStatus(BuildContext context) {
if (H().lastCheckAuthStatusTime != null &&
H().lastCheckAuthStatusTime!.difference(DateTime.now()) < Duration(minutes: 10)) return;
if (context.read<ConfigsProvider>().accessToken.notNull) {
Api.checkAuthStatus().then((value) {
if (!value) context.read<ConfigsProvider>().setProps(email: '', accessToken: '');
});
} else {
H().lastCheckAuthStatusTime = DateTime.now();
}
}
+4 -1
View File
@@ -1,4 +1,7 @@
import 'package:dde_gesture_manager/builder/provider_annotation.dart';
import 'package:dde_gesture_manager/constants/sp_keys.dart';
import 'package:dde_gesture_manager/utils/helper.dart';
import 'package:dde_gesture_manager/extensions.dart';
@ProviderModel()
class ContentLayout {
@@ -9,7 +12,7 @@ class ContentLayout {
bool? marketOrMeOpened;
@ProviderModelProp()
bool? currentIsMarket = true;
bool? currentIsMarket = H().sp.getString(SPKeys.accessToken).isNull;
bool get isMarket => currentIsMarket ?? true;
}
+2 -2
View File
@@ -184,8 +184,8 @@ class _LocalManagerState extends State<LocalManager> {
),
),
),
Container(height: 5),
Container(
Padding(
padding: const EdgeInsets.only(top: 5),
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
+1 -1
View File
@@ -82,7 +82,7 @@ class MarketOrMe extends StatelessWidget {
Widget buildMeContent(BuildContext context) {
var accessToken = context.watch<ConfigsProvider>().accessToken;
if (accessToken.isNull) return LoginWidget();
return MeWidget();
return Expanded(child: MeWidget());
}
Widget buildMarketContent(BuildContext context) {
+3 -2
View File
@@ -49,9 +49,9 @@ Future<void> initEvents(BuildContext context) async {
}
}
if (!_updateChecked)
if (!_updateChecked) {
_updateChecked = true;
Api.checkAppVersion(ignoreErrorHandle: true).then((value) async {
_updateChecked = true;
var info = await PackageInfo.fromPlatform();
var _buildNumber = int.parse(info.buildNumber);
var _newVersionCode = value?.versionCode ?? 0;
@@ -74,6 +74,7 @@ Future<void> initEvents(BuildContext context) async {
});
}
});
}
}
Future<void> initConfigs() async {
+45
View File
@@ -0,0 +1,45 @@
import 'dart:collection';
import 'package:collection/collection.dart';
class _SimpleThrottleNode {
int funcHashCode;
int timestamp;
_SimpleThrottleNode(this.funcHashCode, this.timestamp);
}
typedef void _VoidFunc();
final _simpleThrottleQueue = Queue();
/// Usage: If you have a function : test(int n) => n;
/// you can use SimpleThrottle.throttledFunc(test)?.call(1) to make it throttled
/// this will return function's return value if last call time over the timeout
/// otherwise this will return null.
/// If your function is a 'void function()', you can use SimpleThrottle.invoke(func) to call it throttled,
/// and you can get a throttled function by SimpleThrottle.bind(func) if you do not call it immediately.
class SimpleThrottle {
static T? throttledFunc<T extends Function>(T func,
{String? funcKey, Duration timeout = const Duration(seconds: 1)}) {
var node = _simpleThrottleQueue.firstWhereOrNull((element) => element.funcHashCode == (funcKey ?? func).hashCode);
if (node != null) {
if (DateTime.now().millisecondsSinceEpoch - node.timestamp < timeout.inMilliseconds)
return null;
else
node.timestamp = DateTime.now().millisecondsSinceEpoch;
} else {
_simpleThrottleQueue.add(_SimpleThrottleNode((funcKey ?? func).hashCode, DateTime.now().millisecondsSinceEpoch));
while (_simpleThrottleQueue.length > 16) {
_simpleThrottleQueue.removeFirst();
}
}
return func;
}
static void invoke(_VoidFunc func, {String? funcKey, Duration timeout = const Duration(seconds: 1)}) =>
throttledFunc(func, timeout: timeout, funcKey: funcKey)?.call();
static _VoidFunc bind(_VoidFunc func, {String? funcKey, Duration timeout = const Duration(seconds: 1)}) =>
() => invoke(func, timeout: timeout, funcKey: funcKey);
}
+51
View File
@@ -141,6 +141,57 @@ class DButton extends StatefulWidget {
message: LocaleKeys.operation_upload.tr(),
));
factory DButton.download({
Key? key,
required enabled,
GestureTapCallback? onTap,
height = defaultButtonHeight * .7,
width = defaultButtonHeight * .7,
}) =>
DButton(
key: key,
width: width,
height: height,
onTap: enabled ? onTap : null,
child: Tooltip(
child: Opacity(opacity: enabled ? 1 : 0.4, child: const Icon(Icons.file_download, size: 18)),
message: LocaleKeys.operation_download.tr(),
));
factory DButton.share({
Key? key,
required enabled,
GestureTapCallback? onTap,
height = defaultButtonHeight * .7,
width = defaultButtonHeight * .7,
}) =>
DButton(
key: key,
width: width,
height: height,
onTap: enabled ? onTap : null,
child: Tooltip(
child: Opacity(opacity: enabled ? 1 : 0.4, child: const Icon(Icons.share, size: 18)),
message: LocaleKeys.operation_share.tr(),
));
factory DButton.like({
Key? key,
required enabled,
GestureTapCallback? onTap,
height = defaultButtonHeight * .7,
width = defaultButtonHeight * .7,
}) =>
DButton(
key: key,
width: width,
height: height,
onTap: enabled ? onTap : null,
child: Tooltip(
child: Opacity(opacity: enabled ? 1 : 0.4, child: const Icon(Icons.thumb_up, size: 16)),
message: LocaleKeys.operation_like.tr(),
));
factory DButton.dropdown({
Key? key,
width = 60.0,
+189 -11
View File
@@ -1,13 +1,23 @@
import 'package:auto_size_text/auto_size_text.dart';
import 'package:collection/collection.dart';
import 'package:dde_gesture_manager/constants/constants.dart';
import 'package:dde_gesture_manager/extensions.dart';
import 'package:dde_gesture_manager/http/api.dart';
import 'package:dde_gesture_manager/models/configs.provider.dart';
import 'package:dde_gesture_manager/models/settings.provider.dart';
import 'package:dde_gesture_manager/utils/notificator.dart';
import 'package:dde_gesture_manager_api/models.dart';
import 'package:flutter/material.dart';
import 'package:dde_gesture_manager/extensions.dart';
import 'package:flutter_platform_alert/flutter_platform_alert.dart';
import 'dde_button.dart';
enum SchemeListType {
uploaded,
downloaded,
liked,
}
class MeWidget extends StatefulWidget {
const MeWidget({Key? key}) : super(key: key);
@@ -16,23 +26,46 @@ class MeWidget extends StatefulWidget {
}
class _MeWidgetState extends State<MeWidget> {
List<Scheme> uploads = [];
List<SimpleSchemeTransMetaData> _schemes = [];
SchemeListType _type = SchemeListType.uploaded;
String? _selected;
String? _hovering;
@override
void initState() {
super.initState();
Api.userUploads().then((value) {
Api.userSchemes(type: _type).then((value) {
if (mounted)
setState(() {
uploads = value;
_schemes = value;
_selected = value.isNotEmpty ? value.first.uuid : null;
});
});
}
Color _getItemBackgroundColor(int index, String? schemeId) {
Color _color = index % 2 == 0 ? context.t.scaffoldBackgroundColor : context.t.backgroundColor;
if (schemeId == _hovering) _color = context.t.dialogBackgroundColor;
if (schemeId == _selected) _color = context.read<SettingsProvider>().currentActiveColor;
return _color;
}
_refreshList() {
Future.delayed(const Duration(milliseconds: 100), () {
Api.userSchemes(type: _type).then((value) {
if (mounted)
setState(() {
_schemes = value;
});
});
});
}
@override
Widget build(BuildContext context) {
var currentSelectedScheme = _schemes.firstWhereOrNull((e) => e.uuid == _selected);
return Padding(
padding: EdgeInsets.symmetric(vertical: 10),
padding: EdgeInsets.only(top: 10),
child: Column(
children: [
Row(
@@ -54,12 +87,157 @@ class _MeWidgetState extends State<MeWidget> {
),
],
),
Text('我的上传'),
Container(
height: 400,
child: ListView.builder(
itemBuilder: (context, index) => Text(uploads[index].name ?? ''),
itemCount: uploads.length,
Padding(
padding: const EdgeInsets.only(top: 3, bottom: 2),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
SchemeListType.uploaded,
SchemeListType.downloaded,
SchemeListType.liked,
]
.map(
(e) => Flexible(
flex: 1,
fit: FlexFit.tight,
child: MouseRegion(
cursor: SystemMouseCursors.click,
child: GestureDetector(
onTap: () {
setState(() {
_type = e;
});
Api.userSchemes(type: e).then((value) {
if (mounted)
setState(() {
_schemes = value;
});
});
},
child: Center(
child: Text(
'${LocaleKeys.me_scheme_types}.${e.name}'.tr(),
style: _type == e ? TextStyle(fontWeight: FontWeight.bold, fontSize: 15) : null,
),
),
),
),
),
)
.toList(),
),
),
Expanded(
child: Container(
decoration: BoxDecoration(
border: Border.all(
width: .3,
color: context.t.dividerColor,
),
borderRadius: BorderRadius.circular(defaultBorderRadius),
),
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 1, vertical: 2),
child: ListView.builder(
itemBuilder: (context, index) => GestureDetector(
onTap: () {
setState(() {
_selected = _schemes[index].uuid;
});
},
child: MouseRegion(
cursor: SystemMouseCursors.click,
onEnter: (_) {
setState(() {
_hovering = _schemes[index].uuid;
});
},
child: Container(
color: _getItemBackgroundColor(index, _schemes[index].uuid),
child: Padding(
padding: const EdgeInsets.only(left: 6, right: 12.0),
child: DefaultTextStyle(
style: context.t.textTheme.bodyText2!.copyWith(
color: _selected == _schemes[index].uuid ? Colors.white : null,
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(_schemes[index].name ?? ''),
Row(
children: [
Text('${_schemes[index].downloads ?? 0}'.padLeft(4)),
Icon(
Icons.file_download,
size: 18,
),
Text('${_schemes[index].likes ?? 0}'.padLeft(4)),
Icon(_schemes[index].liked == true ? Icons.thumb_up : Icons.thumb_up_off_alt,
size: 17),
]
.map((e) => Padding(
padding: const EdgeInsets.only(right: 3),
child: e,
))
.toList(),
),
],
),
),
),
),
),
),
itemCount: _schemes.length,
),
),
),
),
Padding(
padding: const EdgeInsets.only(top: 5),
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
if (_type == SchemeListType.uploaded)
DButton.share(
enabled: currentSelectedScheme?.shared == false,
onTap: () {
Notificator.showConfirm(
title: LocaleKeys.info_share_title.tr(),
description: LocaleKeys.info_share_description.tr())
.then((value) {
if (value == CustomButton.positiveButton) {
Notificator.success(context, title: LocaleKeys.info_share_success.tr());
}
});
},
),
DButton.like(
enabled: true,
onTap: () {
Api.likeScheme(schemeId: currentSelectedScheme!.uuid!, isLike: !currentSelectedScheme.liked!)
.then((value) {
if (value) {
_refreshList();
}
});
},
),
DButton.download(
enabled: true,
onTap: () {
Api.downloadScheme(schemeId: currentSelectedScheme!.uuid!).then((value) {
value.sout();
_refreshList();
});
},
),
]
.map((e) => Padding(
padding: const EdgeInsets.only(right: 4),
child: e,
))
.toList(),
),
),
],
+4
View File
@@ -57,6 +57,10 @@ dev_dependencies:
build_runner: 2.1.2
source_gen: 1.1.0
dependency_overrides:
angel3_orm:
path: ../api/3rd_party/angel3_orm
# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
+14 -1
View File
@@ -72,7 +72,10 @@
"apply": "apply",
"paste": "paste",
"logout": "sign out",
"upload": "upload"
"upload": "upload",
"download": "download",
"share": "share to market",
"like": "like"
},
"str": {
"null": "Null",
@@ -146,6 +149,11 @@
"upload": {
"success": "Upload success ~",
"failed": "Upload failed.."
},
"share": {
"title": "Are you sure to sharing?",
"description": "Other users can see this scheme and download it after share",
"success": "Share success"
}
},
"me": {
@@ -155,6 +163,11 @@
"email_hint": "Please enter email",
"password_hint": "Please enter 8-16-bit password",
"email_error_hint": "Please enter your vaild email"
},
"scheme_types": {
"uploaded": "Uploaded",
"downloaded": "Downloaded",
"liked": "Liked"
}
}
}
+14 -1
View File
@@ -72,7 +72,10 @@
"apply": "应用",
"paste": "粘贴",
"logout": "退出登录",
"upload": "上传"
"upload": "上传",
"download": "下载",
"share": "分享到市场",
"like": "点赞"
},
"str": {
"null": "无",
@@ -146,6 +149,11 @@
"upload": {
"success": "上传成功~",
"failed": "上传失败。。"
},
"share": {
"title": "确定分享?",
"description": "分享后其他用户可以看到本方案并下载使用",
"success": "分享成功"
}
},
"me": {
@@ -155,6 +163,11 @@
"email_hint": "请输入邮箱",
"password_hint": "请输入8-16位密码",
"email_error_hint": "请输入正确的邮箱"
},
"scheme_types": {
"uploaded": "我的上传",
"downloaded": "我的下载",
"liked": "我的点赞"
}
}
}