Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrating to null safety AND android embedding v2 #81

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/.flutter-plugins-dependencies
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"sqflite","path":"/Users/silvertree/.pub-cache/hosted/pub.dartlang.org/sqflite-1.3.1+1/","dependencies":[]}],"android":[{"name":"sqflite","path":"/Users/silvertree/.pub-cache/hosted/pub.dartlang.org/sqflite-1.3.1+1/","dependencies":[]}],"macos":[{"name":"sqflite","path":"/Users/silvertree/.pub-cache/hosted/pub.dartlang.org/sqflite-1.3.1+1/","dependencies":[]}],"linux":[],"windows":[],"web":[]},"dependencyGraph":[{"name":"sqflite","dependencies":[]}],"date_created":"2020-09-24 18:44:13.977860","version":"1.20.3"}
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"sqflite","path":"D:\\\\Users\\\\shina\\\\Desenvolvimento\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\sqflite-2.0.0+1\\\\","dependencies":[]}],"android":[{"name":"sqflite","path":"D:\\\\Users\\\\shina\\\\Desenvolvimento\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\sqflite-2.0.0+1\\\\","dependencies":[]}],"macos":[{"name":"sqflite","path":"D:\\\\Users\\\\shina\\\\Desenvolvimento\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\sqflite-2.0.0+1\\\\","dependencies":[]}],"linux":[],"windows":[],"web":[]},"dependencyGraph":[{"name":"sqflite","dependencies":[]}],"date_created":"2021-03-03 19:33:12.008560","version":"2.0.0"}
5 changes: 4 additions & 1 deletion example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
additional functionality it is fine to subclass or reimplement
FlutterApplication and put your custom class here. -->
<application
android:name="io.flutter.app.FlutterApplication"
android:label="CacheExample"
android:icon="@mipmap/ic_launcher">
<activity
Expand All @@ -29,5 +28,9 @@
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

<meta-data
android:name="flutterEmbedding"
android:value="2" />
</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
package com.example.example;

import android.os.Bundle;
import io.flutter.app.FlutterActivity;
import io.flutter.plugins.GeneratedPluginRegistrant;
import io.flutter.embedding.android.FlutterActivity;

