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

Add: add Storage Model #208

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
36 changes: 36 additions & 0 deletions lib/extensions/storage_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import 'package:flutter/material.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';

class StorageModel extends ChangeNotifier {
Future<bool> save(String key, String value) async {

Check warning on line 5 in lib/extensions/storage_model.dart

View check run for this annotation

Codecov / codecov/patch

lib/extensions/storage_model.dart#L5

Added line #L5 was not covered by tests
try {
final storage = FlutterSecureStorage();
await storage.write(key: key, value: value);

Check warning on line 8 in lib/extensions/storage_model.dart

View check run for this annotation

Codecov / codecov/patch

lib/extensions/storage_model.dart#L7-L8

Added lines #L7 - L8 were not covered by tests
return true;
} catch (exception) {
print(exception);

Check warning on line 11 in lib/extensions/storage_model.dart

View check run for this annotation

Codecov / codecov/patch

lib/extensions/storage_model.dart#L11

Added line #L11 was not covered by tests
return false;
}
}

Future<String?> read(String key) async {

Check warning on line 16 in lib/extensions/storage_model.dart

View check run for this annotation

Codecov / codecov/patch

lib/extensions/storage_model.dart#L16

Added line #L16 was not covered by tests
try {
final storage = FlutterSecureStorage();
return await storage.read(key: key);

Check warning on line 19 in lib/extensions/storage_model.dart

View check run for this annotation

Codecov / codecov/patch

lib/extensions/storage_model.dart#L18-L19

Added lines #L18 - L19 were not covered by tests
} catch (exception) {
print(exception);

Check warning on line 21 in lib/extensions/storage_model.dart

View check run for this annotation

Codecov / codecov/patch

lib/extensions/storage_model.dart#L21

Added line #L21 was not covered by tests
return null;
}
}

Future<bool> delete(String key) async {

Check warning on line 26 in lib/extensions/storage_model.dart

View check run for this annotation

Codecov / codecov/patch

lib/extensions/storage_model.dart#L26

Added line #L26 was not covered by tests
try {
final storage = FlutterSecureStorage();
await storage.delete(key: key);

Check warning on line 29 in lib/extensions/storage_model.dart

View check run for this annotation

Codecov / codecov/patch

lib/extensions/storage_model.dart#L28-L29

Added lines #L28 - L29 were not covered by tests
return true;
} catch (exception) {
print(exception);

Check warning on line 32 in lib/extensions/storage_model.dart

View check run for this annotation

Codecov / codecov/patch

lib/extensions/storage_model.dart#L32

Added line #L32 was not covered by tests
return false;
}
}
}
6 changes: 5 additions & 1 deletion lib/pages/login_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@
}
},
);

