import 'dart:convert'; import 'package:flutter/services.dart'; import 'package:pikapi/basic/Entities.dart'; import 'package:pikapi/basic/config/Quality.dart'; final method = Method._(); class Method { Method._(); MethodChannel _channel = MethodChannel("method"); Future _flatInvoke(String method, dynamic params) { return _channel.invokeMethod("flatInvoke", { "method": method, "params": params is String ? params : jsonEncode(params), }); } Future loadTheme() async { return await _flatInvoke("loadProperty", { "name": "theme", "defaultValue": "pink", }); } Future saveTheme(String code) async { return await _flatInvoke("saveProperty", { "name": "theme", "value": code, }); } Future loadProperty(String propertyName, String defaultValue) async { return await _flatInvoke("loadProperty", { "name": propertyName, "defaultValue": defaultValue, }); } Future saveProperty(String propertyName, String value) { return _flatInvoke("saveProperty", { "name": propertyName, "value": value, }); } Future loadQuality() async { return await _flatInvoke("loadProperty", { "name": "quality", "defaultValue": ImageQualityOriginal, }); } Future saveQuality(String code) async { return await _flatInvoke("saveProperty", { "name": "quality", "value": code, }); } Future getSwitchAddress() async { return await _flatInvoke("getSwitchAddress", ""); } Future setSwitchAddress(String switchAddress) async { return await _flatInvoke("setSwitchAddress", switchAddress); } Future getProxy() async { return await _flatInvoke("getProxy", ""); } Future setProxy(String proxy) async { return await _flatInvoke("setProxy", proxy); } Future getUsername() async { return await _flatInvoke("getUsername", ""); } Future setUsername(String username) async { return await _flatInvoke("setUsername", username); } Future getPassword() async { return await _flatInvoke("getPassword", ""); } Future setPassword(String password) async { return await _flatInvoke("setPassword", password); } Future preLogin() async { String rsp = await _flatInvoke("preLogin", ""); return rsp == "true"; } Future login() async { return _flatInvoke("login", ""); } Future register( String email, String name, String password, String gender, String birthday, String question1, String answer1, String question2, String answer2, String question3, String answer3) { return _flatInvoke("register", { "email": email, "name": name, "password": password, "gender": gender, "birthday": birthday, "question1": question1, "answer1": answer1, "question2": question2, "answer2": answer2, "question3": question3, "answer3": answer3, }); } Future clearToken() { return _flatInvoke("clearToken", ""); } Future userProfile() async { String rsp = await _flatInvoke("userProfile", ""); return UserProfile.fromJson(json.decode(rsp)); } Future punchIn() { return _flatInvoke("punchIn", ""); } Future remoteImageData( String fileServer, String path) async { var data = await _flatInvoke("remoteImageData", { "fileServer": fileServer, "path": path, }); return RemoteImageData.fromJson(json.decode(data)); } Future remoteImagePreload(String fileServer, String path) async { return _flatInvoke("remoteImagePreload", { "fileServer": fileServer, "path": path, }); } Future downloadImagePath(String path) async { return await _flatInvoke("downloadImagePath", path); } Future> categories() async { String rsp = await _flatInvoke("categories", ""); List list = json.decode(rsp); return list.map((e) => Category.fromJson(e)).toList(); } Future comics( String sort, int page, { String category = "", String tag = "", String creatorId = "", String chineseTeam = "", }) async { String rsp = await _flatInvoke("comics", { "category": category, "tag": tag, "creatorId": creatorId, "chineseTeam": chineseTeam, "sort": sort, "page": page, }); return ComicsPage.fromJson(json.decode(rsp)); } Future searchComics(String keyword, String sort, int page) { return searchComicsInCategories(keyword, sort, page, []); } Future searchComicsInCategories( String keyword, String sort, int page, List categories) async { String rsp = await _flatInvoke("searchComics", { "keyword": keyword, "sort": sort, "page": page, "categories": categories, }); return ComicsPage.fromJson(json.decode(rsp)); } Future> randomComics() async { String data = await _flatInvoke("randomComics", ""); return List.of(jsonDecode(data)) .map((e) => Map.of(e)) .map((e) => ComicSimple.fromJson(e)) .toList(); } Future> leaderboard(String type) async { String data = await _flatInvoke("leaderboard", type); return List.of(jsonDecode(data)) .map((e) => Map.of(e)) .map((e) => ComicSimple.fromJson(e)) .toList(); } Future comicInfo(String comicId) async { String rsp = await _flatInvoke("comicInfo", comicId); return ComicInfo.fromJson(json.decode(rsp)); } Future comicEpPage(String comicId, int page) async { String rsp = await _flatInvoke("comicEpPage", { "comicId": comicId, "page": page, }); return EpPage.fromJson(json.decode(rsp)); } Future comicPicturePageWithQuality( String comicId, int epOrder, int page, String quality) async { String data = await _flatInvoke("comicPicturePageWithQuality", { "comicId": comicId, "epOrder": epOrder, "page": page, "quality": quality, }); return PicturePage.fromJson(json.decode(data)); } Future switchLike(String comicId) async { return await _flatInvoke("switchLike", comicId); } Future switchFavourite(String comicId) async { return await _flatInvoke("switchFavourite", comicId); } Future favouriteComics(String sort, int page) async { var rsp = await _flatInvoke("favouriteComics", { "sort": sort, "page": page, }); return ComicsPage.fromJson(json.decode(rsp)); } Future> recommendation(String comicId) async { String rsp = await _flatInvoke("recommendation", comicId); List list = json.decode(rsp); return list.map((e) => ComicSimple.fromJson(e)).toList(); } Future postComment(String comicId, String content) { return _flatInvoke("postComment", { "comicId": comicId, "content": content, }); } Future postChildComment(String commentId, String content) { return _flatInvoke("postChildComment", { "commentId": commentId, "content": content, }); } Future comments(String comicId, int page) async { var rsp = await _flatInvoke("comments", { "comicId": comicId, "page": page, }); return CommentPage.fromJson(json.decode(rsp)); } Future commentChildren( String comicId, String commentId, int page, ) async { var rsp = await _flatInvoke("commentChildren", { "comicId": comicId, "commentId": commentId, "page": page, }); return CommentChildrenPage.fromJson(json.decode(rsp)); } Future myComments(int page) async { String response = await _flatInvoke("myComments", "$page"); print("RESPONSE"); print(response); return MyCommentsPage.fromJson(jsonDecode(response)); } Future> viewLogPage(int offset, int limit) async { var data = await _flatInvoke("viewLogPage", { "offset": offset, "limit": limit, }); List list = json.decode(data); return list.map((e) => ViewLog.fromJson(e)).toList(); } Future clearAllViewLog() { return _flatInvoke("clearAllViewLog", ""); } Future deleteViewLog(String id) { return _flatInvoke("deleteViewLog", id); } Future games(int page) async { var data = await _flatInvoke("games", "$page"); return GamePage.fromJson(json.decode(data)); } Future game(String gameId) async { var data = await _flatInvoke("game", gameId); return GameInfo.fromJson(json.decode(data)); } Future clean() { return _flatInvoke("clean", ""); } Future autoClean(String expireSec) { return _flatInvoke("autoClean", expireSec); } Future storeViewEp( String comicId, int epOrder, String epTitle, int pictureRank) { return _flatInvoke("storeViewEp", { "comicId": comicId, "epOrder": epOrder, "epTitle": epTitle, "pictureRank": pictureRank, }); } Future loadView(String comicId) async { String data = await _flatInvoke("loadView", comicId); if (data == "") { return null; } return ViewLog.fromJson(jsonDecode(data)); } Future downloadRunning() async { String rsp = await _flatInvoke("downloadRunning", ""); return rsp == "true"; } Future setDownloadRunning(bool status) async { return _flatInvoke("setDownloadRunning", "$status"); } Future createDownload( Map comic, List> epList) async { return _flatInvoke("createDownload", { "comic": comic, "epList": epList, }); } Future addDownload( Map comic, List> epList) async { await _flatInvoke("addDownload", { "comic": comic, "epList": epList, }); } Future loadDownloadComic(String comicId) async { var data = await _flatInvoke("loadDownloadComic", comicId); // 未找到 且 未异常 if (data == "") { return null; } return DownloadComic.fromJson(json.decode(data)); } Future> allDownloads() async { var data = await _flatInvoke("allDownloads", ""); data = jsonDecode(data); if (data == null) { return []; } List list = data; return list.map((e) => DownloadComic.fromJson(e)).toList(); } Future deleteDownloadComic(String comicId) async { return _flatInvoke("deleteDownloadComic", comicId); } Future> downloadEpList(String comicId) async { var data = await _flatInvoke("downloadEpList", comicId); List list = json.decode(data); return list.map((e) => DownloadEp.fromJson(e)).toList(); } Future> downloadPicturesByEpId(String epId) async { var data = await _flatInvoke("downloadPicturesByEpId", epId); List list = json.decode(data); return list.map((e) => DownloadPicture.fromJson(e)).toList(); } Future resetFailed() async { return _flatInvoke("resetAllDownloads", ""); } Future exportComicDownload(String comicId, String dir) { return _flatInvoke("exportComicDownload", { "comicId": comicId, "dir": dir, }); } Future exportComicDownloadToJPG(String comicId, String dir) { return _flatInvoke("exportComicDownloadToJPG", { "comicId": comicId, "dir": dir, }); } Future exportComicUsingSocket(String comicId) async { return int.parse(await _flatInvoke("exportComicUsingSocket", comicId)); } Future exportComicUsingSocketExit() { return _flatInvoke("exportComicUsingSocketExit", ""); } Future importComicDownload(String zipPath) { return _flatInvoke("importComicDownload", zipPath); } Future importComicDownloadUsingSocket(String addr) { return _flatInvoke("importComicDownloadUsingSocket", addr); } Future clientIpSet() async { return await _flatInvoke("clientIpSet", ""); } Future> downloadGame(String url) async { if (url.startsWith("https://game.eroge.xyz/hhh.php")) { var data = await _flatInvoke("downloadGame", url); return List.of(jsonDecode(data)).map((e) => e.toString()).toList(); } return [url]; } Future iosSaveFileToImage(String path) async { return _channel.invokeMethod("iosSaveFileToImage", { "path": path, }); } Future androidSaveFileToImage(String path) async { return _channel.invokeMethod("androidSaveFileToImage", { "path": path, }); } Future convertImageToJPEG100(String path, String dir) async { return _flatInvoke("convertImageToJPEG100", { "path": path, "dir": dir, }); } Future getAutoFullScreen() async { var value = await _flatInvoke("loadProperty", { "name": "autoFullScreen", "defaultValue": "false", }); return value == "true"; } Future setAutoFullScreen(bool value) async { return await _flatInvoke("saveProperty", { "name": "autoFullScreen", "value": "$value", }); } Future> getShadowCategories() async { var value = await _flatInvoke("loadProperty", { "name": "shadowCategories", "defaultValue": jsonEncode([]), }); return List.of(jsonDecode(value)).map((e) => "$e").toList(); } Future setShadowCategories(List value) { return _flatInvoke("saveProperty", { "name": "shadowCategories", "value": jsonEncode(value), }); } Future> loadAndroidModes() async { return List.of(await _channel.invokeMethod("androidGetModes")) .map((e) => "$e") .toList(); } Future setAndroidMode(String androidDisplayMode) { return _channel .invokeMethod("androidSetMode", {"mode": androidDisplayMode}); } Future androidGetUiMode() { return _channel.invokeMethod("androidGetUiMode", {}); } Future androidGetVersion() async { return await _channel.invokeMethod("androidGetVersion", {}); } }