public class MainActivity extends FlutterActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
}


}
10 changes: 4 additions & 6 deletions example/ios/Flutter/flutter_export_environment.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#!/bin/sh
# This is a generated file; do not edit or check into version control.
export "FLUTTER_ROOT=/Users/silvertree/Documents/dev/flutter"
export "FLUTTER_APPLICATION_PATH=/Users/silvertree/Documents/codes/dio-http-cache/example"
export "FLUTTER_TARGET=lib/main.dart"
export "FLUTTER_ROOT=D:\Users\shina\Desenvolvimento\flutter"
export "FLUTTER_APPLICATION_PATH=D:\Users\shina\Desenvolvimento\flutter_projecs\dio-http-cache\example"
export "FLUTTER_TARGET=lib\main.dart"
export "FLUTTER_BUILD_DIR=build"
export "SYMROOT=${SOURCE_ROOT}/../build/ios"
export "OTHER_LDFLAGS=$(inherited) -framework Flutter"
export "FLUTTER_FRAMEWORK_DIR=/Users/silvertree/Documents/dev/flutter/bin/cache/artifacts/engine/ios"
export "SYMROOT=${SOURCE_ROOT}/../build\ios"
export "FLUTTER_BUILD_NAME=1.0.0"
export "FLUTTER_BUILD_NUMBER=1"
export "DART_OBFUSCATION=false"
Expand Down
48 changes: 24 additions & 24 deletions lib/src/builder_dio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import 'package:dio_http_cache/src/manager_dio.dart';
/// try to get maxAge and maxStale from response headers.
/// local settings will always overview the value get from service.
Options buildServiceCacheOptions(
{Options options,
Duration maxStale,
String primaryKey,
String subKey,
bool forceRefresh}) =>
{Options? options,
Duration? maxStale,
String? primaryKey,
String? subKey,
bool? forceRefresh}) =>
buildConfigurableCacheOptions(
options: options,
maxStale: maxStale,
Expand All @@ -18,11 +18,11 @@ Options buildServiceCacheOptions(

/// build a normal cache options
Options buildCacheOptions(Duration maxAge,
{Duration maxStale,
String primaryKey,
String subKey,
Options options,
bool forceRefresh}) =>
{Duration? maxStale,
String? primaryKey,
String? subKey,
Options? options,
bool? forceRefresh}) =>
buildConfigurableCacheOptions(
maxAge: maxAge,
options: options,
Expand All @@ -34,32 +34,32 @@ Options buildCacheOptions(Duration maxAge,
/// if null==maxAge, will try to get maxAge and maxStale from response headers.
/// local settings will always overview the value get from service.
Options buildConfigurableCacheOptions(
{Options options,
Duration maxAge,
Duration maxStale,
String primaryKey,
String subKey,
bool forceRefresh}) {
if (null == options) {
options = Options();
{Options? options,
Duration? maxAge,
Duration? maxStale,
String? primaryKey,
String? subKey,
bool? forceRefresh}) {
if (options == null) {
options = Options(extra: {});
} else if (options.responseType == ResponseType.stream) {
throw Exception("ResponseType.stream is not supported");
}
options.extra.addAll({DIO_CACHE_KEY_TRY_CACHE: true});
options.extra?.addAll({DIO_CACHE_KEY_TRY_CACHE: true});
if (null != maxAge) {
options.extra.addAll({DIO_CACHE_KEY_MAX_AGE: maxAge});
options.extra?.addAll({DIO_CACHE_KEY_MAX_AGE: maxAge});
}
if (null != maxStale) {
options.extra.addAll({DIO_CACHE_KEY_MAX_STALE: maxStale});
options.extra?.addAll({DIO_CACHE_KEY_MAX_STALE: maxStale});
}
if (null != primaryKey) {
options.extra.addAll({DIO_CACHE_KEY_PRIMARY_KEY: primaryKey});
options.extra?.addAll({DIO_CACHE_KEY_PRIMARY_KEY: primaryKey});
}
if (null != subKey) {
options.extra.addAll({DIO_CACHE_KEY_SUB_KEY: subKey});
options.extra?.addAll({DIO_CACHE_KEY_SUB_KEY: subKey});
}
if (null != forceRefresh) {
options.extra.addAll({DIO_CACHE_KEY_FORCE_REFRESH: forceRefresh});
options.extra?.addAll({DIO_CACHE_KEY_FORCE_REFRESH: forceRefresh});
}
return options;
}
12 changes: 6 additions & 6 deletions lib/src/core/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ typedef Future<List<int>> Decrypt(List<int> str);

class CacheConfig {
final Duration defaultMaxAge;
final Duration defaultMaxStale;
final String databasePath;
final Duration? defaultMaxStale;
final String? databasePath;
final String databaseName;
final String baseUrl;
final String? baseUrl;
final String defaultRequestMethod;

final bool skipMemoryCache;
final bool skipDiskCache;

final int maxMemoryCacheCount;

final Encrypt encrypt;
final Decrypt decrypt;
final ICacheStore diskStore;
final Encrypt? encrypt;
final Decrypt? decrypt;
final ICacheStore? diskStore;

CacheConfig(
{this.defaultMaxAge = const Duration(days: 7),
Expand Down
50 changes: 25 additions & 25 deletions lib/src/core/manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import 'package:sqflite/utils/utils.dart';

class CacheManager {
CacheConfig _config;
ICacheStore _diskCacheStore;
ICacheStore _memoryCacheStore;
MD5 _md5;
Utf8Encoder _utf8encoder;
ICacheStore? _diskCacheStore;
ICacheStore? _memoryCacheStore;
late Hash _md5;
late Utf8Encoder _utf8encoder;

CacheManager(this._config) {
_md5 = md5;
Expand All @@ -26,7 +26,7 @@ class CacheManager {
_memoryCacheStore = MemoryCacheStore(_config.maxMemoryCacheCount);
}

Future<CacheObj> _pullFromCache(String key, {String subKey}) async {
Future<CacheObj?> _pullFromCache(String key, {String? subKey}) async {
key = _convertMd5(key);
if (null != subKey) subKey = _convertMd5(subKey);
var obj = await _memoryCacheStore?.getCacheObj(key, subKey: subKey);
Expand All @@ -36,15 +36,15 @@ class CacheManager {
}
if (null != obj) {
var now = DateTime.now().millisecondsSinceEpoch;
if (null != obj.maxStaleDate && obj.maxStaleDate > 0) {
if (null != obj.maxStaleDate && obj.maxStaleDate! > 0) {
//if maxStaleDate exist, Remove it if maxStaleDate expired.
if (obj.maxStaleDate < now) {
if (obj.maxStaleDate! < now) {
await delete(key, subKey: subKey);
return null;
}
} else {
//if maxStaleDate NOT exist, Remove it if maxAgeDate expired.
if (obj.maxAgeDate < now) {
if (obj.maxAgeDate! < now) {
await delete(key, subKey: subKey);
return null;
}
Expand All @@ -53,33 +53,33 @@ class CacheManager {
return obj;
}

Future<CacheObj> pullFromCacheBeforeMaxAge(String key,
{String subKey}) async {
Future<CacheObj?> pullFromCacheBeforeMaxAge(String key,
{String? subKey}) async {
var obj = await _pullFromCache(key, subKey: subKey);
if (null != obj &&
null != obj.maxAgeDate &&
obj.maxAgeDate < DateTime.now().millisecondsSinceEpoch) {
obj.maxAgeDate! < DateTime.now().millisecondsSinceEpoch) {
return null;
}
return obj;
}

Future<CacheObj> pullFromCacheBeforeMaxStale(String key,
{String subKey}) async {
Future<CacheObj?> pullFromCacheBeforeMaxStale(String key,
{String? subKey}) async {
return await _pullFromCache(key, subKey: subKey);
}

Future<bool> pushToCache(CacheObj obj) {
obj.key = _convertMd5(obj.key);
if (null != obj.subKey) obj.subKey = _convertMd5(obj.subKey);
obj.key = _convertMd5(obj.key!);
if (null != obj.subKey) obj.subKey = _convertMd5(obj.subKey!);

if (null == obj.maxAgeDate || obj.maxAgeDate <= 0) {
if (null == obj.maxAgeDate || obj.maxAgeDate! <= 0) {
obj.maxAge = _config.defaultMaxAge;
}
if (null == obj.maxAgeDate || obj.maxAgeDate <= 0) {
if (null == obj.maxAgeDate || obj.maxAgeDate! <= 0) {
return Future.value(false);
}
if ((null == obj.maxStaleDate || obj.maxStaleDate <= 0) &&
if ((null == obj.maxStaleDate || obj.maxStaleDate! <= 0) &&
null != _config.defaultMaxStale) {
obj.maxStale = _config.defaultMaxStale;
}
Expand All @@ -88,7 +88,7 @@ class CacheManager {
_memoryCacheStore?.setCacheObj(obj), _diskCacheStore?.setCacheObj(obj));
}

Future<bool> delete(String key, {String subKey}) {
Future<bool> delete(String key, {String? subKey}) {
key = _convertMd5(key);
if (null != subKey) subKey = _convertMd5(subKey);

Expand All @@ -114,12 +114,12 @@ class CacheManager {
}

Future<bool> _getCacheFutureResult(
ICacheStore memoryCacheStore,
ICacheStore diskCacheStore,
Future<bool> memoryCacheFuture,
Future<bool> diskCacheFuture) async {
var result1 = (null == memoryCacheStore) ? true : (await memoryCacheFuture);
var result2 = (null == diskCacheStore) ? true : (await diskCacheFuture);
ICacheStore? memoryCacheStore,
ICacheStore? diskCacheStore,
Future<bool>? memoryCacheFuture,
Future<bool>? diskCacheFuture) async {
var result1 = (null == memoryCacheStore) ? true : (await memoryCacheFuture!);
var result2 = (null == diskCacheStore) ? true : (await diskCacheFuture!);
return result1 && result2;
}
}
30 changes: 15 additions & 15 deletions lib/src/core/obj.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,35 @@ part 'obj.g.dart';

@JsonSerializable()
class CacheObj {
String key;
String subKey;
String? key;
String? subKey;
@JsonKey(name: "max_age_date")
int maxAgeDate;
int? maxAgeDate;
@JsonKey(name: "max_stale_date")
int maxStaleDate;
List<int> content;
int statusCode;
List<int> headers;
int? maxStaleDate;
List<int>? content;
int? statusCode;
List<int>? headers;

CacheObj._(
this.key, this.subKey, this.content, this.statusCode, this.headers);

factory CacheObj(String key, List<int> content,
{String subKey = "",
Duration maxAge,
Duration maxStale,
int statusCode = 200,
List<int> headers}) {
factory CacheObj(String? key, List<int>? content,
{String? subKey = "",
Duration? maxAge,
Duration? maxStale,
int? statusCode = 200,
List<int>? headers}) {
return CacheObj._(key, subKey, content, statusCode, headers)
..maxAge = maxAge
..maxStale = maxStale;
}

set maxAge(Duration duration) {
set maxAge(Duration? duration) {
if (null != duration) this.maxAgeDate = _convertDuration(duration);
}

set maxStale(Duration duration) {
set maxStale(Duration? duration) {
if (null != duration) this.maxStaleDate = _convertDuration(duration);
}

Expand Down
14 changes: 7 additions & 7 deletions lib/src/core/obj.g.dart

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

Loading