if (!context.read<AuthModel>().isLogined) {
context.read<AuthModel>().authenticateFromStorage().then((value) {
print("hello");

Check warning on line 44 in lib/pages/login_page.dart

View check run for this annotation

Codecov / codecov/patch

lib/pages/login_page.dart#L42-L44

Added lines #L42 - L44 were not covered by tests
});
}
const AUTHORITY = 'otl.sparcs.org';
Map<String, dynamic> query = {'next': 'https://otl.sparcs.org/'};
if (Platform.isIOS) {
Expand Down
35 changes: 34 additions & 1 deletion lib/providers/auth_model.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:otlplus/dio_provider.dart';
import 'package:otlplus/extensions/storage_model.dart';
import 'package:webview_cookie_manager/webview_cookie_manager.dart';

class AuthModel extends ChangeNotifier {
class AuthModel extends StorageModel {
bool _isLogined = false;
bool get isLogined => _isLogined;

Expand All @@ -13,6 +16,35 @@
DioProvider().authenticate(cookies);
_isLogined = true;
notifyListeners();
save('cookies', cookies.map((cookie) => cookie.toString()).join(';'));

Check warning on line 19 in lib/providers/auth_model.dart

View check run for this annotation

Codecov / codecov/patch

lib/providers/auth_model.dart#L19

Added line #L19 was not covered by tests
} catch (exception) {
print(exception);

Check warning on line 21 in lib/providers/auth_model.dart

View check run for this annotation

Codecov / codecov/patch

lib/providers/auth_model.dart#L21

Added line #L21 was not covered by tests
}
}

Future<void> authenticateWithCookies(List<Cookie> cookies) async {

Check warning on line 25 in lib/providers/auth_model.dart

View check run for this annotation

Codecov / codecov/patch

lib/providers/auth_model.dart#L25

Added line #L25 was not covered by tests
try {
DioProvider().authenticate(cookies);
_isLogined = true;
notifyListeners();

Check warning on line 29 in lib/providers/auth_model.dart

View check run for this annotation

Codecov / codecov/patch

lib/providers/auth_model.dart#L27-L29

Added lines #L27 - L29 were not covered by tests
} catch (exception) {
print(exception);

Check warning on line 31 in lib/providers/auth_model.dart

View check run for this annotation

Codecov / codecov/patch

lib/providers/auth_model.dart#L31

Added line #L31 was not covered by tests
}
}

Future<void> authenticateFromStorage() async {

Check warning on line 35 in lib/providers/auth_model.dart

View check run for this annotation

Codecov / codecov/patch

lib/providers/auth_model.dart#L35

Added line #L35 was not covered by tests
try {
final cookies_string = await read('cookies');
print("IT CALLED");
print(cookies_string);

Check warning on line 39 in lib/providers/auth_model.dart

View check run for this annotation

Codecov / codecov/patch

lib/providers/auth_model.dart#L37-L39

Added lines #L37 - L39 were not covered by tests
if (cookies_string != null) {
final cookies = cookies_string //
.split(';')
.map((cookie) => Cookie.fromSetCookieValue(cookie))
.toList();

Check warning on line 44 in lib/providers/auth_model.dart

View check run for this annotation

Codecov / codecov/patch

lib/providers/auth_model.dart#L42-L44

Added lines #L42 - L44 were not covered by tests

authenticateWithCookies(cookies);

Check warning on line 46 in lib/providers/auth_model.dart

View check run for this annotation

Codecov / codecov/patch

lib/providers/auth_model.dart#L46

Added line #L46 was not covered by tests
}
} catch (exception) {
print(exception);
}
Expand All @@ -24,6 +56,7 @@
// cookieManager.removeCookie(url);
cookieManager.clearCookies();
DioProvider().logout();
delete('cookies');

Check warning on line 59 in lib/providers/auth_model.dart

View check run for this annotation

Codecov / codecov/patch

lib/providers/auth_model.dart#L59

Added line #L59 was not covered by tests
_isLogined = false;
notifyListeners();
} catch (exception) {
Expand Down
54 changes: 31 additions & 23 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ packages:
dependency: transitive
description:
name: collection
sha256: ee67cb0715911d28db6bf4af1026078bd6f0128b07a5f66fb2ed94ec6783c09a
sha256: a1ace0a119f20aabc852d165077c036cd864315bd99b7eaa10a60100341941bf
url: "https://pub.dev"
source: hosted
version: "1.18.0"
version: "1.19.0"
convert:
dependency: transitive
description:
Expand Down Expand Up @@ -206,10 +206,10 @@ packages:
dependency: transitive
description:
name: file
sha256: "5fc22d7c25582e38ad9a8515372cd9a93834027aacf1801cf01164dac0ffa08c"
sha256: a3b4f84adafef897088c160faf7dfffb7696046cb13ae90b508c2cbc95d3b8d4
url: "https://pub.dev"
source: hosted
version: "7.0.0"
version: "7.0.1"
firebase_analytics:
dependency: "direct main"
description:
Expand Down Expand Up @@ -321,6 +321,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.4.2"
flutter_secure_storage:
dependency: "direct main"
description:
name: flutter_secure_storage
sha256: "9f3dd2ac3b6875b0fde5b04734789c3ef35ba3965c18e99dd564a7a2f8056df6"
url: "https://pub.dev"
source: hosted
version: "4.2.1"
flutter_svg:
dependency: "direct main"
description:
Expand Down Expand Up @@ -444,18 +452,18 @@ packages:
dependency: transitive
description:
name: leak_tracker
sha256: "3f87a60e8c63aecc975dda1ceedbc8f24de75f09e4856ea27daf8958f2f0ce05"
sha256: c35baad643ba394b40aac41080300150a4f08fd0fd6a10378f8f7c6bc161acec
url: "https://pub.dev"
source: hosted
version: "10.0.5"
version: "10.0.8"
leak_tracker_flutter_testing:
dependency: transitive
description:
name: leak_tracker_flutter_testing
sha256: "932549fb305594d82d7183ecd9fa93463e9914e1b67cacc34bc40906594a1806"
sha256: f8b613e7e6a13ec79cfdc0e97638fddb3ab848452eff057653abd3edba760573
url: "https://pub.dev"
source: hosted
version: "3.0.5"
version: "3.0.9"
leak_tracker_testing:
dependency: transitive
description:
Expand Down Expand Up @@ -812,7 +820,7 @@ packages:
dependency: transitive
description: flutter
source: sdk
version: "0.0.99"
version: "0.0.0"
source_map_stack_trace:
dependency: transitive
description:
Expand Down Expand Up @@ -841,10 +849,10 @@ packages:
dependency: transitive
description:
name: stack_trace
sha256: "73713990125a6d93122541237550ee3352a2d84baad52d375a4cad2eb9b7ce0b"
sha256: "9f47fd3630d76be3ab26f0ee06d213679aa425996925ff3feffdec504931c377"
url: "https://pub.dev"
source: hosted
version: "1.11.1"
version: "1.12.0"
stream_channel:
dependency: transitive
description:
Expand All @@ -857,10 +865,10 @@ packages:
dependency: transitive
description:
name: string_scanner
sha256: "556692adab6cfa87322a115640c11f13cb77b3f076ddcc5d6ae3c20242bedcde"
sha256: "688af5ed3402a4bde5b3a6c15fd768dbf2621a614950b17f04626c431ab3c4c3"
url: "https://pub.dev"
source: hosted
version: "1.2.0"
version: "1.3.0"
sync_http:
dependency: transitive
description:
Expand All @@ -881,26 +889,26 @@ packages:
dependency: "direct dev"
description:
name: test
sha256: "7ee44229615f8f642b68120165ae4c2a75fe77ae2065b1e55ae4711f6cf0899e"
sha256: "713a8789d62f3233c46b4a90b174737b2c04cb6ae4500f2aa8b1be8f03f5e67f"
url: "https://pub.dev"
source: hosted
version: "1.25.7"
version: "1.25.8"
test_api:
dependency: transitive
description:
name: test_api
sha256: "5b8a98dafc4d5c4c9c72d8b31ab2b23fc13422348d2997120294d3bac86b4ddb"
sha256: "664d3a9a64782fcdeb83ce9c6b39e78fd2971d4e37827b9b06c3aa1edc5e760c"
url: "https://pub.dev"
source: hosted
version: "0.7.2"
version: "0.7.3"
test_core:
dependency: transitive
description:
name: test_core
sha256: "55ea5a652e38a1dfb32943a7973f3681a60f872f8c3a05a14664ad54ef9c6696"
sha256: "12391302411737c176b0b5d6491f466b0dd56d4763e347b6714efbaa74d7953d"
url: "https://pub.dev"
source: hosted
version: "0.6.4"
version: "0.6.5"
typed_data:
dependency: transitive
description:
Expand Down Expand Up @@ -1017,10 +1025,10 @@ packages:
dependency: transitive
description:
name: vm_service
sha256: "5c5f338a667b4c644744b661f309fb8080bb94b18a7e91ef1dbd343bed00ed6d"
sha256: f6be3ed8bd01289b34d679c2b62226f63c0e69f9fd2e50a6b3c1c729a961041b
url: "https://pub.dev"
source: hosted
version: "14.2.5"
version: "14.3.0"
watcher:
dependency: transitive
description:
Expand Down Expand Up @@ -1049,10 +1057,10 @@ packages:
dependency: transitive
description:
name: webdriver
sha256: "003d7da9519e1e5f329422b36c4dcdf18d7d2978d1ba099ea4e45ba490ed845e"
sha256: "3d773670966f02a646319410766d3b5e1037efb7f07cc68f844d5e06cd4d61c8"
url: "https://pub.dev"
source: hosted
version: "3.0.3"
version: "3.0.4"
webkit_inspection_protocol:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ dependencies:
git:
url: https://github.com/happycastle114/channel_talk_flutter
ref: main
flutter_secure_storage: ^4.2.1

dev_dependencies:
test: ^1.25.7
Expand Down
Loading