Skip to content

Commit

Permalink
Merge pull request #10 from chimon89/master
Browse files Browse the repository at this point in the history
Add Windows support
  • Loading branch information
神楽坂花火 authored Apr 15, 2021
2 parents cfea86e + ac57566 commit 3505000
Show file tree
Hide file tree
Showing 37 changed files with 1,446 additions and 145 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@
.atom/
.buildlog/
.history
.idea/
.svn/
.vscode/
test/

# IntelliJ related
*.iml
*.ipr
*.iws
**/.idea/caches
**/.idea/libraries
**/.idea/misc.xml
**/.idea/modules.xml
**/.idea/workspace.xml
**/.idea/navEditor.xml
Expand Down
5 changes: 5 additions & 0 deletions .idea/dictionaries/KagurazakaHanabi.xml

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

1 change: 1 addition & 0 deletions android/.idea/gradle.xml

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

5 changes: 3 additions & 2 deletions lib/components/today.dart
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,9 @@ class _TodayComponentState extends State<TodayComponent> with AutomaticKeepAlive
}

Future<void> _fetchText() async {
String url = 'https://v1.hitokoto.cn/?encode=text';
String source = (await http.get(url)).body;
String url = 'https://cloudgw.api.dailypics.cn/release/tu_hitokoto';
String body = (await http.get(url)).body;
String source = Hitokoto.fromJson(jsonDecode(body)).content;
if (mounted) setState(() => text = source);
}

Expand Down
16 changes: 16 additions & 0 deletions lib/misc/bean.dart
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,22 @@ class Splash {
static DateTime _parseDateTime(String s) => DateTime.parse(s);
}

@JsonSerializable()
class Hitokoto {
Hitokoto({this.source, this.content});

factory Hitokoto.fromJson(Map<String, dynamic> json) {
return _$HitokotoFromJson(json);
}

String source;

@JsonKey(name: 'hitokoto')
String content;

String toJson() => jsonEncode(_$HitokotoToJson(this));
}

