Skip to content

Commit

Permalink
add windows support, add webp image support
Browse files Browse the repository at this point in the history
  • Loading branch information
JerryFans committed Jan 9, 2022
1 parent 50e5d33 commit 529fe9a
Show file tree
Hide file tree
Showing 33 changed files with 2,777 additions and 27 deletions.
7 changes: 7 additions & 0 deletions lib/Controller/path_provider_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,11 @@ class PathProviderUtil {
}
return PathProviderPlatform.instance;
}

static String platformDirectoryLine() {
if (Platform.isWindows) {
return "\\";
}
return "/";
}
}
15 changes: 10 additions & 5 deletions lib/Controller/tiny_image_info_controller.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import 'dart:ffi';

import 'package:TinyPNG4Flutter/Controller/path_provider_util.dart';
import 'package:TinyPNG4Flutter/Model/tiny_image_info.dart';
import 'package:tiny_png4_flutter/Controller/path_provider_util.dart';
import 'package:tiny_png4_flutter/Model/tiny_image_info.dart';
import 'package:get/get_state_manager/get_state_manager.dart';
import 'package:TinyPNG4Flutter/Model/tiny_image_info_item_view_model.dart';
import 'package:tiny_png4_flutter/Model/tiny_image_info_item_view_model.dart';
import 'package:get/get.dart';
import 'dart:convert';
import 'dart:io';
Expand Down Expand Up @@ -139,6 +139,11 @@ class TinyImageInfoController extends GetxController {
return pre.getString(KApiKey) != null;
}

Future<bool> checkHaveSavePath() async {
var pre = await SharedPreferences.getInstance();
return pre.getString(KSavePathKey) != null;
}

