From c5ece3bf8859d99401c9713963072534855e25ad Mon Sep 17 00:00:00 2001 From: Aasim Khan Date: Wed, 10 May 2023 13:30:12 +0400 Subject: [PATCH 1/5] Added `secure-storage` in the framework and composed `MiraiAction` to support storage --- .../assets/json/home_screen.json | 32 +++ .../assets/json/storage_example.json | 106 ++++++++++ .../mirai_gallery/ios/Flutter/Debug.xcconfig | 1 + .../ios/Flutter/Release.xcconfig | 1 + examples/mirai_gallery/ios/Podfile | 41 ++++ examples/mirai_gallery/ios/Podfile.lock | 22 ++ .../ios/Runner.xcodeproj/project.pbxproj | 68 +++++++ .../contents.xcworkspacedata | 3 + .../flutter/generated_plugin_registrant.cc | 4 + .../linux/flutter/generated_plugins.cmake | 1 + .../macos/Flutter/Flutter-Debug.xcconfig | 1 + .../macos/Flutter/Flutter-Release.xcconfig | 1 + .../Flutter/GeneratedPluginRegistrant.swift | 2 + examples/mirai_gallery/macos/Podfile | 40 ++++ .../macos/Runner/DebugProfile.entitlements | 2 + .../macos/Runner/Release.entitlements | 2 + examples/mirai_gallery/pubspec.lock | 73 ++++++- .../flutter/generated_plugin_registrant.cc | 3 + .../windows/flutter/generated_plugins.cmake | 1 + packages/mirai/.flutter-plugins | 6 + packages/mirai/.flutter-plugins-dependencies | 1 + .../mirai/lib/src/action/mirai_action.dart | 2 + .../lib/src/action/mirai_action.freezed.dart | 50 ++++- .../mirai/lib/src/action/mirai_action.g.dart | 4 + .../lib/src/action/mirai_action_parser.dart | 6 + .../mirai/lib/src/storage/mirai_storage.dart | 18 ++ .../src/storage/mirai_storage.freezed.dart | 188 ++++++++++++++++++ .../lib/src/storage/mirai_storage.g.dart | 28 +++ .../lib/src/storage/mirai_storage_parser.dart | 17 ++ packages/mirai/pubspec.yaml | 4 +- 30 files changed, 713 insertions(+), 15 deletions(-) create mode 100644 examples/mirai_gallery/assets/json/storage_example.json create mode 100644 examples/mirai_gallery/ios/Podfile create mode 100644 examples/mirai_gallery/ios/Podfile.lock create mode 100644 examples/mirai_gallery/macos/Podfile create mode 100644 packages/mirai/.flutter-plugins create mode 100644 packages/mirai/.flutter-plugins-dependencies create mode 100644 packages/mirai/lib/src/storage/mirai_storage.dart create mode 100644 packages/mirai/lib/src/storage/mirai_storage.freezed.dart create mode 100644 packages/mirai/lib/src/storage/mirai_storage.g.dart create mode 100644 packages/mirai/lib/src/storage/mirai_storage_parser.dart diff --git a/examples/mirai_gallery/assets/json/home_screen.json b/examples/mirai_gallery/assets/json/home_screen.json index 4b1e8d6f..cf70bfd2 100644 --- a/examples/mirai_gallery/assets/json/home_screen.json +++ b/examples/mirai_gallery/assets/json/home_screen.json @@ -222,6 +222,38 @@ } } }, + { + "type": "listTile", + "leading": { + "type": "icon", + "iconType": "material", + "icon": "layers" + }, + "title": { + "type": "text", + "data": "Mirai Storage", + "style": { + "fontSize": 21 + } + }, + "subtitle": { + "type": "text", + "data": "This is an example of how mirai-storage works.", + "style": { + "fontSize": 12 + } + }, + "isThreeLine": true, + "onTap": { + "actionType": "navigate", + "navigationStyle": "push", + "navigationType": "screen", + "widgetJson": { + "type": "exampleScreen", + "assetPath": "assets/json/storage_example.json" + } + } + }, { "type": "listTile", "leading": { diff --git a/examples/mirai_gallery/assets/json/storage_example.json b/examples/mirai_gallery/assets/json/storage_example.json new file mode 100644 index 00000000..d06f9903 --- /dev/null +++ b/examples/mirai_gallery/assets/json/storage_example.json @@ -0,0 +1,106 @@ +{ + "type": "scaffold", + "appBar": { + "type": "appBar", + "title": { + "type": "text", + "data": "Storage" + } + }, + "body": { + "type": "column", + "mainAxisAlignment": "start", + "crossAxisAlignment": "center", + "children": [ + { + "type": "sizedBox", + "height": 24 + }, + { + "type": "row", + "mainAxisAlignment": "center", + "crossAxisAlignment": "center", + "children": [ + { + "type": "outlinedButton", + "child": { + "type": "text", + "data": "Store 'name:jack'" + }, + "style": { + "padding": { + "top": 8, + "left": 12, + "right": 12, + "bottom": 8 + } + }, + "onPressed": { + "storage": { + "type": "store", + "key": "name", + "value": "jack" + } + } + }, + { + "type": "sizedBox", + "width": 12 + }, + { + "type": "elevatedButton", + "child": { + "type": "text", + "data": "Retreive 'name:'", + "style": { + "color": "#ffffff" + } + }, + "style": { + "padding": { + "top": 8, + "left": 12, + "right": 12, + "bottom": 8 + } + }, + "onPressed": { + "storage": { + "type": "retreive", + "key": "name" + } + } + } + ] + }, + { + "type": "sizedBox", + "height": 24 + }, + { + "type": "center", + "child": { + "type": "outlinedButton", + "child": { + "type": "text", + "data": "Delete 'name:'" + }, + "style": { + "padding": { + "top": 8, + "left": 12, + "right": 12, + "bottom": 8 + } + }, + "onPressed": { + "storage": { + "type": "delete", + "key": "name" + } + } + } + } + ] + } +} \ No newline at end of file diff --git a/examples/mirai_gallery/ios/Flutter/Debug.xcconfig b/examples/mirai_gallery/ios/Flutter/Debug.xcconfig index 592ceee8..ec97fc6f 100644 --- a/examples/mirai_gallery/ios/Flutter/Debug.xcconfig +++ b/examples/mirai_gallery/ios/Flutter/Debug.xcconfig @@ -1 +1,2 @@ +#include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig" #include "Generated.xcconfig" diff --git a/examples/mirai_gallery/ios/Flutter/Release.xcconfig b/examples/mirai_gallery/ios/Flutter/Release.xcconfig index 592ceee8..c4855bfe 100644 --- a/examples/mirai_gallery/ios/Flutter/Release.xcconfig +++ b/examples/mirai_gallery/ios/Flutter/Release.xcconfig @@ -1 +1,2 @@ +#include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig" #include "Generated.xcconfig" diff --git a/examples/mirai_gallery/ios/Podfile b/examples/mirai_gallery/ios/Podfile new file mode 100644 index 00000000..88359b22 --- /dev/null +++ b/examples/mirai_gallery/ios/Podfile @@ -0,0 +1,41 @@ +# Uncomment this line to define a global platform for your project +# platform :ios, '11.0' + +# CocoaPods analytics sends network stats synchronously affecting flutter build latency. +ENV['COCOAPODS_DISABLE_STATS'] = 'true' + +project 'Runner', { + 'Debug' => :debug, + 'Profile' => :release, + 'Release' => :release, +} + +def flutter_root + generated_xcode_build_settings_path = File.expand_path(File.join('..', 'Flutter', 'Generated.xcconfig'), __FILE__) + unless File.exist?(generated_xcode_build_settings_path) + raise "#{generated_xcode_build_settings_path} must exist. If you're running pod install manually, make sure flutter pub get is executed first" + end + + File.foreach(generated_xcode_build_settings_path) do |line| + matches = line.match(/FLUTTER_ROOT\=(.*)/) + return matches[1].strip if matches + end + raise "FLUTTER_ROOT not found in #{generated_xcode_build_settings_path}. Try deleting Generated.xcconfig, then run flutter pub get" +end + +require File.expand_path(File.join('packages', 'flutter_tools', 'bin', 'podhelper'), flutter_root) + +flutter_ios_podfile_setup + +target 'Runner' do + use_frameworks! + use_modular_headers! + + flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__)) +end + +post_install do |installer| + installer.pods_project.targets.each do |target| + flutter_additional_ios_build_settings(target) + end +end diff --git a/examples/mirai_gallery/ios/Podfile.lock b/examples/mirai_gallery/ios/Podfile.lock new file mode 100644 index 00000000..ca2c29c5 --- /dev/null +++ b/examples/mirai_gallery/ios/Podfile.lock @@ -0,0 +1,22 @@ +PODS: + - Flutter (1.0.0) + - flutter_secure_storage (6.0.0): + - Flutter + +DEPENDENCIES: + - Flutter (from `Flutter`) + - flutter_secure_storage (from `.symlinks/plugins/flutter_secure_storage/ios`) + +EXTERNAL SOURCES: + Flutter: + :path: Flutter + flutter_secure_storage: + :path: ".symlinks/plugins/flutter_secure_storage/ios" + +SPEC CHECKSUMS: + Flutter: f04841e97a9d0b0a8025694d0796dd46242b2854 + flutter_secure_storage: 23fc622d89d073675f2eaa109381aefbcf5a49be + +PODFILE CHECKSUM: ef19549a9bc3046e7bb7d2fab4d021637c0c58a3 + +COCOAPODS: 1.11.3 diff --git a/examples/mirai_gallery/ios/Runner.xcodeproj/project.pbxproj b/examples/mirai_gallery/ios/Runner.xcodeproj/project.pbxproj index cd191099..8fd0349d 100644 --- a/examples/mirai_gallery/ios/Runner.xcodeproj/project.pbxproj +++ b/examples/mirai_gallery/ios/Runner.xcodeproj/project.pbxproj @@ -13,6 +13,7 @@ 97C146FC1CF9000F007C117D /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FA1CF9000F007C117D /* Main.storyboard */; }; 97C146FE1CF9000F007C117D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FD1CF9000F007C117D /* Assets.xcassets */; }; 97C147011CF9000F007C117D /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FF1CF9000F007C117D /* LaunchScreen.storyboard */; }; + E272320FB1B337B0B1C6A7E5 /* Pods_Runner.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6E2FBB7158597452D8D8BE82 /* Pods_Runner.framework */; }; /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ @@ -32,9 +33,12 @@ 1498D2321E8E86230040F4C2 /* GeneratedPluginRegistrant.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GeneratedPluginRegistrant.h; sourceTree = ""; }; 1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GeneratedPluginRegistrant.m; sourceTree = ""; }; 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = AppFrameworkInfo.plist; path = Flutter/AppFrameworkInfo.plist; sourceTree = ""; }; + 6E2FBB7158597452D8D8BE82 /* Pods_Runner.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Runner.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 74858FAD1ED2DC5600515810 /* Runner-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Runner-Bridging-Header.h"; sourceTree = ""; }; 74858FAE1ED2DC5600515810 /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 7AFA3C8E1D35360C0083082E /* Release.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = Release.xcconfig; path = Flutter/Release.xcconfig; sourceTree = ""; }; + 8A2D8073E26A549F058502AC /* Pods-Runner.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.release.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"; sourceTree = ""; }; + 8C416854D715E1538892E679 /* Pods-Runner.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.debug.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig"; sourceTree = ""; }; 9740EEB21CF90195004384FC /* Debug.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; name = Debug.xcconfig; path = Flutter/Debug.xcconfig; sourceTree = ""; }; 9740EEB31CF90195004384FC /* Generated.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; name = Generated.xcconfig; path = Flutter/Generated.xcconfig; sourceTree = ""; }; 97C146EE1CF9000F007C117D /* Runner.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Runner.app; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -42,6 +46,7 @@ 97C146FD1CF9000F007C117D /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 97C147001CF9000F007C117D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 97C147021CF9000F007C117D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + 9A7655BF95CAD427E3552642 /* Pods-Runner.profile.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.profile.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.profile.xcconfig"; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -49,12 +54,24 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + E272320FB1B337B0B1C6A7E5 /* Pods_Runner.framework in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ + 6B7632A0329A0EA037370D20 /* Pods */ = { + isa = PBXGroup; + children = ( + 8C416854D715E1538892E679 /* Pods-Runner.debug.xcconfig */, + 8A2D8073E26A549F058502AC /* Pods-Runner.release.xcconfig */, + 9A7655BF95CAD427E3552642 /* Pods-Runner.profile.xcconfig */, + ); + name = Pods; + path = Pods; + sourceTree = ""; + }; 9740EEB11CF90186004384FC /* Flutter */ = { isa = PBXGroup; children = ( @@ -72,6 +89,8 @@ 9740EEB11CF90186004384FC /* Flutter */, 97C146F01CF9000F007C117D /* Runner */, 97C146EF1CF9000F007C117D /* Products */, + 6B7632A0329A0EA037370D20 /* Pods */, + ECF9350C1F7A9CAFDC318AEF /* Frameworks */, ); sourceTree = ""; }; @@ -98,6 +117,14 @@ path = Runner; sourceTree = ""; }; + ECF9350C1F7A9CAFDC318AEF /* Frameworks */ = { + isa = PBXGroup; + children = ( + 6E2FBB7158597452D8D8BE82 /* Pods_Runner.framework */, + ); + name = Frameworks; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -105,12 +132,14 @@ isa = PBXNativeTarget; buildConfigurationList = 97C147051CF9000F007C117D /* Build configuration list for PBXNativeTarget "Runner" */; buildPhases = ( + A860C7D4639B249F515EC038 /* [CP] Check Pods Manifest.lock */, 9740EEB61CF901F6004384FC /* Run Script */, 97C146EA1CF9000F007C117D /* Sources */, 97C146EB1CF9000F007C117D /* Frameworks */, 97C146EC1CF9000F007C117D /* Resources */, 9705A1C41CF9048500538489 /* Embed Frameworks */, 3B06AD1E1E4923F5004D2608 /* Thin Binary */, + 3204B3072A06F8C9F13FF29B /* [CP] Embed Pods Frameworks */, ); buildRules = ( ); @@ -169,6 +198,23 @@ /* End PBXResourcesBuildPhase section */ /* Begin PBXShellScriptBuildPhase section */ + 3204B3072A06F8C9F13FF29B /* [CP] Embed Pods Frameworks */ = { + isa = PBXShellScriptBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + inputFileListPaths = ( + "${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks-${CONFIGURATION}-input-files.xcfilelist", + ); + name = "[CP] Embed Pods Frameworks"; + outputFileListPaths = ( + "${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks-${CONFIGURATION}-output-files.xcfilelist", + ); + runOnlyForDeploymentPostprocessing = 0; + shellPath = /bin/sh; + shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh\"\n"; + showEnvVarsInLog = 0; + }; 3B06AD1E1E4923F5004D2608 /* Thin Binary */ = { isa = PBXShellScriptBuildPhase; alwaysOutOfDate = 1; @@ -199,6 +245,28 @@ shellPath = /bin/sh; shellScript = "/bin/sh \"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh\" build"; }; + A860C7D4639B249F515EC038 /* [CP] Check Pods Manifest.lock */ = { + isa = PBXShellScriptBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + inputFileListPaths = ( + ); + inputPaths = ( + "${PODS_PODFILE_DIR_PATH}/Podfile.lock", + "${PODS_ROOT}/Manifest.lock", + ); + name = "[CP] Check Pods Manifest.lock"; + outputFileListPaths = ( + ); + outputPaths = ( + "$(DERIVED_FILE_DIR)/Pods-Runner-checkManifestLockResult.txt", + ); + runOnlyForDeploymentPostprocessing = 0; + shellPath = /bin/sh; + shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n"; + showEnvVarsInLog = 0; + }; /* End PBXShellScriptBuildPhase section */ /* Begin PBXSourcesBuildPhase section */ diff --git a/examples/mirai_gallery/ios/Runner.xcworkspace/contents.xcworkspacedata b/examples/mirai_gallery/ios/Runner.xcworkspace/contents.xcworkspacedata index 1d526a16..21a3cc14 100644 --- a/examples/mirai_gallery/ios/Runner.xcworkspace/contents.xcworkspacedata +++ b/examples/mirai_gallery/ios/Runner.xcworkspace/contents.xcworkspacedata @@ -4,4 +4,7 @@ + + diff --git a/examples/mirai_gallery/linux/flutter/generated_plugin_registrant.cc b/examples/mirai_gallery/linux/flutter/generated_plugin_registrant.cc index e71a16d2..d0e7f797 100644 --- a/examples/mirai_gallery/linux/flutter/generated_plugin_registrant.cc +++ b/examples/mirai_gallery/linux/flutter/generated_plugin_registrant.cc @@ -6,6 +6,10 @@ #include "generated_plugin_registrant.h" +#include void fl_register_plugins(FlPluginRegistry* registry) { + g_autoptr(FlPluginRegistrar) flutter_secure_storage_linux_registrar = + fl_plugin_registry_get_registrar_for_plugin(registry, "FlutterSecureStorageLinuxPlugin"); + flutter_secure_storage_linux_plugin_register_with_registrar(flutter_secure_storage_linux_registrar); } diff --git a/examples/mirai_gallery/linux/flutter/generated_plugins.cmake b/examples/mirai_gallery/linux/flutter/generated_plugins.cmake index 2e1de87a..b29e9ba0 100644 --- a/examples/mirai_gallery/linux/flutter/generated_plugins.cmake +++ b/examples/mirai_gallery/linux/flutter/generated_plugins.cmake @@ -3,6 +3,7 @@ # list(APPEND FLUTTER_PLUGIN_LIST + flutter_secure_storage_linux ) list(APPEND FLUTTER_FFI_PLUGIN_LIST diff --git a/examples/mirai_gallery/macos/Flutter/Flutter-Debug.xcconfig b/examples/mirai_gallery/macos/Flutter/Flutter-Debug.xcconfig index c2efd0b6..4b81f9b2 100644 --- a/examples/mirai_gallery/macos/Flutter/Flutter-Debug.xcconfig +++ b/examples/mirai_gallery/macos/Flutter/Flutter-Debug.xcconfig @@ -1 +1,2 @@ +#include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig" #include "ephemeral/Flutter-Generated.xcconfig" diff --git a/examples/mirai_gallery/macos/Flutter/Flutter-Release.xcconfig b/examples/mirai_gallery/macos/Flutter/Flutter-Release.xcconfig index c2efd0b6..5caa9d15 100644 --- a/examples/mirai_gallery/macos/Flutter/Flutter-Release.xcconfig +++ b/examples/mirai_gallery/macos/Flutter/Flutter-Release.xcconfig @@ -1 +1,2 @@ +#include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig" #include "ephemeral/Flutter-Generated.xcconfig" diff --git a/examples/mirai_gallery/macos/Flutter/GeneratedPluginRegistrant.swift b/examples/mirai_gallery/macos/Flutter/GeneratedPluginRegistrant.swift index cccf817a..e84ed5a8 100644 --- a/examples/mirai_gallery/macos/Flutter/GeneratedPluginRegistrant.swift +++ b/examples/mirai_gallery/macos/Flutter/GeneratedPluginRegistrant.swift @@ -5,6 +5,8 @@ import FlutterMacOS import Foundation +import flutter_secure_storage_macos func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { + FlutterSecureStoragePlugin.register(with: registry.registrar(forPlugin: "FlutterSecureStoragePlugin")) } diff --git a/examples/mirai_gallery/macos/Podfile b/examples/mirai_gallery/macos/Podfile new file mode 100644 index 00000000..049abe29 --- /dev/null +++ b/examples/mirai_gallery/macos/Podfile @@ -0,0 +1,40 @@ +platform :osx, '10.14' + +# CocoaPods analytics sends network stats synchronously affecting flutter build latency. +ENV['COCOAPODS_DISABLE_STATS'] = 'true' + +project 'Runner', { + 'Debug' => :debug, + 'Profile' => :release, + 'Release' => :release, +} + +def flutter_root + generated_xcode_build_settings_path = File.expand_path(File.join('..', 'Flutter', 'ephemeral', 'Flutter-Generated.xcconfig'), __FILE__) + unless File.exist?(generated_xcode_build_settings_path) + raise "#{generated_xcode_build_settings_path} must exist. If you're running pod install manually, make sure \"flutter pub get\" is executed first" + end + + File.foreach(generated_xcode_build_settings_path) do |line| + matches = line.match(/FLUTTER_ROOT\=(.*)/) + return matches[1].strip if matches + end + raise "FLUTTER_ROOT not found in #{generated_xcode_build_settings_path}. Try deleting Flutter-Generated.xcconfig, then run \"flutter pub get\"" +end + +require File.expand_path(File.join('packages', 'flutter_tools', 'bin', 'podhelper'), flutter_root) + +flutter_macos_podfile_setup + +target 'Runner' do + use_frameworks! + use_modular_headers! + + flutter_install_all_macos_pods File.dirname(File.realpath(__FILE__)) +end + +post_install do |installer| + installer.pods_project.targets.each do |target| + flutter_additional_macos_build_settings(target) + end +end diff --git a/examples/mirai_gallery/macos/Runner/DebugProfile.entitlements b/examples/mirai_gallery/macos/Runner/DebugProfile.entitlements index dddb8a30..b231432b 100644 --- a/examples/mirai_gallery/macos/Runner/DebugProfile.entitlements +++ b/examples/mirai_gallery/macos/Runner/DebugProfile.entitlements @@ -8,5 +8,7 @@ com.apple.security.network.server + keychain-access-groups + diff --git a/examples/mirai_gallery/macos/Runner/Release.entitlements b/examples/mirai_gallery/macos/Runner/Release.entitlements index 852fa1a4..0833c1b4 100644 --- a/examples/mirai_gallery/macos/Runner/Release.entitlements +++ b/examples/mirai_gallery/macos/Runner/Release.entitlements @@ -4,5 +4,7 @@ com.apple.security.app-sandbox + keychain-access-groups + diff --git a/examples/mirai_gallery/pubspec.lock b/examples/mirai_gallery/pubspec.lock index 28774a61..c8c9a9d4 100644 --- a/examples/mirai_gallery/pubspec.lock +++ b/examples/mirai_gallery/pubspec.lock @@ -189,10 +189,10 @@ packages: dependency: transitive description: name: dio - sha256: "3709d74615bba5e443eb141f6a7f4bcc4788f8fae6f743edadfb79c2a8e6287e" + sha256: "347d56c26d63519552ef9a569f2a593dda99a81fdbdff13c584b7197cfe05059" url: "https://pub.dev" source: hosted - version: "5.0.1" + version: "5.1.2" fake_async: dependency: transitive description: @@ -238,11 +238,64 @@ packages: url: "https://pub.dev" source: hosted version: "2.0.1" + flutter_secure_storage: + dependency: transitive + description: + name: flutter_secure_storage + sha256: "98352186ee7ad3639ccc77ad7924b773ff6883076ab952437d20f18a61f0a7c5" + url: "https://pub.dev" + source: hosted + version: "8.0.0" + flutter_secure_storage_linux: + dependency: transitive + description: + name: flutter_secure_storage_linux + sha256: "0912ae29a572230ad52d8a4697e5518d7f0f429052fd51df7e5a7952c7efe2a3" + url: "https://pub.dev" + source: hosted + version: "1.1.3" + flutter_secure_storage_macos: + dependency: transitive + description: + name: flutter_secure_storage_macos + sha256: "083add01847fc1c80a07a08e1ed6927e9acd9618a35e330239d4422cd2a58c50" + url: "https://pub.dev" + source: hosted + version: "3.0.0" + flutter_secure_storage_platform_interface: + dependency: transitive + description: + name: flutter_secure_storage_platform_interface + sha256: b3773190e385a3c8a382007893d678ae95462b3c2279e987b55d140d3b0cb81b + url: "https://pub.dev" + source: hosted + version: "1.0.1" + flutter_secure_storage_web: + dependency: transitive + description: + name: flutter_secure_storage_web + sha256: "42938e70d4b872e856e678c423cc0e9065d7d294f45bc41fc1981a4eb4beaffe" + url: "https://pub.dev" + source: hosted + version: "1.1.1" + flutter_secure_storage_windows: + dependency: transitive + description: + name: flutter_secure_storage_windows + sha256: fc2910ec9b28d60598216c29ea763b3a96c401f0ce1d13cdf69ccb0e5c93c3ee + url: "https://pub.dev" + source: hosted + version: "2.0.0" flutter_test: dependency: "direct dev" description: flutter source: sdk version: "0.0.0" + flutter_web_plugins: + dependency: transitive + description: flutter + source: sdk + version: "0.0.0" freezed: dependency: "direct dev" description: @@ -343,10 +396,10 @@ packages: dependency: transitive description: name: logger - sha256: c40f9ef51e5bffb4ce69ad2d8c8aad7bd47ec109c090521109b63a4e2bc27191 + sha256: db2ff852ed77090ba9f62d3611e4208a3d11dfa35991a81ae724c113fcb3e3f7 url: "https://pub.dev" source: hosted - version: "1.2.2" + version: "1.3.0" logging: dependency: transitive description: @@ -393,7 +446,7 @@ packages: path: "../../packages/mirai" relative: true source: path - version: "0.1.0" + version: "0.2.0" nested: dependency: transitive description: @@ -418,6 +471,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.8.2" + plugin_platform_interface: + dependency: transitive + description: + name: plugin_platform_interface + sha256: "6a2128648c854906c53fa8e33986fc0247a1116122f9534dd20e3ab9e16a32bc" + url: "https://pub.dev" + source: hosted + version: "2.1.4" pool: dependency: transitive description: @@ -593,4 +654,4 @@ packages: version: "3.1.1" sdks: dart: ">=2.19.2 <3.0.0" - flutter: ">=1.17.0" + flutter: ">=2.0.0" diff --git a/examples/mirai_gallery/windows/flutter/generated_plugin_registrant.cc b/examples/mirai_gallery/windows/flutter/generated_plugin_registrant.cc index 8b6d4680..0c507538 100644 --- a/examples/mirai_gallery/windows/flutter/generated_plugin_registrant.cc +++ b/examples/mirai_gallery/windows/flutter/generated_plugin_registrant.cc @@ -6,6 +6,9 @@ #include "generated_plugin_registrant.h" +#include void RegisterPlugins(flutter::PluginRegistry* registry) { + FlutterSecureStorageWindowsPluginRegisterWithRegistrar( + registry->GetRegistrarForPlugin("FlutterSecureStorageWindowsPlugin")); } diff --git a/examples/mirai_gallery/windows/flutter/generated_plugins.cmake b/examples/mirai_gallery/windows/flutter/generated_plugins.cmake index b93c4c30..4fc759c4 100644 --- a/examples/mirai_gallery/windows/flutter/generated_plugins.cmake +++ b/examples/mirai_gallery/windows/flutter/generated_plugins.cmake @@ -3,6 +3,7 @@ # list(APPEND FLUTTER_PLUGIN_LIST + flutter_secure_storage_windows ) list(APPEND FLUTTER_FFI_PLUGIN_LIST diff --git a/packages/mirai/.flutter-plugins b/packages/mirai/.flutter-plugins new file mode 100644 index 00000000..8c0a7bb4 --- /dev/null +++ b/packages/mirai/.flutter-plugins @@ -0,0 +1,6 @@ +# This is a generated file; do not edit or check into version control. +flutter_secure_storage=/Users/asim.khan/.pub-cache/hosted/pub.dev/flutter_secure_storage-8.0.0/ +flutter_secure_storage_linux=/Users/asim.khan/.pub-cache/hosted/pub.dev/flutter_secure_storage_linux-1.1.3/ +flutter_secure_storage_macos=/Users/asim.khan/.pub-cache/hosted/pub.dev/flutter_secure_storage_macos-3.0.0/ +flutter_secure_storage_web=/Users/asim.khan/.pub-cache/hosted/pub.dev/flutter_secure_storage_web-1.1.1/ +flutter_secure_storage_windows=/Users/asim.khan/.pub-cache/hosted/pub.dev/flutter_secure_storage_windows-2.0.0/ diff --git a/packages/mirai/.flutter-plugins-dependencies b/packages/mirai/.flutter-plugins-dependencies new file mode 100644 index 00000000..b61fdf63 --- /dev/null +++ b/packages/mirai/.flutter-plugins-dependencies @@ -0,0 +1 @@ +{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"flutter_secure_storage","path":"/Users/asim.khan/.pub-cache/hosted/pub.dev/flutter_secure_storage-8.0.0/","native_build":true,"dependencies":[]}],"android":[{"name":"flutter_secure_storage","path":"/Users/asim.khan/.pub-cache/hosted/pub.dev/flutter_secure_storage-8.0.0/","native_build":true,"dependencies":[]}],"macos":[{"name":"flutter_secure_storage_macos","path":"/Users/asim.khan/.pub-cache/hosted/pub.dev/flutter_secure_storage_macos-3.0.0/","native_build":true,"dependencies":[]}],"linux":[{"name":"flutter_secure_storage_linux","path":"/Users/asim.khan/.pub-cache/hosted/pub.dev/flutter_secure_storage_linux-1.1.3/","native_build":true,"dependencies":[]}],"windows":[{"name":"flutter_secure_storage_windows","path":"/Users/asim.khan/.pub-cache/hosted/pub.dev/flutter_secure_storage_windows-2.0.0/","native_build":true,"dependencies":[]}],"web":[{"name":"flutter_secure_storage_web","path":"/Users/asim.khan/.pub-cache/hosted/pub.dev/flutter_secure_storage_web-1.1.1/","dependencies":[]}]},"dependencyGraph":[{"name":"flutter_secure_storage","dependencies":["flutter_secure_storage_linux","flutter_secure_storage_macos","flutter_secure_storage_web","flutter_secure_storage_windows"]},{"name":"flutter_secure_storage_linux","dependencies":[]},{"name":"flutter_secure_storage_macos","dependencies":[]},{"name":"flutter_secure_storage_web","dependencies":[]},{"name":"flutter_secure_storage_windows","dependencies":[]}],"date_created":"2023-05-10 11:20:10.561982","version":"3.7.12"} \ No newline at end of file diff --git a/packages/mirai/lib/src/action/mirai_action.dart b/packages/mirai/lib/src/action/mirai_action.dart index 650d89e5..84ea2433 100644 --- a/packages/mirai/lib/src/action/mirai_action.dart +++ b/packages/mirai/lib/src/action/mirai_action.dart @@ -1,6 +1,7 @@ import 'package:freezed_annotation/freezed_annotation.dart'; import 'package:mirai/src/navigation/mirai_navigator.dart'; import 'package:mirai/src/network/mirai_request.dart'; +import 'package:mirai/src/storage/mirai_storage.dart'; part 'mirai_action.freezed.dart'; part 'mirai_action.g.dart'; @@ -16,6 +17,7 @@ class MiraiAction with _$MiraiAction { String? assetPath, NavigationType? navigationType, NavigationStyle? navigationStyle, + MiraiStorage? storage, }) = _MiraiAction; factory MiraiAction.fromJson(Map json) => diff --git a/packages/mirai/lib/src/action/mirai_action.freezed.dart b/packages/mirai/lib/src/action/mirai_action.freezed.dart index e657f5ed..0060236c 100644 --- a/packages/mirai/lib/src/action/mirai_action.freezed.dart +++ b/packages/mirai/lib/src/action/mirai_action.freezed.dart @@ -26,6 +26,7 @@ mixin _$MiraiAction { String? get assetPath => throw _privateConstructorUsedError; NavigationType? get navigationType => throw _privateConstructorUsedError; NavigationStyle? get navigationStyle => throw _privateConstructorUsedError; + MiraiStorage? get storage => throw _privateConstructorUsedError; Map toJson() => throw _privateConstructorUsedError; @JsonKey(ignore: true) @@ -45,9 +46,11 @@ abstract class $MiraiActionCopyWith<$Res> { Map? widgetJson, String? assetPath, NavigationType? navigationType, - NavigationStyle? navigationStyle}); + NavigationStyle? navigationStyle, + MiraiStorage? storage}); $MiraiRequestCopyWith<$Res>? get request; + $MiraiStorageCopyWith<$Res>? get storage; } /// @nodoc @@ -69,6 +72,7 @@ class _$MiraiActionCopyWithImpl<$Res, $Val extends MiraiAction> Object? assetPath = freezed, Object? navigationType = freezed, Object? navigationStyle = freezed, + Object? storage = freezed, }) { return _then(_value.copyWith( actionType: null == actionType @@ -95,6 +99,10 @@ class _$MiraiActionCopyWithImpl<$Res, $Val extends MiraiAction> ? _value.navigationStyle : navigationStyle // ignore: cast_nullable_to_non_nullable as NavigationStyle?, + storage: freezed == storage + ? _value.storage + : storage // ignore: cast_nullable_to_non_nullable + as MiraiStorage?, ) as $Val); } @@ -109,6 +117,18 @@ class _$MiraiActionCopyWithImpl<$Res, $Val extends MiraiAction> return _then(_value.copyWith(request: value) as $Val); }); } + + @override + @pragma('vm:prefer-inline') + $MiraiStorageCopyWith<$Res>? get storage { + if (_value.storage == null) { + return null; + } + + return $MiraiStorageCopyWith<$Res>(_value.storage!, (value) { + return _then(_value.copyWith(storage: value) as $Val); + }); + } } /// @nodoc @@ -125,10 +145,13 @@ abstract class _$$_MiraiActionCopyWith<$Res> Map? widgetJson, String? assetPath, NavigationType? navigationType, - NavigationStyle? navigationStyle}); + NavigationStyle? navigationStyle, + MiraiStorage? storage}); @override $MiraiRequestCopyWith<$Res>? get request; + @override + $MiraiStorageCopyWith<$Res>? get storage; } /// @nodoc @@ -148,6 +171,7 @@ class __$$_MiraiActionCopyWithImpl<$Res> Object? assetPath = freezed, Object? navigationType = freezed, Object? navigationStyle = freezed, + Object? storage = freezed, }) { return _then(_$_MiraiAction( actionType: null == actionType @@ -174,6 +198,10 @@ class __$$_MiraiActionCopyWithImpl<$Res> ? _value.navigationStyle : navigationStyle // ignore: cast_nullable_to_non_nullable as NavigationStyle?, + storage: freezed == storage + ? _value.storage + : storage // ignore: cast_nullable_to_non_nullable + as MiraiStorage?, )); } } @@ -187,7 +215,8 @@ class _$_MiraiAction implements _MiraiAction { final Map? widgetJson, this.assetPath, this.navigationType, - this.navigationStyle}) + this.navigationStyle, + this.storage}) : _widgetJson = widgetJson; factory _$_MiraiAction.fromJson(Map json) => @@ -214,10 +243,12 @@ class _$_MiraiAction implements _MiraiAction { final NavigationType? navigationType; @override final NavigationStyle? navigationStyle; + @override + final MiraiStorage? storage; @override String toString() { - return 'MiraiAction(actionType: $actionType, request: $request, widgetJson: $widgetJson, assetPath: $assetPath, navigationType: $navigationType, navigationStyle: $navigationStyle)'; + return 'MiraiAction(actionType: $actionType, request: $request, widgetJson: $widgetJson, assetPath: $assetPath, navigationType: $navigationType, navigationStyle: $navigationStyle, storage: $storage)'; } @override @@ -235,7 +266,8 @@ class _$_MiraiAction implements _MiraiAction { (identical(other.navigationType, navigationType) || other.navigationType == navigationType) && (identical(other.navigationStyle, navigationStyle) || - other.navigationStyle == navigationStyle)); + other.navigationStyle == navigationStyle) && + (identical(other.storage, storage) || other.storage == storage)); } @JsonKey(ignore: true) @@ -247,7 +279,8 @@ class _$_MiraiAction implements _MiraiAction { const DeepCollectionEquality().hash(_widgetJson), assetPath, navigationType, - navigationStyle); + navigationStyle, + storage); @JsonKey(ignore: true) @override @@ -270,7 +303,8 @@ abstract class _MiraiAction implements MiraiAction { final Map? widgetJson, final String? assetPath, final NavigationType? navigationType, - final NavigationStyle? navigationStyle}) = _$_MiraiAction; + final NavigationStyle? navigationStyle, + final MiraiStorage? storage}) = _$_MiraiAction; factory _MiraiAction.fromJson(Map json) = _$_MiraiAction.fromJson; @@ -288,6 +322,8 @@ abstract class _MiraiAction implements MiraiAction { @override NavigationStyle? get navigationStyle; @override + MiraiStorage? get storage; + @override @JsonKey(ignore: true) _$$_MiraiActionCopyWith<_$_MiraiAction> get copyWith => throw _privateConstructorUsedError; diff --git a/packages/mirai/lib/src/action/mirai_action.g.dart b/packages/mirai/lib/src/action/mirai_action.g.dart index 56171bdb..f57fbcba 100644 --- a/packages/mirai/lib/src/action/mirai_action.g.dart +++ b/packages/mirai/lib/src/action/mirai_action.g.dart @@ -20,6 +20,9 @@ _$_MiraiAction _$$_MiraiActionFromJson(Map json) => $enumDecodeNullable(_$NavigationTypeEnumMap, json['navigationType']), navigationStyle: $enumDecodeNullable( _$NavigationStyleEnumMap, json['navigationStyle']), + storage: json['storage'] == null + ? null + : MiraiStorage.fromJson(json['storage'] as Map), ); Map _$$_MiraiActionToJson(_$_MiraiAction instance) => @@ -30,6 +33,7 @@ Map _$$_MiraiActionToJson(_$_MiraiAction instance) => 'assetPath': instance.assetPath, 'navigationType': _$NavigationTypeEnumMap[instance.navigationType], 'navigationStyle': _$NavigationStyleEnumMap[instance.navigationStyle], + 'storage': instance.storage, }; const _$ActionTypeEnumMap = { diff --git a/packages/mirai/lib/src/action/mirai_action_parser.dart b/packages/mirai/lib/src/action/mirai_action_parser.dart index e2b5cf43..1f6864a4 100644 --- a/packages/mirai/lib/src/action/mirai_action_parser.dart +++ b/packages/mirai/lib/src/action/mirai_action_parser.dart @@ -3,6 +3,7 @@ import 'package:mirai/src/action/mirai_action.dart'; import 'package:mirai/src/framework/framework.dart'; import 'package:mirai/src/navigation/mirai_navigator.dart'; import 'package:mirai/src/network/mirai_network.dart'; +import 'package:mirai/src/storage/mirai_storage_parser.dart'; extension MiraiActionParser on MiraiAction? { Future? onCall(BuildContext context) async { @@ -10,6 +11,11 @@ extension MiraiActionParser on MiraiAction? { if (this?.navigationStyle == NavigationStyle.pop) { MiraiNavigator.navigateBack(context); } + + if (this?.storage != null) { + return this?.storage?.onCall(); + } + switch (this?.actionType ?? ActionType.none) { case ActionType.navigate: Widget? widget; diff --git a/packages/mirai/lib/src/storage/mirai_storage.dart b/packages/mirai/lib/src/storage/mirai_storage.dart new file mode 100644 index 00000000..d915366d --- /dev/null +++ b/packages/mirai/lib/src/storage/mirai_storage.dart @@ -0,0 +1,18 @@ +import 'package:freezed_annotation/freezed_annotation.dart'; + +part 'mirai_storage.freezed.dart'; +part 'mirai_storage.g.dart'; + +enum MiraiStorageType { delete, store, retreive } + +@freezed +class MiraiStorage with _$MiraiStorage { + factory MiraiStorage({ + @Default(MiraiStorageType.retreive) MiraiStorageType type, + required String key, + String? value, + }) = _MiraiStorage; + + factory MiraiStorage.fromJson(Map json) => + _$MiraiStorageFromJson(json); +} diff --git a/packages/mirai/lib/src/storage/mirai_storage.freezed.dart b/packages/mirai/lib/src/storage/mirai_storage.freezed.dart new file mode 100644 index 00000000..2c91c650 --- /dev/null +++ b/packages/mirai/lib/src/storage/mirai_storage.freezed.dart @@ -0,0 +1,188 @@ +// coverage:ignore-file +// GENERATED CODE - DO NOT MODIFY BY HAND +// ignore_for_file: type=lint +// ignore_for_file: unused_element, deprecated_member_use, deprecated_member_use_from_same_package, use_function_type_syntax_for_parameters, unnecessary_const, avoid_init_to_null, invalid_override_different_default_values_named, prefer_expression_function_bodies, annotate_overrides, invalid_annotation_target, unnecessary_question_mark + +part of 'mirai_storage.dart'; + +// ************************************************************************** +// FreezedGenerator +// ************************************************************************** + +T _$identity(T value) => value; + +final _privateConstructorUsedError = UnsupportedError( + 'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more information: https://github.com/rrousselGit/freezed#custom-getters-and-methods'); + +MiraiStorage _$MiraiStorageFromJson(Map json) { + return _MiraiStorage.fromJson(json); +} + +/// @nodoc +mixin _$MiraiStorage { + MiraiStorageType get type => throw _privateConstructorUsedError; + String get key => throw _privateConstructorUsedError; + String? get value => throw _privateConstructorUsedError; + + Map toJson() => throw _privateConstructorUsedError; + @JsonKey(ignore: true) + $MiraiStorageCopyWith get copyWith => + throw _privateConstructorUsedError; +} + +/// @nodoc +abstract class $MiraiStorageCopyWith<$Res> { + factory $MiraiStorageCopyWith( + MiraiStorage value, $Res Function(MiraiStorage) then) = + _$MiraiStorageCopyWithImpl<$Res, MiraiStorage>; + @useResult + $Res call({MiraiStorageType type, String key, String? value}); +} + +/// @nodoc +class _$MiraiStorageCopyWithImpl<$Res, $Val extends MiraiStorage> + implements $MiraiStorageCopyWith<$Res> { + _$MiraiStorageCopyWithImpl(this._value, this._then); + + // ignore: unused_field + final $Val _value; + // ignore: unused_field + final $Res Function($Val) _then; + + @pragma('vm:prefer-inline') + @override + $Res call({ + Object? type = null, + Object? key = null, + Object? value = freezed, + }) { + return _then(_value.copyWith( + type: null == type + ? _value.type + : type // ignore: cast_nullable_to_non_nullable + as MiraiStorageType, + key: null == key + ? _value.key + : key // ignore: cast_nullable_to_non_nullable + as String, + value: freezed == value + ? _value.value + : value // ignore: cast_nullable_to_non_nullable + as String?, + ) as $Val); + } +} + +/// @nodoc +abstract class _$$_MiraiStorageCopyWith<$Res> + implements $MiraiStorageCopyWith<$Res> { + factory _$$_MiraiStorageCopyWith( + _$_MiraiStorage value, $Res Function(_$_MiraiStorage) then) = + __$$_MiraiStorageCopyWithImpl<$Res>; + @override + @useResult + $Res call({MiraiStorageType type, String key, String? value}); +} + +/// @nodoc +class __$$_MiraiStorageCopyWithImpl<$Res> + extends _$MiraiStorageCopyWithImpl<$Res, _$_MiraiStorage> + implements _$$_MiraiStorageCopyWith<$Res> { + __$$_MiraiStorageCopyWithImpl( + _$_MiraiStorage _value, $Res Function(_$_MiraiStorage) _then) + : super(_value, _then); + + @pragma('vm:prefer-inline') + @override + $Res call({ + Object? type = null, + Object? key = null, + Object? value = freezed, + }) { + return _then(_$_MiraiStorage( + type: null == type + ? _value.type + : type // ignore: cast_nullable_to_non_nullable + as MiraiStorageType, + key: null == key + ? _value.key + : key // ignore: cast_nullable_to_non_nullable + as String, + value: freezed == value + ? _value.value + : value // ignore: cast_nullable_to_non_nullable + as String?, + )); + } +} + +/// @nodoc +@JsonSerializable() +class _$_MiraiStorage implements _MiraiStorage { + _$_MiraiStorage( + {this.type = MiraiStorageType.retreive, required this.key, this.value}); + + factory _$_MiraiStorage.fromJson(Map json) => + _$$_MiraiStorageFromJson(json); + + @override + @JsonKey() + final MiraiStorageType type; + @override + final String key; + @override + final String? value; + + @override + String toString() { + return 'MiraiStorage(type: $type, key: $key, value: $value)'; + } + + @override + bool operator ==(dynamic other) { + return identical(this, other) || + (other.runtimeType == runtimeType && + other is _$_MiraiStorage && + (identical(other.type, type) || other.type == type) && + (identical(other.key, key) || other.key == key) && + (identical(other.value, value) || other.value == value)); + } + + @JsonKey(ignore: true) + @override + int get hashCode => Object.hash(runtimeType, type, key, value); + + @JsonKey(ignore: true) + @override + @pragma('vm:prefer-inline') + _$$_MiraiStorageCopyWith<_$_MiraiStorage> get copyWith => + __$$_MiraiStorageCopyWithImpl<_$_MiraiStorage>(this, _$identity); + + @override + Map toJson() { + return _$$_MiraiStorageToJson( + this, + ); + } +} + +abstract class _MiraiStorage implements MiraiStorage { + factory _MiraiStorage( + {final MiraiStorageType type, + required final String key, + final String? value}) = _$_MiraiStorage; + + factory _MiraiStorage.fromJson(Map json) = + _$_MiraiStorage.fromJson; + + @override + MiraiStorageType get type; + @override + String get key; + @override + String? get value; + @override + @JsonKey(ignore: true) + _$$_MiraiStorageCopyWith<_$_MiraiStorage> get copyWith => + throw _privateConstructorUsedError; +} diff --git a/packages/mirai/lib/src/storage/mirai_storage.g.dart b/packages/mirai/lib/src/storage/mirai_storage.g.dart new file mode 100644 index 00000000..df4159c8 --- /dev/null +++ b/packages/mirai/lib/src/storage/mirai_storage.g.dart @@ -0,0 +1,28 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'mirai_storage.dart'; + +// ************************************************************************** +// JsonSerializableGenerator +// ************************************************************************** + +_$_MiraiStorage _$$_MiraiStorageFromJson(Map json) => + _$_MiraiStorage( + type: $enumDecodeNullable(_$MiraiStorageTypeEnumMap, json['type']) ?? + MiraiStorageType.retreive, + key: json['key'] as String, + value: json['value'] as String?, + ); + +Map _$$_MiraiStorageToJson(_$_MiraiStorage instance) => + { + 'type': _$MiraiStorageTypeEnumMap[instance.type]!, + 'key': instance.key, + 'value': instance.value, + }; + +const _$MiraiStorageTypeEnumMap = { + MiraiStorageType.delete: 'delete', + MiraiStorageType.store: 'store', + MiraiStorageType.retreive: 'retreive', +}; diff --git a/packages/mirai/lib/src/storage/mirai_storage_parser.dart b/packages/mirai/lib/src/storage/mirai_storage_parser.dart new file mode 100644 index 00000000..4ff40390 --- /dev/null +++ b/packages/mirai/lib/src/storage/mirai_storage_parser.dart @@ -0,0 +1,17 @@ +import 'package:flutter_secure_storage/flutter_secure_storage.dart'; +import 'package:mirai/src/storage/mirai_storage.dart'; + +extension MiraiStorageParser on MiraiStorage? { + static const _storage = FlutterSecureStorage(); + + Future? onCall() async { + switch (this?.type ?? MiraiStorageType.retreive) { + case MiraiStorageType.store: + return _storage.write(key: this?.key ?? "", value: this?.value); + case MiraiStorageType.retreive: + return _storage.read(key: this?.key ?? ""); + case MiraiStorageType.delete: + return _storage.delete(key: this?.key ?? ""); + } + } +} diff --git a/packages/mirai/pubspec.yaml b/packages/mirai/pubspec.yaml index c5d011c0..52b5d1bf 100644 --- a/packages/mirai/pubspec.yaml +++ b/packages/mirai/pubspec.yaml @@ -14,7 +14,7 @@ dependencies: json_annotation: ^4.8.0 logger: ^1.3.0 dio: ^5.1.1 - + flutter_secure_storage: ^8.0.0 dev_dependencies: flutter_test: @@ -22,4 +22,4 @@ dev_dependencies: flutter_lints: ^2.0.1 build_runner: ^2.3.3 freezed: ^2.3.2 - json_serializable: ^6.6.1 \ No newline at end of file + json_serializable: ^6.6.1 From e577916fc902a225f659074b924d69b0362a43c8 Mon Sep 17 00:00:00 2001 From: Aasim Khan Date: Mon, 15 May 2023 15:04:21 +0400 Subject: [PATCH 2/5] feat: Updated example ui in storage example --- .../assets/json/home_screen.json | 2 +- .../assets/json/storage_example.json | 39 ++++++++++++++++--- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/examples/mirai_gallery/assets/json/home_screen.json b/examples/mirai_gallery/assets/json/home_screen.json index cf70bfd2..4656ad3b 100644 --- a/examples/mirai_gallery/assets/json/home_screen.json +++ b/examples/mirai_gallery/assets/json/home_screen.json @@ -227,7 +227,7 @@ "leading": { "type": "icon", "iconType": "material", - "icon": "layers" + "icon": "storage" }, "title": { "type": "text", diff --git a/examples/mirai_gallery/assets/json/storage_example.json b/examples/mirai_gallery/assets/json/storage_example.json index d06f9903..39e807b6 100644 --- a/examples/mirai_gallery/assets/json/storage_example.json +++ b/examples/mirai_gallery/assets/json/storage_example.json @@ -48,13 +48,10 @@ "width": 12 }, { - "type": "elevatedButton", + "type": "outlinedButton", "child": { "type": "text", - "data": "Retreive 'name:'", - "style": { - "color": "#ffffff" - } + "data": "Retreive 'name:'" }, "style": { "padding": { @@ -100,6 +97,38 @@ } } } + }, + { + "type": "sizedBox", + "height": 24 + }, + { + "type": "padding", + "padding": { + "left": 0, + "right": 0 + }, + "child": { + "type": "column", + "children": [ + { + "type": "text", + "data": "Stored Value for key:name", + "align": "center", + "style": { + "fontSize": 16 + } + }, + { + "type": "text", + "data": "$MiraiStorage('token')", + "align": "center", + "style": { + "fontSize": 16 + } + } + ] + } } ] } From 6745bcb9e5e2745b0b6bd023d295e4708ef832a1 Mon Sep 17 00:00:00 2001 From: Aasim Khan Date: Tue, 16 May 2023 12:02:50 +0400 Subject: [PATCH 3/5] Storage widget created to retrieve values from storage and use them --- .../assets/json/storage_example.json | 50 +++-- .../mirai/lib/src/action/mirai_action.dart | 2 +- .../mirai/lib/src/action/mirai_action.g.dart | 1 + .../lib/src/action/mirai_action_parser.dart | 19 +- packages/mirai/lib/src/framework/mirai.dart | 2 + .../mirai_storage_widget.dart | 17 ++ .../mirai_storage_widget.freezed.dart | 190 ++++++++++++++++++ .../mirai_storage_widget.g.dart | 24 +++ .../mirai_storage_widget_parser.dart | 31 +++ .../mirai/lib/src/storage/mirai_storage.dart | 4 +- .../src/storage/mirai_storage.freezed.dart | 2 +- .../lib/src/storage/mirai_storage.g.dart | 6 +- .../lib/src/storage/mirai_storage_parser.dart | 17 -- .../mirai/lib/src/storage/secure_storage.dart | 39 ++++ packages/mirai/lib/src/utils/widget_type.dart | 1 + 15 files changed, 354 insertions(+), 51 deletions(-) create mode 100644 packages/mirai/lib/src/parsers/mirai_storage_widget/mirai_storage_widget.dart create mode 100644 packages/mirai/lib/src/parsers/mirai_storage_widget/mirai_storage_widget.freezed.dart create mode 100644 packages/mirai/lib/src/parsers/mirai_storage_widget/mirai_storage_widget.g.dart create mode 100644 packages/mirai/lib/src/parsers/mirai_storage_widget/mirai_storage_widget_parser.dart delete mode 100644 packages/mirai/lib/src/storage/mirai_storage_parser.dart create mode 100644 packages/mirai/lib/src/storage/secure_storage.dart diff --git a/examples/mirai_gallery/assets/json/storage_example.json b/examples/mirai_gallery/assets/json/storage_example.json index 39e807b6..0a01049b 100644 --- a/examples/mirai_gallery/assets/json/storage_example.json +++ b/examples/mirai_gallery/assets/json/storage_example.json @@ -25,7 +25,7 @@ "type": "outlinedButton", "child": { "type": "text", - "data": "Store 'name:jack'" + "data": "write 'name:jack'" }, "style": { "padding": { @@ -37,7 +37,7 @@ }, "onPressed": { "storage": { - "type": "store", + "type": "write", "key": "name", "value": "jack" } @@ -51,7 +51,7 @@ "type": "outlinedButton", "child": { "type": "text", - "data": "Retreive 'name:'" + "data": "read 'name:'" }, "style": { "padding": { @@ -63,7 +63,7 @@ }, "onPressed": { "storage": { - "type": "retreive", + "type": "read", "key": "name" } } @@ -109,25 +109,31 @@ "right": 0 }, "child": { - "type": "column", - "children": [ - { - "type": "text", - "data": "Stored Value for key:name", - "align": "center", - "style": { - "fontSize": 16 - } - }, - { - "type": "text", - "data": "$MiraiStorage('token')", - "align": "center", - "style": { - "fontSize": 16 + "type": "storageWidget", + "storageKeys": [ + "name" + ], + "body": { + "type": "column", + "children": [ + { + "type": "text", + "data": "writed Value for key:name", + "align": "center", + "style": { + "fontSize": 16 + } + }, + { + "type": "text", + "data": "$name", + "align": "center", + "style": { + "fontSize": 16 + } } - } - ] + ] + } } } ] diff --git a/packages/mirai/lib/src/action/mirai_action.dart b/packages/mirai/lib/src/action/mirai_action.dart index 84ea2433..fb81807b 100644 --- a/packages/mirai/lib/src/action/mirai_action.dart +++ b/packages/mirai/lib/src/action/mirai_action.dart @@ -6,7 +6,7 @@ import 'package:mirai/src/storage/mirai_storage.dart'; part 'mirai_action.freezed.dart'; part 'mirai_action.g.dart'; -enum ActionType { navigate, request, none } +enum ActionType { navigate, request, storage, none } @freezed class MiraiAction with _$MiraiAction { diff --git a/packages/mirai/lib/src/action/mirai_action.g.dart b/packages/mirai/lib/src/action/mirai_action.g.dart index f57fbcba..d683de73 100644 --- a/packages/mirai/lib/src/action/mirai_action.g.dart +++ b/packages/mirai/lib/src/action/mirai_action.g.dart @@ -39,6 +39,7 @@ Map _$$_MiraiActionToJson(_$_MiraiAction instance) => const _$ActionTypeEnumMap = { ActionType.navigate: 'navigate', ActionType.request: 'request', + ActionType.storage: 'storage', ActionType.none: 'none', }; diff --git a/packages/mirai/lib/src/action/mirai_action_parser.dart b/packages/mirai/lib/src/action/mirai_action_parser.dart index 1f6864a4..c41f1001 100644 --- a/packages/mirai/lib/src/action/mirai_action_parser.dart +++ b/packages/mirai/lib/src/action/mirai_action_parser.dart @@ -3,7 +3,8 @@ import 'package:mirai/src/action/mirai_action.dart'; import 'package:mirai/src/framework/framework.dart'; import 'package:mirai/src/navigation/mirai_navigator.dart'; import 'package:mirai/src/network/mirai_network.dart'; -import 'package:mirai/src/storage/mirai_storage_parser.dart'; +import 'package:mirai/src/storage/mirai_storage.dart'; +import 'package:mirai/src/storage/secure_storage.dart'; extension MiraiActionParser on MiraiAction? { Future? onCall(BuildContext context) async { @@ -12,10 +13,6 @@ extension MiraiActionParser on MiraiAction? { MiraiNavigator.navigateBack(context); } - if (this?.storage != null) { - return this?.storage?.onCall(); - } - switch (this?.actionType ?? ActionType.none) { case ActionType.navigate: Widget? widget; @@ -56,6 +53,18 @@ extension MiraiActionParser on MiraiAction? { case ActionType.request: return MiraiNetwork.request(this!.request!); + + case ActionType.storage: + switch (this?.storage?.type ?? MiraiStorageType.read) { + case MiraiStorageType.write: + return SecureStorage.write( + this?.storage?.key ?? "", this?.storage?.value ?? ""); + case MiraiStorageType.read: + return SecureStorage.read(this?.storage?.key ?? ""); + case MiraiStorageType.delete: + return SecureStorage.delete(this?.storage?.key ?? ""); + } + case ActionType.none: break; } diff --git a/packages/mirai/lib/src/framework/mirai.dart b/packages/mirai/lib/src/framework/mirai.dart index faf024ba..22588b16 100644 --- a/packages/mirai/lib/src/framework/mirai.dart +++ b/packages/mirai/lib/src/framework/mirai.dart @@ -9,6 +9,7 @@ import 'package:mirai/src/network/mirai_network.dart'; import 'package:mirai/src/network/mirai_request.dart'; import 'package:mirai/src/parsers/mirai_center/mirai_center_parser.dart'; import 'package:mirai/src/parsers/mirai_fractionally_sized_box/mirai_fractionally_sized_box_parser.dart'; +import 'package:mirai/src/parsers/mirai_storage_widget/mirai_storage_widget.dart'; import 'package:mirai/src/parsers/mirai_tab/mirai_tab_parser.dart'; import 'package:mirai/src/parsers/parsers.dart'; import 'package:mirai/src/utils/log.dart'; @@ -53,6 +54,7 @@ class Mirai { const MiraiScrollViewParser(), const MiraiAlertDialogParser(), const MiraiTabParser(), + const MiraiStorageWidgetParser(), ]; static Future initialize({ diff --git a/packages/mirai/lib/src/parsers/mirai_storage_widget/mirai_storage_widget.dart b/packages/mirai/lib/src/parsers/mirai_storage_widget/mirai_storage_widget.dart new file mode 100644 index 00000000..403f4bf1 --- /dev/null +++ b/packages/mirai/lib/src/parsers/mirai_storage_widget/mirai_storage_widget.dart @@ -0,0 +1,17 @@ +import 'package:freezed_annotation/freezed_annotation.dart'; + +export 'package:mirai/src/parsers/mirai_storage_widget/mirai_storage_widget_parser.dart'; + +part 'mirai_storage_widget.freezed.dart'; +part 'mirai_storage_widget.g.dart'; + +@freezed +class MiraiStorageWidget with _$MiraiStorageWidget { + const factory MiraiStorageWidget({ + required Map body, + @Default([]) List storageKeys, + }) = _MiraiStorageWidget; + + factory MiraiStorageWidget.fromJson(Map json) => + _$MiraiStorageWidgetFromJson(json); +} diff --git a/packages/mirai/lib/src/parsers/mirai_storage_widget/mirai_storage_widget.freezed.dart b/packages/mirai/lib/src/parsers/mirai_storage_widget/mirai_storage_widget.freezed.dart new file mode 100644 index 00000000..b0d7247a --- /dev/null +++ b/packages/mirai/lib/src/parsers/mirai_storage_widget/mirai_storage_widget.freezed.dart @@ -0,0 +1,190 @@ +// coverage:ignore-file +// GENERATED CODE - DO NOT MODIFY BY HAND +// ignore_for_file: type=lint +// ignore_for_file: unused_element, deprecated_member_use, deprecated_member_use_from_same_package, use_function_type_syntax_for_parameters, unnecessary_const, avoid_init_to_null, invalid_override_different_default_values_named, prefer_expression_function_bodies, annotate_overrides, invalid_annotation_target, unnecessary_question_mark + +part of 'mirai_storage_widget.dart'; + +// ************************************************************************** +// FreezedGenerator +// ************************************************************************** + +T _$identity(T value) => value; + +final _privateConstructorUsedError = UnsupportedError( + 'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more information: https://github.com/rrousselGit/freezed#custom-getters-and-methods'); + +MiraiStorageWidget _$MiraiStorageWidgetFromJson(Map json) { + return _MiraiStorageWidget.fromJson(json); +} + +/// @nodoc +mixin _$MiraiStorageWidget { + Map get body => throw _privateConstructorUsedError; + List get storageKeys => throw _privateConstructorUsedError; + + Map toJson() => throw _privateConstructorUsedError; + @JsonKey(ignore: true) + $MiraiStorageWidgetCopyWith get copyWith => + throw _privateConstructorUsedError; +} + +/// @nodoc +abstract class $MiraiStorageWidgetCopyWith<$Res> { + factory $MiraiStorageWidgetCopyWith( + MiraiStorageWidget value, $Res Function(MiraiStorageWidget) then) = + _$MiraiStorageWidgetCopyWithImpl<$Res, MiraiStorageWidget>; + @useResult + $Res call({Map body, List storageKeys}); +} + +/// @nodoc +class _$MiraiStorageWidgetCopyWithImpl<$Res, $Val extends MiraiStorageWidget> + implements $MiraiStorageWidgetCopyWith<$Res> { + _$MiraiStorageWidgetCopyWithImpl(this._value, this._then); + + // ignore: unused_field + final $Val _value; + // ignore: unused_field + final $Res Function($Val) _then; + + @pragma('vm:prefer-inline') + @override + $Res call({ + Object? body = null, + Object? storageKeys = null, + }) { + return _then(_value.copyWith( + body: null == body + ? _value.body + : body // ignore: cast_nullable_to_non_nullable + as Map, + storageKeys: null == storageKeys + ? _value.storageKeys + : storageKeys // ignore: cast_nullable_to_non_nullable + as List, + ) as $Val); + } +} + +/// @nodoc +abstract class _$$_MiraiStorageWidgetCopyWith<$Res> + implements $MiraiStorageWidgetCopyWith<$Res> { + factory _$$_MiraiStorageWidgetCopyWith(_$_MiraiStorageWidget value, + $Res Function(_$_MiraiStorageWidget) then) = + __$$_MiraiStorageWidgetCopyWithImpl<$Res>; + @override + @useResult + $Res call({Map body, List storageKeys}); +} + +/// @nodoc +class __$$_MiraiStorageWidgetCopyWithImpl<$Res> + extends _$MiraiStorageWidgetCopyWithImpl<$Res, _$_MiraiStorageWidget> + implements _$$_MiraiStorageWidgetCopyWith<$Res> { + __$$_MiraiStorageWidgetCopyWithImpl( + _$_MiraiStorageWidget _value, $Res Function(_$_MiraiStorageWidget) _then) + : super(_value, _then); + + @pragma('vm:prefer-inline') + @override + $Res call({ + Object? body = null, + Object? storageKeys = null, + }) { + return _then(_$_MiraiStorageWidget( + body: null == body + ? _value._body + : body // ignore: cast_nullable_to_non_nullable + as Map, + storageKeys: null == storageKeys + ? _value._storageKeys + : storageKeys // ignore: cast_nullable_to_non_nullable + as List, + )); + } +} + +/// @nodoc +@JsonSerializable() +class _$_MiraiStorageWidget implements _MiraiStorageWidget { + const _$_MiraiStorageWidget( + {required final Map body, + final List storageKeys = const []}) + : _body = body, + _storageKeys = storageKeys; + + factory _$_MiraiStorageWidget.fromJson(Map json) => + _$$_MiraiStorageWidgetFromJson(json); + + final Map _body; + @override + Map get body { + if (_body is EqualUnmodifiableMapView) return _body; + // ignore: implicit_dynamic_type + return EqualUnmodifiableMapView(_body); + } + + final List _storageKeys; + @override + @JsonKey() + List get storageKeys { + if (_storageKeys is EqualUnmodifiableListView) return _storageKeys; + // ignore: implicit_dynamic_type + return EqualUnmodifiableListView(_storageKeys); + } + + @override + String toString() { + return 'MiraiStorageWidget(body: $body, storageKeys: $storageKeys)'; + } + + @override + bool operator ==(dynamic other) { + return identical(this, other) || + (other.runtimeType == runtimeType && + other is _$_MiraiStorageWidget && + const DeepCollectionEquality().equals(other._body, _body) && + const DeepCollectionEquality() + .equals(other._storageKeys, _storageKeys)); + } + + @JsonKey(ignore: true) + @override + int get hashCode => Object.hash( + runtimeType, + const DeepCollectionEquality().hash(_body), + const DeepCollectionEquality().hash(_storageKeys)); + + @JsonKey(ignore: true) + @override + @pragma('vm:prefer-inline') + _$$_MiraiStorageWidgetCopyWith<_$_MiraiStorageWidget> get copyWith => + __$$_MiraiStorageWidgetCopyWithImpl<_$_MiraiStorageWidget>( + this, _$identity); + + @override + Map toJson() { + return _$$_MiraiStorageWidgetToJson( + this, + ); + } +} + +abstract class _MiraiStorageWidget implements MiraiStorageWidget { + const factory _MiraiStorageWidget( + {required final Map body, + final List storageKeys}) = _$_MiraiStorageWidget; + + factory _MiraiStorageWidget.fromJson(Map json) = + _$_MiraiStorageWidget.fromJson; + + @override + Map get body; + @override + List get storageKeys; + @override + @JsonKey(ignore: true) + _$$_MiraiStorageWidgetCopyWith<_$_MiraiStorageWidget> get copyWith => + throw _privateConstructorUsedError; +} diff --git a/packages/mirai/lib/src/parsers/mirai_storage_widget/mirai_storage_widget.g.dart b/packages/mirai/lib/src/parsers/mirai_storage_widget/mirai_storage_widget.g.dart new file mode 100644 index 00000000..4e3a7a82 --- /dev/null +++ b/packages/mirai/lib/src/parsers/mirai_storage_widget/mirai_storage_widget.g.dart @@ -0,0 +1,24 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'mirai_storage_widget.dart'; + +// ************************************************************************** +// JsonSerializableGenerator +// ************************************************************************** + +_$_MiraiStorageWidget _$$_MiraiStorageWidgetFromJson( + Map json) => + _$_MiraiStorageWidget( + body: json['body'] as Map, + storageKeys: (json['storageKeys'] as List?) + ?.map((e) => e as String) + .toList() ?? + const [], + ); + +Map _$$_MiraiStorageWidgetToJson( + _$_MiraiStorageWidget instance) => + { + 'body': instance.body, + 'storageKeys': instance.storageKeys, + }; diff --git a/packages/mirai/lib/src/parsers/mirai_storage_widget/mirai_storage_widget_parser.dart b/packages/mirai/lib/src/parsers/mirai_storage_widget/mirai_storage_widget_parser.dart new file mode 100644 index 00000000..30782484 --- /dev/null +++ b/packages/mirai/lib/src/parsers/mirai_storage_widget/mirai_storage_widget_parser.dart @@ -0,0 +1,31 @@ +import 'package:flutter/material.dart'; +import 'package:mirai/src/framework/framework.dart'; +import 'package:mirai/src/parsers/mirai_storage_widget/mirai_storage_widget.dart'; +import 'package:mirai/src/utils/widget_type.dart'; + +class MiraiStorageWidgetParser extends MiraiParser { + const MiraiStorageWidgetParser(); + + @override + MiraiStorageWidget getModel(Map json) => + MiraiStorageWidget.fromJson(json); + + @override + String get type => WidgetType.storageWidget.name; + + @override + Widget parse(BuildContext context, MiraiStorageWidget model) { + return FutureBuilder( + future: Future.value(), + builder: (BuildContext context, AsyncSnapshot snapshot) { + if (snapshot.hasData) { + return Mirai.fromJson(model.body, context) ?? const SizedBox(); + } + + return const SizedBox( + child: CircularProgressIndicator(), + ); + }, + ); + } +} diff --git a/packages/mirai/lib/src/storage/mirai_storage.dart b/packages/mirai/lib/src/storage/mirai_storage.dart index d915366d..6d8257ad 100644 --- a/packages/mirai/lib/src/storage/mirai_storage.dart +++ b/packages/mirai/lib/src/storage/mirai_storage.dart @@ -3,12 +3,12 @@ import 'package:freezed_annotation/freezed_annotation.dart'; part 'mirai_storage.freezed.dart'; part 'mirai_storage.g.dart'; -enum MiraiStorageType { delete, store, retreive } +enum MiraiStorageType { write, read, delete } @freezed class MiraiStorage with _$MiraiStorage { factory MiraiStorage({ - @Default(MiraiStorageType.retreive) MiraiStorageType type, + @Default(MiraiStorageType.read) MiraiStorageType type, required String key, String? value, }) = _MiraiStorage; diff --git a/packages/mirai/lib/src/storage/mirai_storage.freezed.dart b/packages/mirai/lib/src/storage/mirai_storage.freezed.dart index 2c91c650..b078af1a 100644 --- a/packages/mirai/lib/src/storage/mirai_storage.freezed.dart +++ b/packages/mirai/lib/src/storage/mirai_storage.freezed.dart @@ -120,7 +120,7 @@ class __$$_MiraiStorageCopyWithImpl<$Res> @JsonSerializable() class _$_MiraiStorage implements _MiraiStorage { _$_MiraiStorage( - {this.type = MiraiStorageType.retreive, required this.key, this.value}); + {this.type = MiraiStorageType.read, required this.key, this.value}); factory _$_MiraiStorage.fromJson(Map json) => _$$_MiraiStorageFromJson(json); diff --git a/packages/mirai/lib/src/storage/mirai_storage.g.dart b/packages/mirai/lib/src/storage/mirai_storage.g.dart index df4159c8..71d73b1f 100644 --- a/packages/mirai/lib/src/storage/mirai_storage.g.dart +++ b/packages/mirai/lib/src/storage/mirai_storage.g.dart @@ -9,7 +9,7 @@ part of 'mirai_storage.dart'; _$_MiraiStorage _$$_MiraiStorageFromJson(Map json) => _$_MiraiStorage( type: $enumDecodeNullable(_$MiraiStorageTypeEnumMap, json['type']) ?? - MiraiStorageType.retreive, + MiraiStorageType.read, key: json['key'] as String, value: json['value'] as String?, ); @@ -22,7 +22,7 @@ Map _$$_MiraiStorageToJson(_$_MiraiStorage instance) => }; const _$MiraiStorageTypeEnumMap = { + MiraiStorageType.write: 'write', + MiraiStorageType.read: 'read', MiraiStorageType.delete: 'delete', - MiraiStorageType.store: 'store', - MiraiStorageType.retreive: 'retreive', }; diff --git a/packages/mirai/lib/src/storage/mirai_storage_parser.dart b/packages/mirai/lib/src/storage/mirai_storage_parser.dart deleted file mode 100644 index 4ff40390..00000000 --- a/packages/mirai/lib/src/storage/mirai_storage_parser.dart +++ /dev/null @@ -1,17 +0,0 @@ -import 'package:flutter_secure_storage/flutter_secure_storage.dart'; -import 'package:mirai/src/storage/mirai_storage.dart'; - -extension MiraiStorageParser on MiraiStorage? { - static const _storage = FlutterSecureStorage(); - - Future? onCall() async { - switch (this?.type ?? MiraiStorageType.retreive) { - case MiraiStorageType.store: - return _storage.write(key: this?.key ?? "", value: this?.value); - case MiraiStorageType.retreive: - return _storage.read(key: this?.key ?? ""); - case MiraiStorageType.delete: - return _storage.delete(key: this?.key ?? ""); - } - } -} diff --git a/packages/mirai/lib/src/storage/secure_storage.dart b/packages/mirai/lib/src/storage/secure_storage.dart new file mode 100644 index 00000000..ffe3ae60 --- /dev/null +++ b/packages/mirai/lib/src/storage/secure_storage.dart @@ -0,0 +1,39 @@ +import 'package:flutter_secure_storage/flutter_secure_storage.dart'; + +// extension MiraiStorageParser on MiraiStorage? { +// static const _storage = FlutterSecureStorage(); + +// Future? onCall() async { +// switch (this?.type ?? MiraiStorageType.retreive) { +// case MiraiStorageType.store: +// return _storage.write(key: this?.key ?? "", value: this?.value); +// case MiraiStorageType.retreive: +// return _storage.read(key: this?.key ?? ""); +// case MiraiStorageType.delete: +// return _storage.delete(key: this?.key ?? ""); +// } +// } +// } + +// import 'package:dio/dio.dart'; +// import 'package:mirai/src/network/mirai_request.dart'; + +class SecureStorage { + const SecureStorage._(); + + static late FlutterSecureStorage _flutterSecureStorage; + + static void initialize(FlutterSecureStorage flutterSecureStorage) => + _flutterSecureStorage = flutterSecureStorage; + + static Future deleteAll() => _flutterSecureStorage.deleteAll(); + + static Future delete(String key) => + _flutterSecureStorage.delete(key: key); + + static Future read(String key) => + _flutterSecureStorage.read(key: key); + + static Future write(String key, String value) => + _flutterSecureStorage.write(key: key, value: value); +} diff --git a/packages/mirai/lib/src/utils/widget_type.dart b/packages/mirai/lib/src/utils/widget_type.dart index 62643a24..829d7844 100644 --- a/packages/mirai/lib/src/utils/widget_type.dart +++ b/packages/mirai/lib/src/utils/widget_type.dart @@ -30,4 +30,5 @@ enum WidgetType { defaultTabController, scrollView, tab, + storageWidget, } From eccdd95d55f28e35c22b9973c63c29a04aa7083e Mon Sep 17 00:00:00 2001 From: Aasim Khan Date: Tue, 16 May 2023 14:02:03 +0400 Subject: [PATCH 4/5] Storage action types are renamed to read/write from retrieve/store --- .../assets/json/storage_example.json | 4 +- .../ios/Runner.xcodeproj/project.pbxproj | 1 + examples/mirai_gallery/pubspec.lock | 34 ++++----- packages/mirai/lib/src/framework/mirai.dart | 3 + .../mirai_storage_widget_parser.dart | 69 +++++++++++++++++-- 5 files changed, 89 insertions(+), 22 deletions(-) diff --git a/examples/mirai_gallery/assets/json/storage_example.json b/examples/mirai_gallery/assets/json/storage_example.json index 0a01049b..c659facd 100644 --- a/examples/mirai_gallery/assets/json/storage_example.json +++ b/examples/mirai_gallery/assets/json/storage_example.json @@ -9,7 +9,6 @@ }, "body": { "type": "column", - "mainAxisAlignment": "start", "crossAxisAlignment": "center", "children": [ { @@ -36,6 +35,7 @@ } }, "onPressed": { + "actionType": "storage", "storage": { "type": "write", "key": "name", @@ -62,6 +62,7 @@ } }, "onPressed": { + "actionType": "storage", "storage": { "type": "read", "key": "name" @@ -91,6 +92,7 @@ } }, "onPressed": { + "actionType": "storage", "storage": { "type": "delete", "key": "name" diff --git a/examples/mirai_gallery/ios/Runner.xcodeproj/project.pbxproj b/examples/mirai_gallery/ios/Runner.xcodeproj/project.pbxproj index 8fd0349d..9dfa1b21 100644 --- a/examples/mirai_gallery/ios/Runner.xcodeproj/project.pbxproj +++ b/examples/mirai_gallery/ios/Runner.xcodeproj/project.pbxproj @@ -222,6 +222,7 @@ files = ( ); inputPaths = ( + "${TARGET_BUILD_DIR}/${INFOPLIST_PATH}", ); name = "Thin Binary"; outputPaths = ( diff --git a/examples/mirai_gallery/pubspec.lock b/examples/mirai_gallery/pubspec.lock index c8c9a9d4..feb20b87 100644 --- a/examples/mirai_gallery/pubspec.lock +++ b/examples/mirai_gallery/pubspec.lock @@ -29,10 +29,10 @@ packages: dependency: transitive description: name: async - sha256: bfe67ef28df125b7dddcea62755991f807aa39a2492a23e1550161692950bbe0 + sha256: "947bfcf187f74dbc5e146c9eb9c0f10c9f8b30743e341481c1e2ed3ecc18c20c" url: "https://pub.dev" source: hosted - version: "2.10.0" + version: "2.11.0" bloc: dependency: transitive description: @@ -117,10 +117,10 @@ packages: dependency: transitive description: name: characters - sha256: e6a326c8af69605aec75ed6c187d06b349707a27fbff8222ca9cc2cff167975c + sha256: "04a925763edad70e8443c99234dc3328f442e811f1d8fd1a72f1c8ad0f69a605" url: "https://pub.dev" source: hosted - version: "1.2.1" + version: "1.3.0" checked_yaml: dependency: transitive description: @@ -149,10 +149,10 @@ packages: dependency: transitive description: name: collection - sha256: cfc915e6923fe5ce6e153b0723c753045de46de1b4d63771530504004a45fae0 + sha256: "4a07be6cb69c84d677a6c3096fcf960cc3285a8330b4603e0d463d15d9bd934c" url: "https://pub.dev" source: hosted - version: "1.17.0" + version: "1.17.1" convert: dependency: transitive description: @@ -364,10 +364,10 @@ packages: dependency: transitive description: name: js - sha256: "5528c2f391ededb7775ec1daa69e65a2d61276f7552de2b5f7b8d34ee9fd4ab7" + sha256: f2c445dce49627136094980615a031419f7f3eb393237e4ecd97ac15dea343f3 url: "https://pub.dev" source: hosted - version: "0.6.5" + version: "0.6.7" json_annotation: dependency: "direct main" description: @@ -412,10 +412,10 @@ packages: dependency: transitive description: name: matcher - sha256: "16db949ceee371e9b99d22f88fa3a73c4e59fd0afed0bd25fc336eb76c198b72" + sha256: "6501fbd55da300384b768785b83e5ce66991266cec21af89ab9ae7f5ce1c4cbb" url: "https://pub.dev" source: hosted - version: "0.12.13" + version: "0.12.15" material_color_utilities: dependency: transitive description: @@ -428,10 +428,10 @@ packages: dependency: transitive description: name: meta - sha256: "6c268b42ed578a53088d834796959e4a1814b5e9e164f147f580a386e5decf42" + sha256: "3c74dbf8763d36539f114c799d8a2d87343b5067e9d796ca22b5eb8437090ee3" url: "https://pub.dev" source: hosted - version: "1.8.0" + version: "1.9.1" mime: dependency: transitive description: @@ -467,10 +467,10 @@ packages: dependency: transitive description: name: path - sha256: db9d4f58c908a4ba5953fcee2ae317c94889433e5024c27ce74a37f94267945b + sha256: "8829d8a55c13fc0e37127c29fedf290c102f4e40ae94ada574091fe0ff96c917" url: "https://pub.dev" source: hosted - version: "1.8.2" + version: "1.8.3" plugin_platform_interface: dependency: transitive description: @@ -600,10 +600,10 @@ packages: dependency: transitive description: name: test_api - sha256: ad540f65f92caa91bf21dfc8ffb8c589d6e4dc0c2267818b4cc2792857706206 + sha256: eb6ac1540b26de412b3403a163d919ba86f6a973fe6cc50ae3541b80092fdcfb url: "https://pub.dev" source: hosted - version: "0.4.16" + version: "0.5.1" timing: dependency: transitive description: @@ -653,5 +653,5 @@ packages: source: hosted version: "3.1.1" sdks: - dart: ">=2.19.2 <3.0.0" + dart: ">=3.0.0-0 <4.0.0" flutter: ">=2.0.0" diff --git a/packages/mirai/lib/src/framework/mirai.dart b/packages/mirai/lib/src/framework/mirai.dart index 22588b16..2e3b327e 100644 --- a/packages/mirai/lib/src/framework/mirai.dart +++ b/packages/mirai/lib/src/framework/mirai.dart @@ -3,6 +3,7 @@ import 'dart:convert'; import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; +import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:mirai/src/framework/mirai_parser.dart'; import 'package:mirai/src/framework/mirai_registry.dart'; import 'package:mirai/src/network/mirai_network.dart'; @@ -12,6 +13,7 @@ import 'package:mirai/src/parsers/mirai_fractionally_sized_box/mirai_fractionall import 'package:mirai/src/parsers/mirai_storage_widget/mirai_storage_widget.dart'; import 'package:mirai/src/parsers/mirai_tab/mirai_tab_parser.dart'; import 'package:mirai/src/parsers/parsers.dart'; +import 'package:mirai/src/storage/secure_storage.dart'; import 'package:mirai/src/utils/log.dart'; typedef ErrorWidgetBuilder = Widget Function( @@ -64,6 +66,7 @@ class Mirai { _parsers.addAll(parsers); MiraiRegistry.instance.registerAll(_parsers); MiraiNetwork.initialize(dio ?? Dio()); + SecureStorage.initialize(const FlutterSecureStorage()); } static Widget? fromJson(Map? json, BuildContext context) { diff --git a/packages/mirai/lib/src/parsers/mirai_storage_widget/mirai_storage_widget_parser.dart b/packages/mirai/lib/src/parsers/mirai_storage_widget/mirai_storage_widget_parser.dart index 30782484..ba66e0f6 100644 --- a/packages/mirai/lib/src/parsers/mirai_storage_widget/mirai_storage_widget_parser.dart +++ b/packages/mirai/lib/src/parsers/mirai_storage_widget/mirai_storage_widget_parser.dart @@ -1,6 +1,8 @@ import 'package:flutter/material.dart'; import 'package:mirai/src/framework/framework.dart'; import 'package:mirai/src/parsers/mirai_storage_widget/mirai_storage_widget.dart'; +import 'package:mirai/src/storage/secure_storage.dart'; +import 'package:mirai/src/utils/log.dart'; import 'package:mirai/src/utils/widget_type.dart'; class MiraiStorageWidgetParser extends MiraiParser { @@ -15,17 +17,76 @@ class MiraiStorageWidgetParser extends MiraiParser { @override Widget parse(BuildContext context, MiraiStorageWidget model) { - return FutureBuilder( - future: Future.value(), + Map bodyJson = Map.from(model.body); + + return FutureBuilder>( + future: _loadDataFromStorage(model.storageKeys), builder: (BuildContext context, AsyncSnapshot snapshot) { if (snapshot.hasData) { - return Mirai.fromJson(model.body, context) ?? const SizedBox(); + snapshot.data.forEach((key, value) { + _updateBodyJson(bodyJson, key, value); + }); + + return Mirai.fromJson(bodyJson, context) ?? const SizedBox(); } - return const SizedBox( + return const Center( child: CircularProgressIndicator(), ); }, ); } + + Future> _loadDataFromStorage(List keys) async { + Map data = {}; + + for (String key in keys) { + final value = await SecureStorage.read(key) ?? ""; + data[key] = value; + } + + return data; + } + + _updateBodyJson(Map data, String key, dynamic value) { + for (MapEntry mapEntry in data.entries) { + if (key.sameKeySymbol("${mapEntry.value}")) { + try { + data.update( + mapEntry.key, + (existingValue) => "$value", + ); + } catch (e) { + Log.e(e); + } + + break; + } + + if (mapEntry.value is Map) { + _updateBodyJson(mapEntry.value, key, value); + } + + if (mapEntry.value is List) { + for (Map listItem in mapEntry.value) { + _updateBodyJson(listItem, key, value); + } + } + } + } +} + +extension StringExt on String { + bool sameKeySymbol(String value) { + if (value == this) { + return true; + } else if (contains("\$") || value.contains("\$")) { + if (replaceAll(RegExp(r'[^\w\s]+'), '') == + value.replaceAll(RegExp(r'[^\w\s]+'), '')) { + return true; + } + } + + return false; + } } From fdbf09bb5d0764c7a01333ca9bea0aae1386a799 Mon Sep 17 00:00:00 2001 From: Aasim Khan Date: Thu, 18 May 2023 14:13:18 +0400 Subject: [PATCH 5/5] Removed StorageType from the Storage type Mirai Action --- .../lib/src/action/mirai_action_parser.dart | 5 ++--- .../mirai/lib/src/storage/mirai_storage.dart | 4 ++-- .../src/storage/mirai_storage.freezed.dart | 2 +- .../lib/src/storage/mirai_storage.g.dart | 3 +-- .../mirai/lib/src/storage/secure_storage.dart | 22 ------------------- 5 files changed, 6 insertions(+), 30 deletions(-) diff --git a/packages/mirai/lib/src/action/mirai_action_parser.dart b/packages/mirai/lib/src/action/mirai_action_parser.dart index c41f1001..0890855f 100644 --- a/packages/mirai/lib/src/action/mirai_action_parser.dart +++ b/packages/mirai/lib/src/action/mirai_action_parser.dart @@ -55,12 +55,11 @@ extension MiraiActionParser on MiraiAction? { return MiraiNetwork.request(this!.request!); case ActionType.storage: - switch (this?.storage?.type ?? MiraiStorageType.read) { + switch (this?.storage?.type ?? MiraiStorageType.write) { case MiraiStorageType.write: return SecureStorage.write( this?.storage?.key ?? "", this?.storage?.value ?? ""); - case MiraiStorageType.read: - return SecureStorage.read(this?.storage?.key ?? ""); + case MiraiStorageType.delete: return SecureStorage.delete(this?.storage?.key ?? ""); } diff --git a/packages/mirai/lib/src/storage/mirai_storage.dart b/packages/mirai/lib/src/storage/mirai_storage.dart index 6d8257ad..7c5cd3a3 100644 --- a/packages/mirai/lib/src/storage/mirai_storage.dart +++ b/packages/mirai/lib/src/storage/mirai_storage.dart @@ -3,12 +3,12 @@ import 'package:freezed_annotation/freezed_annotation.dart'; part 'mirai_storage.freezed.dart'; part 'mirai_storage.g.dart'; -enum MiraiStorageType { write, read, delete } +enum MiraiStorageType { write, delete } @freezed class MiraiStorage with _$MiraiStorage { factory MiraiStorage({ - @Default(MiraiStorageType.read) MiraiStorageType type, + @Default(MiraiStorageType.write) MiraiStorageType type, required String key, String? value, }) = _MiraiStorage; diff --git a/packages/mirai/lib/src/storage/mirai_storage.freezed.dart b/packages/mirai/lib/src/storage/mirai_storage.freezed.dart index b078af1a..2ee4e507 100644 --- a/packages/mirai/lib/src/storage/mirai_storage.freezed.dart +++ b/packages/mirai/lib/src/storage/mirai_storage.freezed.dart @@ -120,7 +120,7 @@ class __$$_MiraiStorageCopyWithImpl<$Res> @JsonSerializable() class _$_MiraiStorage implements _MiraiStorage { _$_MiraiStorage( - {this.type = MiraiStorageType.read, required this.key, this.value}); + {this.type = MiraiStorageType.write, required this.key, this.value}); factory _$_MiraiStorage.fromJson(Map json) => _$$_MiraiStorageFromJson(json); diff --git a/packages/mirai/lib/src/storage/mirai_storage.g.dart b/packages/mirai/lib/src/storage/mirai_storage.g.dart index 71d73b1f..f37822bd 100644 --- a/packages/mirai/lib/src/storage/mirai_storage.g.dart +++ b/packages/mirai/lib/src/storage/mirai_storage.g.dart @@ -9,7 +9,7 @@ part of 'mirai_storage.dart'; _$_MiraiStorage _$$_MiraiStorageFromJson(Map json) => _$_MiraiStorage( type: $enumDecodeNullable(_$MiraiStorageTypeEnumMap, json['type']) ?? - MiraiStorageType.read, + MiraiStorageType.write, key: json['key'] as String, value: json['value'] as String?, ); @@ -23,6 +23,5 @@ Map _$$_MiraiStorageToJson(_$_MiraiStorage instance) => const _$MiraiStorageTypeEnumMap = { MiraiStorageType.write: 'write', - MiraiStorageType.read: 'read', MiraiStorageType.delete: 'delete', }; diff --git a/packages/mirai/lib/src/storage/secure_storage.dart b/packages/mirai/lib/src/storage/secure_storage.dart index ffe3ae60..f2ff9090 100644 --- a/packages/mirai/lib/src/storage/secure_storage.dart +++ b/packages/mirai/lib/src/storage/secure_storage.dart @@ -1,39 +1,17 @@ import 'package:flutter_secure_storage/flutter_secure_storage.dart'; -// extension MiraiStorageParser on MiraiStorage? { -// static const _storage = FlutterSecureStorage(); - -// Future? onCall() async { -// switch (this?.type ?? MiraiStorageType.retreive) { -// case MiraiStorageType.store: -// return _storage.write(key: this?.key ?? "", value: this?.value); -// case MiraiStorageType.retreive: -// return _storage.read(key: this?.key ?? ""); -// case MiraiStorageType.delete: -// return _storage.delete(key: this?.key ?? ""); -// } -// } -// } - -// import 'package:dio/dio.dart'; -// import 'package:mirai/src/network/mirai_request.dart'; - class SecureStorage { const SecureStorage._(); static late FlutterSecureStorage _flutterSecureStorage; - static void initialize(FlutterSecureStorage flutterSecureStorage) => _flutterSecureStorage = flutterSecureStorage; static Future deleteAll() => _flutterSecureStorage.deleteAll(); - static Future delete(String key) => _flutterSecureStorage.delete(key: key); - static Future read(String key) => _flutterSecureStorage.read(key: key); - static Future write(String key, String value) => _flutterSecureStorage.write(key: key, value: value); }