@JsonSerializable()
class Contributor {
Contributor({this.avatar, this.name, this.position, this.url});
Expand Down
12 changes: 12 additions & 0 deletions lib/misc/bean.g.dart

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

4 changes: 2 additions & 2 deletions lib/pages/about.dart
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class _AboutPageState extends State<AboutPage> {
member.position,
style: TextStyle(
fontSize: 14,
color: CupertinoDynamicColor.withBrightness(
color: const CupertinoDynamicColor.withBrightness(
color: Colors.black54,
darkColor: Colors.white70,
).resolveFrom(context),
Expand Down Expand Up @@ -187,7 +187,7 @@ class _AboutPageState extends State<AboutPage> {
Widget _buildAppInfo() {
final String version = packageInfo?.version ?? '0.0.0';
final String buildNumber = packageInfo?.buildNumber ?? '190000';
Color textColor = CupertinoDynamicColor.withBrightness(
Color textColor = const CupertinoDynamicColor.withBrightness(
color: Colors.black54,
darkColor: Colors.white70,
).resolveFrom(context);
Expand Down
14 changes: 6 additions & 8 deletions lib/pages/details.dart
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ class _DetailsPageState extends State<DetailsPage> {
styleSheet: MarkdownStyleSheet(
a: const TextStyle(color: CupertinoColors.link),
p: textStyle.copyWith(
color: CupertinoDynamicColor.withBrightness(
color: const CupertinoDynamicColor.withBrightness(
color: Colors.black54,
darkColor: Colors.white70,
).resolveFrom(context),
Expand Down Expand Up @@ -290,7 +290,7 @@ class _DetailsPageState extends State<DetailsPage> {
}

Widget _buildDivider() {
Color dividerColor = CupertinoDynamicColor.withBrightness(
Color dividerColor = const CupertinoDynamicColor.withBrightness(
color: Colors.black38,
darkColor: Colors.white54,
).resolveFrom(context);
Expand All @@ -311,7 +311,7 @@ class _DetailsPageState extends State<DetailsPage> {
return DefaultTextStyle(
style: TextStyle(
fontSize: 14,
color: CupertinoDynamicColor.withBrightness(
color: const CupertinoDynamicColor.withBrightness(
color: Colors.black45,
darkColor: Colors.white60,
).resolveFrom(context),
Expand All @@ -336,7 +336,7 @@ class _DetailsPageState extends State<DetailsPage> {
}

Widget _buildShareButton() {
Color color = CupertinoDynamicColor.withBrightness(
Color color = const CupertinoDynamicColor.withBrightness(
color: CupertinoColors.systemBlue,
darkColor: Colors.white,
).resolveFrom(context);
Expand Down Expand Up @@ -515,9 +515,7 @@ class _DownloadButtonState extends State<_DownloadButton> {
SystemUtils.openAppSettings();
break;
case _DownloadState.successful:
if (Platform.isAndroid) {
SystemUtils.useAsWallpaper(file);
}
SystemUtils.useAsWallpaper(file);
break;
}
}
Expand All @@ -535,7 +533,7 @@ class _DownloadButtonState extends State<_DownloadButton> {
? '授权'
: (state == null || state == _DownloadState.pending)
? '获取'
: Platform.isAndroid ? '设定' : '完成',
: (Platform.isAndroid || Platform.isWindows) ? '设定' : '完成',
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.w500,
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class _HomePageState extends State<HomePage> {
}

BottomNavigationBarItem _buildNavigationItem(IconData icon, String title) {
return BottomNavigationBarItem(icon: Icon(icon), title: Text(title));
return BottomNavigationBarItem(icon: Icon(icon), label: title);
}

void _handleUniLink(Uri uri) {
Expand Down
10 changes: 6 additions & 4 deletions lib/pages/upload.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class _UploadPageState extends State<UploadPage> {
String type;
double progress = -1;

ImagePicker picker = ImagePicker();

@override
Widget build(BuildContext context) {
return CupertinoPageScaffold(
Expand Down Expand Up @@ -153,9 +155,9 @@ class _UploadPageState extends State<UploadPage> {
}
return GestureDetector(
onTap: () async {
File file = await ImagePicker.pickImage(source: ImageSource.gallery);
PickedFile file = await picker.getImage(source: ImageSource.gallery);
if (file != null) {
setState(() => imageFile = file);
setState(() => imageFile = File(file.path));
}
},
child: AspectRatio(
Expand Down Expand Up @@ -240,12 +242,12 @@ class _UploadPageState extends State<UploadPage> {
children: <Widget>[
CupertinoButton(
padding: EdgeInsets.zero,
child: Icon(Ionicons.ios_arrow_up),
child: const Icon(Ionicons.ios_arrow_up),
onPressed: () => FocusScope.of(context).previousFocus(),
),
CupertinoButton(
padding: EdgeInsets.zero,
child: Icon(Ionicons.ios_arrow_down),
child: const Icon(Ionicons.ios_arrow_down),
onPressed: () => FocusScope.of(context).nextFocus(),
),
Expanded(child: Container()),
Expand Down
4 changes: 3 additions & 1 deletion lib/pages/user_space.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class _UserSpacePageState extends State<UserSpacePage> {
ScrollController controller = ScrollController();
ScrollPosition position;

ImagePicker picker = ImagePicker();

@override
void initState() {
super.initState();
Expand Down Expand Up @@ -246,7 +248,7 @@ class _UserSpacePageState extends State<UserSpacePage> {
CupertinoActionSheetAction(
child: const Text('从相册选择'),
onPressed: () async => Navigator.of(context).pop(
await ImagePicker.pickImage(source: ImageSource.gallery),
await picker.getImage(source: ImageSource.gallery),
),
),
],
Expand Down
28 changes: 21 additions & 7 deletions lib/utils/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import 'dart:io';

import 'package:dailypics/misc/bean.dart';
import 'package:dailypics/utils/http.dart';
import 'package:dailypics/utils/windows.dart';
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
Expand All @@ -39,7 +40,18 @@ class SystemUtils {
}

static Future<void> useAsWallpaper(File file) async {
await _channel.invokeMethod('useAsWallpaper', file.path);
switch (Platform.operatingSystem) {
case "android":
await _channel.invokeMethod('useAsWallpaper', file.path);
break;
case "windows":
Windows.useAsWallpaper(file);
break;
}
}

static Future<void> useAsWallpaperForWindows(File file) async {

}

static Future<void> requestReview(bool inApp) async {
Expand Down Expand Up @@ -126,7 +138,7 @@ class DownloadManager {

Future<DownloadTask> runTask(Picture data, ValueNotifier<double> onProgress) async {
String url = data.url ?? data.cdnUrl;
String dest = (await getTemporaryDirectory()).path;
String dest = Platform.isWindows ? (await getDownloadsDirectory()).path : (await getTemporaryDirectory()).path;
File file;
String name;
if (url.contains('bing.com/')) {
Expand Down Expand Up @@ -156,11 +168,13 @@ class DownloadManager {
task.progress.value = count / total;
},
).then((value) async {
await _channel.invokeMethod('syncAlbum', {
'file': file.path,
'title': data.title,
'content': data.content,
});
if (!Platform.isWindows) {
await _channel.invokeMethod('syncAlbum', {
'file': file.path,
'title': data.title,
'content': data.content,
});
}
task.progress.value = -1;
_tasks.remove(task);
}, onError: (err) => _tasks.remove(task));
Expand Down
24 changes: 24 additions & 0 deletions lib/utils/windows.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import 'dart:ffi';
import 'dart:io';

import 'package:ffi/ffi.dart';
import 'package:win32/win32.dart';

class Windows {
static void useAsWallpaper(File wallpaperFile) {
final hr = CoInitializeEx(
nullptr, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
if (FAILED(hr)) throw WindowsException(hr);

final wallpaper = DesktopWallpaper.createInstance();

final pathPtr = TEXT(wallpaperFile.path);
wallpaper.SetWallpaper(nullptr, pathPtr);
if (FAILED(hr)) throw WindowsException(hr);

calloc.free(pathPtr);
calloc.free(wallpaper.ptr);

CoUninitialize();
}
}
2 changes: 1 addition & 1 deletion lib/widget/error.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class CustomErrorWidget extends StatelessWidget {
child: Text(
details.exceptionAsString(),
style: TextStyle(
color: CupertinoDynamicColor.withBrightness(
color: const CupertinoDynamicColor.withBrightness(
color: Colors.black54,
darkColor: Colors.white70,
).resolveFrom(context),
Expand Down
Loading

0 comments on commit 3505000

Please sign in to comment.