Future<bool> downloadOutputImage(TinyImageInfo imageInfo, String savePath,
{Function(int count, int total)? onReceiveProgress}) async {
String? url = imageInfo.output?.url;
Expand Down Expand Up @@ -168,13 +173,13 @@ class TinyImageInfoController extends GetxController {
Future<File?> createFile(String path, String fileName) async {
try {
bool isExist = true;
var filePath = path + "/" + fileName;
var filePath = path + PathProviderUtil.platformDirectoryLine() + fileName;
var count = 0;
while (true) {
if (count > 0) {
var onlyName = fileName.split(".").first;
var type = fileName.split(".").last;
filePath = path + "/" + onlyName + "_$count" + "." + type;
filePath = path + PathProviderUtil.platformDirectoryLine() + onlyName + "_$count" + "." + type;
}
isExist = await File(filePath).exists();
print("try create path $filePath isExist $isExist");
Expand Down
7 changes: 3 additions & 4 deletions lib/Model/tiny_image_info_item_view_model.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import 'dart:io';
import 'dart:ui';

import 'package:flutter/material.dart';

import 'tiny_image_info.dart';
import 'package:tiny_png4_flutter/Controller/path_provider_util.dart';

extension FileExtention on FileSystemEntity {
String get fileName {
return this.path.split("/").last;
print("my file path is ${this.path}");
return this.path.split(PathProviderUtil.platformDirectoryLine()).last;
}
}

Expand Down
10 changes: 7 additions & 3 deletions lib/View/bottom_setting_view.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import 'dart:io';

import 'package:TinyPNG4Flutter/Controller/const_util.dart';
import 'package:TinyPNG4Flutter/Controller/tiny_image_info_controller.dart';
import 'package:tiny_png4_flutter/Controller/const_util.dart';
import 'package:tiny_png4_flutter/Controller/tiny_image_info_controller.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:oktoast/oktoast.dart';
import 'package:shared_preferences/shared_preferences.dart';
Expand Down Expand Up @@ -73,7 +72,11 @@ class BottomSettingView extends StatelessWidget {
),
TextButton(
onPressed: () {
if (Platform.isMacOS) {
Process.run("open", ["https://tinypng.com/developers"]);
} else if (Platform.isWindows) {
Process.run("explorer", ["https://tinypng.com/developers"]);
}
},
child: Text(
"Get your API key",
Expand Down Expand Up @@ -106,6 +109,7 @@ class BottomSettingView extends StatelessWidget {
cursorHeight: 10,
cursorColor: Colors.black,
decoration: InputDecoration(
hintText: "choose your output path",
focusedBorder: OutlineInputBorder(
borderSide:
const BorderSide(color: Colors.black)),
Expand Down
4 changes: 2 additions & 2 deletions lib/View/image_task_cell.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:TinyPNG4Flutter/ImagesAnim.dart';
import 'package:TinyPNG4Flutter/Model/tiny_image_info_item_view_model.dart';
import 'package:tiny_png4_flutter/ImagesAnim.dart';
import 'package:tiny_png4_flutter/Model/tiny_image_info_item_view_model.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

Expand Down
37 changes: 26 additions & 11 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'dart:io';
import 'package:TinyPNG4Flutter/Controller/const_util.dart';
import 'package:TinyPNG4Flutter/Controller/path_provider_util.dart';
import 'package:TinyPNG4Flutter/Controller/tiny_image_info_controller.dart';
import 'package:TinyPNG4Flutter/View/image_task_cell.dart';
import 'package:tiny_png4_flutter/Controller/const_util.dart';
import 'package:tiny_png4_flutter/Controller/path_provider_util.dart';
import 'package:tiny_png4_flutter/Controller/tiny_image_info_controller.dart';
import 'package:tiny_png4_flutter/View/image_task_cell.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:get/get_state_manager/get_state_manager.dart';
Expand All @@ -26,7 +26,7 @@ class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'TinyPNG4Flutter',
title: 'tiny_png4_flutter',
theme: ThemeData(),
home: MyHomePage(title: 'TinyPNGFlutter'),
);
Expand All @@ -51,11 +51,15 @@ class _MyHomePageState extends State<MyHomePage> {
SharedPreferences.getInstance().then((pre) async {
var savePath = pre.getString(KSavePathKey);
if (savePath == null || savePath.isEmpty) {
var provider = PathProviderUtil.provider();
String? path = await provider.getDownloadsPath();
if (path == null) return;
final filePath = path + "/" + "tinyPngFlutterOutput";
pre.setString(KSavePathKey, filePath);
try {
var provider = PathProviderUtil.provider();
String? path = await provider.getDownloadsPath();
if (path == null) return;
final filePath = path + PathProviderUtil.platformDirectoryLine() + "tinyPngFlutterOutput";
pre.setString(KSavePathKey, filePath);
} catch (e) {

}
}
});
}
Expand Down Expand Up @@ -84,7 +88,11 @@ class _MyHomePageState extends State<MyHomePage> {
if (savePath == null) return;
var checkCreate = await controller.createDirectory(savePath);
if (checkCreate != null) {
Process.run("open", [savePath]);
if (Platform.isMacOS) {
Process.run("open", [savePath]);
} else if (Platform.isWindows) {
Process.run("explorer", [savePath]);
}
}
},
tooltip: 'Open compress image folder',
Expand Down Expand Up @@ -162,6 +170,12 @@ class _MyHomePageState extends State<MyHomePage> {
textPadding: EdgeInsets.all(15));
return;
}
if (await controller.checkHaveSavePath() == false) {
_showSettingBottomSheet();
showToast("Please choose your output path",
textPadding: EdgeInsets.all(15));
return;
}
FilePickerResult? result =
await FilePicker.platform.pickFiles(allowMultiple: true);
if (result != null) {
Expand All @@ -170,6 +184,7 @@ class _MyHomePageState extends State<MyHomePage> {
files.forEach((element) {
if (element.path.toLowerCase().endsWith("jpg") ||
element.path.toLowerCase().endsWith("jpeg") ||
element.path.toLowerCase().endsWith("webp") ||
element.path.toLowerCase().endsWith("png")) {
chooseFiles.add(element);
} else {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: TinyPNG4Flutter
name: tiny_png4_flutter
description: A new Flutter project.

# The following line prevents the package from being accidentally published to
Expand Down
2 changes: 1 addition & 1 deletion test/widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

import 'package:TinyPNG4Flutter/main.dart';
import 'package:tiny_png4_flutter/main.dart';

void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
Expand Down
17 changes: 17 additions & 0 deletions windows/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
flutter/ephemeral/

# Visual Studio user-specific files.
*.suo
*.user
*.userosscache
*.sln.docstates

# Visual Studio build-related files.
x64/
x86/

# Visual Studio cache files
# files ending in .cache can be ignored
*.[Cc]ache
# but keep track of directories ending in .cache
!*.[Cc]ache/
95 changes: 95 additions & 0 deletions windows/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
cmake_minimum_required(VERSION 3.15)
project(tiny_png4_flutter LANGUAGES CXX)

set(BINARY_NAME "tiny_png4_flutter")

cmake_policy(SET CMP0063 NEW)

set(CMAKE_INSTALL_RPATH "$ORIGIN/lib")

# Configure build options.
get_property(IS_MULTICONFIG GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
if(IS_MULTICONFIG)
set(CMAKE_CONFIGURATION_TYPES "Debug;Profile;Release"
CACHE STRING "" FORCE)
else()
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
set(CMAKE_BUILD_TYPE "Debug" CACHE
STRING "Flutter build mode" FORCE)
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS
"Debug" "Profile" "Release")
endif()
endif()

set(CMAKE_EXE_LINKER_FLAGS_PROFILE "${CMAKE_EXE_LINKER_FLAGS_RELEASE}")
set(CMAKE_SHARED_LINKER_FLAGS_PROFILE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE}")
set(CMAKE_C_FLAGS_PROFILE "${CMAKE_C_FLAGS_RELEASE}")
set(CMAKE_CXX_FLAGS_PROFILE "${CMAKE_CXX_FLAGS_RELEASE}")

# Use Unicode for all projects.
add_definitions(-DUNICODE -D_UNICODE)

# Compilation settings that should be applied to most targets.
function(APPLY_STANDARD_SETTINGS TARGET)
target_compile_features(${TARGET} PUBLIC cxx_std_17)
target_compile_options(${TARGET} PRIVATE /W4 /WX /wd"4100")
target_compile_options(${TARGET} PRIVATE /EHsc)
target_compile_definitions(${TARGET} PRIVATE "_HAS_EXCEPTIONS=0")
target_compile_definitions(${TARGET} PRIVATE "$<$<CONFIG:Debug>:_DEBUG>")
endfunction()

set(FLUTTER_MANAGED_DIR "${CMAKE_CURRENT_SOURCE_DIR}/flutter")

# Flutter library and tool build rules.
add_subdirectory(${FLUTTER_MANAGED_DIR})

# Application build
add_subdirectory("runner")

# Generated plugin build rules, which manage building the plugins and adding
# them to the application.
include(flutter/generated_plugins.cmake)


# === Installation ===
# Support files are copied into place next to the executable, so that it can
# run in place. This is done instead of making a separate bundle (as on Linux)
# so that building and running from within Visual Studio will work.
set(BUILD_BUNDLE_DIR "$<TARGET_FILE_DIR:${BINARY_NAME}>")
# Make the "install" step default, as it's required to run.
set(CMAKE_VS_INCLUDE_INSTALL_TO_DEFAULT_BUILD 1)
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
set(CMAKE_INSTALL_PREFIX "${BUILD_BUNDLE_DIR}" CACHE PATH "..." FORCE)
endif()

set(INSTALL_BUNDLE_DATA_DIR "${CMAKE_INSTALL_PREFIX}/data")
set(INSTALL_BUNDLE_LIB_DIR "${CMAKE_INSTALL_PREFIX}")

install(TARGETS ${BINARY_NAME} RUNTIME DESTINATION "${CMAKE_INSTALL_PREFIX}"
COMPONENT Runtime)

install(FILES "${FLUTTER_ICU_DATA_FILE}" DESTINATION "${INSTALL_BUNDLE_DATA_DIR}"
COMPONENT Runtime)

install(FILES "${FLUTTER_LIBRARY}" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}"
COMPONENT Runtime)

if(PLUGIN_BUNDLED_LIBRARIES)
install(FILES "${PLUGIN_BUNDLED_LIBRARIES}"
DESTINATION "${INSTALL_BUNDLE_LIB_DIR}"
COMPONENT Runtime)
endif()

# Fully re-copy the assets directory on each build to avoid having stale files
# from a previous install.
set(FLUTTER_ASSET_DIR_NAME "flutter_assets")
install(CODE "
file(REMOVE_RECURSE \"${INSTALL_BUNDLE_DATA_DIR}/${FLUTTER_ASSET_DIR_NAME}\")
" COMPONENT Runtime)
install(DIRECTORY "${PROJECT_BUILD_DIR}/${FLUTTER_ASSET_DIR_NAME}"
DESTINATION "${INSTALL_BUNDLE_DATA_DIR}" COMPONENT Runtime)

# Install the AOT library on non-Debug builds only.
install(FILES "${AOT_LIBRARY}" DESTINATION "${INSTALL_BUNDLE_DATA_DIR}"
CONFIGURATIONS Profile;Release
COMPONENT Runtime)
Loading

0 comments on commit 529fe9a

Please sign in to comment.