Skip to content

Commit

Permalink
增加了演示模式
Browse files Browse the repository at this point in the history
  • Loading branch information
mazj committed Jan 23, 2024
1 parent 37527e8 commit ee9fd15
Show file tree
Hide file tree
Showing 11 changed files with 177 additions and 42 deletions.
18 changes: 17 additions & 1 deletion .github/workflows/dart.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ jobs:
- name: 'Get packages'
run: 'flutter pub get'
# Get the Flutter packages.

- name: Run Build Runner
run: flutter pub run build_runner build --delete-conflicting-outputs

- name: 'Build APK'
id: 'build'
Expand All @@ -54,4 +57,17 @@ jobs:
prerelease: false
files: |
build/app/outputs/flutter-apk/*.apk
build/app/outputs/flutter-apk/*.apk.sha1
build/app/outputs/flutter-apk/*.apk.sha1
- name: Notice to Slack
id: slack
uses: slackapi/slack-github-action@v1.18.0
with:
payload: |
{
"text": "ScreenMe Flutter Project CI/CD Build ${{ job.status }} <${{ github.event.pull_request.html_url || github.event.head_commit.url }}| HERE>"
}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK

5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,7 @@ app.*.map.json
.dart_tool
.idea
.flutter-plugins
.flutter-plugins-dependencies
.flutter-plugins-dependencies

*.g.dart
*.freezed.dart
11 changes: 8 additions & 3 deletions lib/api/common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class Config with _$Config {
@Default("") String password,
@Default(60) int fetchSeconds,
@Default(false) bool showBingWallpaper,
@Default("") String cyberPass}) = _Config;
@Default("") String cyberPass,
@Default(true) bool demoMode}) = _Config;

factory Config.fromJson(Map<String, dynamic> json) => _$ConfigFromJson(json);
}
Expand All @@ -38,6 +39,8 @@ extension ConfigHelper on Config {
'Authorization': cyberBase64Token,
'Content-Type': 'application/json'
};

int get changeSeconds => demoMode ? 10 : fetchSeconds;
}

@riverpod
Expand All @@ -49,13 +52,15 @@ class Configs extends _$Configs {
return data;
}

set(String user, String pass, int duration, bool showWallpaper) async {
set(String user, String pass, int duration, bool showWallpaper,
{bool demoMode = false}) async {
final c = Config(
user: user,
password: pass,
cyberPass: encryptPassword(pass, 60 * 60 * 24 * 30),
fetchSeconds: duration,
showBingWallpaper: showWallpaper);
showBingWallpaper: showWallpaper,
demoMode: demoMode);
final s = await SharedPreferences.getInstance();
await s.setString("config", jsonEncode(c.toJson()));
data = c;
Expand Down
41 changes: 32 additions & 9 deletions lib/api/common.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ mixin _$Config {
int get fetchSeconds => throw _privateConstructorUsedError;
bool get showBingWallpaper => throw _privateConstructorUsedError;
String get cyberPass => throw _privateConstructorUsedError;
bool get demoMode => throw _privateConstructorUsedError;

Map<String, dynamic> toJson() => throw _privateConstructorUsedError;
@JsonKey(ignore: true)
Expand All @@ -41,7 +42,8 @@ abstract class $ConfigCopyWith<$Res> {
String password,
int fetchSeconds,
bool showBingWallpaper,
String cyberPass});
String cyberPass,
bool demoMode});
}

/// @nodoc
Expand All @@ -62,6 +64,7 @@ class _$ConfigCopyWithImpl<$Res, $Val extends Config>
Object? fetchSeconds = null,
Object? showBingWallpaper = null,
Object? cyberPass = null,
Object? demoMode = null,
}) {
return _then(_value.copyWith(
user: null == user
Expand All @@ -84,6 +87,10 @@ class _$ConfigCopyWithImpl<$Res, $Val extends Config>
? _value.cyberPass
: cyberPass // ignore: cast_nullable_to_non_nullable
as String,
demoMode: null == demoMode
? _value.demoMode
: demoMode // ignore: cast_nullable_to_non_nullable
as bool,
) as $Val);
}
}
Expand All @@ -100,7 +107,8 @@ abstract class _$$ConfigImplCopyWith<$Res> implements $ConfigCopyWith<$Res> {
String password,
int fetchSeconds,
bool showBingWallpaper,
String cyberPass});
String cyberPass,
bool demoMode});
}

/// @nodoc
Expand All @@ -119,6 +127,7 @@ class __$$ConfigImplCopyWithImpl<$Res>
Object? fetchSeconds = null,
Object? showBingWallpaper = null,
Object? cyberPass = null,
Object? demoMode = null,
}) {
return _then(_$ConfigImpl(
user: null == user
Expand All @@ -141,6 +150,10 @@ class __$$ConfigImplCopyWithImpl<$Res>
? _value.cyberPass
: cyberPass // ignore: cast_nullable_to_non_nullable
as String,
demoMode: null == demoMode
? _value.demoMode
: demoMode // ignore: cast_nullable_to_non_nullable
as bool,
));
}
}
Expand All @@ -153,7 +166,8 @@ class _$ConfigImpl with DiagnosticableTreeMixin implements _Config {
this.password = "",
this.fetchSeconds = 60,
this.showBingWallpaper = false,
this.cyberPass = ""});
this.cyberPass = "",
this.demoMode = true});

factory _$ConfigImpl.fromJson(Map<String, dynamic> json) =>
_$$ConfigImplFromJson(json);
Expand All @@ -173,10 +187,13 @@ class _$ConfigImpl with DiagnosticableTreeMixin implements _Config {
@override
@JsonKey()
final String cyberPass;
@override
@JsonKey()
final bool demoMode;

@override
String toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) {
return 'Config(user: $user, password: $password, fetchSeconds: $fetchSeconds, showBingWallpaper: $showBingWallpaper, cyberPass: $cyberPass)';
return 'Config(user: $user, password: $password, fetchSeconds: $fetchSeconds, showBingWallpaper: $showBingWallpaper, cyberPass: $cyberPass, demoMode: $demoMode)';
}

@override
Expand All @@ -188,7 +205,8 @@ class _$ConfigImpl with DiagnosticableTreeMixin implements _Config {
..add(DiagnosticsProperty('password', password))
..add(DiagnosticsProperty('fetchSeconds', fetchSeconds))
..add(DiagnosticsProperty('showBingWallpaper', showBingWallpaper))
..add(DiagnosticsProperty('cyberPass', cyberPass));
..add(DiagnosticsProperty('cyberPass', cyberPass))
..add(DiagnosticsProperty('demoMode', demoMode));
}

@override
Expand All @@ -204,13 +222,15 @@ class _$ConfigImpl with DiagnosticableTreeMixin implements _Config {
(identical(other.showBingWallpaper, showBingWallpaper) ||
other.showBingWallpaper == showBingWallpaper) &&
(identical(other.cyberPass, cyberPass) ||
other.cyberPass == cyberPass));
other.cyberPass == cyberPass) &&
(identical(other.demoMode, demoMode) ||
other.demoMode == demoMode));
}

@JsonKey(ignore: true)
@override
int get hashCode => Object.hash(
runtimeType, user, password, fetchSeconds, showBingWallpaper, cyberPass);
int get hashCode => Object.hash(runtimeType, user, password, fetchSeconds,
showBingWallpaper, cyberPass, demoMode);

@JsonKey(ignore: true)
@override
Expand All @@ -232,7 +252,8 @@ abstract class _Config implements Config {
final String password,
final int fetchSeconds,
final bool showBingWallpaper,
final String cyberPass}) = _$ConfigImpl;
final String cyberPass,
final bool demoMode}) = _$ConfigImpl;

factory _Config.fromJson(Map<String, dynamic> json) = _$ConfigImpl.fromJson;

Expand All @@ -247,6 +268,8 @@ abstract class _Config implements Config {
@override
String get cyberPass;
@override
bool get demoMode;
@override
@JsonKey(ignore: true)
_$$ConfigImplCopyWith<_$ConfigImpl> get copyWith =>
throw _privateConstructorUsedError;
Expand Down
4 changes: 3 additions & 1 deletion lib/api/common.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 31 additions & 5 deletions lib/api/dash.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// ignore_for_file: invalid_annotation_target

import 'dart:math';

import 'package:flutter/material.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
Expand Down Expand Up @@ -69,6 +71,7 @@ class DashInfo with _$DashInfo {
@Default(DashTemp()) DashTemp tempInfo,
@Default(DashTemp()) DashTemp tempFutureInfo,
@Default(DashFit()) DashFit fitnessInfo,
@Default("") String debugInfo,
@Default("") String lastError}) = _DashInfo;

factory DashInfo.fromJson(Map<String, dynamic> json) =>
Expand All @@ -77,20 +80,43 @@ class DashInfo with _$DashInfo {

@riverpod
Future<DashInfo> getDash(GetDashRef ref) async {
if (ref.read(configsProvider).value?.demoMode ?? false) {
return fakeDashInfo();
}
debugPrint("req for dash");
try {
final (res, d) =
await requestFromRaw("/cyber/client/ios-widget", DashInfo.fromJson);
return res?.copyWith(
lastError:
debugInfo:
"[normal] ver: $version, req: ${Configs.data.copyWith(password: "***")}") ??
DashInfo(
lastError:
"[empty] ver: $version, req: ${Configs.data.copyWith(password: "***")}, origin or err $d");
debugInfo:
"[empty] ver: $version, req: ${Configs.data.copyWith(password: "***")}, origin or err $d",
lastError: d);
} catch (e, st) {
debugPrintStack(stackTrace: st);
return DashInfo(
lastError:
"[error] ver: $version, error: $e, stack: ${st.toString()}, req ${Configs.data.copyWith(password: "***")}");
debugInfo:
"[error] ver: $version, error: $e, stack: ${st.toString()}, req ${Configs.data.copyWith(password: "***")}",
lastError: e.toString());
}
}

DashInfo fakeDashInfo() {
return DashInfo(
weatherInfo: Random().nextBool() ? "今天的天气晴朗,平均气温 23°" : "最近的降雨带在 20 公里以外",
todo: [
const DashTodo(title: "阅读《小王子》"),
const DashTodo(title: "双击时钟打开设置界面"),
DashTodo(title: "取快递", isFinished: Random().nextBool())
],
offWork: Random().nextBool(),
workStatus: (["🔴", "🟡", "🟢", "⚫", "⚪"]..shuffle()).first,
fitnessInfo: DashFit(
exercise: Random().nextDouble() * 10 + 10,
active: Random().nextDouble() * 80 + 200,
mindful: Random().nextInt(3) + 1),
debugInfo:
"[demo] ver: $version, req: ${Configs.data.copyWith(password: "***", cyberPass: "***")}");
}

0 comments on commit ee9fd15

Please sign in to comment.