-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
192 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import 'package:dartz/dartz.dart'; | ||
import 'package:flutter_template/core/shared/data/data_source/error/failure.dart'; | ||
import 'package:flutter_template/core/shared/data/model/user_model.dart'; | ||
|
||
abstract class IUserCacheService { | ||
String get storageKey; | ||
|
||
Future<Either<Failure, User>> fetchUser(); | ||
Future<bool> saveUser({required User user}); | ||
Future<bool> deleteUser(); | ||
Future<bool> hasUser(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:dartz/dartz.dart'; | ||
import 'package:flutter_template/core/app/res/const/app_constant.dart'; | ||
import 'package:flutter_template/core/app/service/interface/i_user_cache_service.dart'; | ||
import 'package:flutter_template/core/shared/data/data_source/error/failure.dart'; | ||
import 'package:flutter_template/core/shared/data/data_source/local/interface/storage_service.dart'; | ||
import 'package:flutter_template/core/shared/data/model/user_model.dart'; | ||
|
||
class UserCacheService implements IUserCacheService { | ||
UserCacheService(this.storageService); | ||
|
||
final StorageService storageService; | ||
|
||
@override | ||
String get storageKey => AppConstant.userLocalStorageKey; | ||
|
||
@override | ||
Future<Either<Failure, User>> fetchUser() async { | ||
final data = await storageService.get(storageKey); | ||
if (data == null) { | ||
return const Left(CacheFailure()); | ||
} | ||
final userJson = jsonDecode(data.toString()); | ||
|
||
return Right(User.fromJson(userJson)); | ||
} | ||
|
||
@override | ||
Future<bool> saveUser({required User user}) async { | ||
return await storageService.set(storageKey, jsonEncode(user.toJson())); | ||
} | ||
|
||
@override | ||
Future<bool> deleteUser() async { | ||
return await storageService.remove(storageKey); | ||
} | ||
|
||
@override | ||
Future<bool> hasUser() async { | ||
return await storageService.has(storageKey); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
lib/core/shared/data/data_source/local/interface/storage_service.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/// Storage service interface | ||
abstract class StorageService { | ||
void init(); | ||
|
||
bool get hasInitialized; | ||
|
||
Future<bool> remove(String key); | ||
|
||
Future<Object?> get(String key); | ||
|
||
Future<bool> set(String key, String data); | ||
|
||
Future<bool> has(String key); | ||
|
||
Future<void> clear(); | ||
} |
48 changes: 48 additions & 0 deletions
48
lib/core/shared/data/data_source/local/shared_prefs_storage_service.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:flutter_template/core/shared/data/data_source/local/interface/storage_service.dart'; | ||
import 'package:shared_preferences/shared_preferences.dart'; | ||
|
||
class SharedPrefsStorageService implements StorageService { | ||
SharedPreferences? _sharedPreferences; | ||
|
||
final Completer<SharedPreferences> initCompleter = Completer<SharedPreferences>(); | ||
|
||
@override | ||
void init() { | ||
initCompleter.complete(SharedPreferences.getInstance()); | ||
} | ||
|
||
@override | ||
bool get hasInitialized => _sharedPreferences != null; | ||
|
||
@override | ||
Future<Object?> get(String key) async { | ||
_sharedPreferences = await initCompleter.future; | ||
return _sharedPreferences!.get(key); | ||
} | ||
|
||
@override | ||
Future<void> clear() async { | ||
_sharedPreferences = await initCompleter.future; | ||
await _sharedPreferences!.clear(); | ||
} | ||
|
||
@override | ||
Future<bool> has(String key) async { | ||
_sharedPreferences = await initCompleter.future; | ||
return _sharedPreferences?.containsKey(key) ?? false; | ||
} | ||
|
||
@override | ||
Future<bool> remove(String key) async { | ||
_sharedPreferences = await initCompleter.future; | ||
return await _sharedPreferences!.remove(key); | ||
} | ||
|
||
@override | ||
Future<bool> set(String key, data) async { | ||
_sharedPreferences = await initCompleter.future; | ||
return await _sharedPreferences!.setString(key, data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// ignore_for_file: public_member_api_docs, sort_constructors_first | ||
import 'dart:convert'; | ||
|
||
import 'package:equatable/equatable.dart'; | ||
|
||
class User extends Equatable { | ||
final String id; | ||
final String name; | ||
final String email; | ||
final String? imageUrl; | ||
|
||
const User({ | ||
required this.id, | ||
required this.name, | ||
required this.email, | ||
this.imageUrl, | ||
}); | ||
|
||
@override | ||
List<Object> get props => [id, name, email, imageUrl ?? '']; | ||
|
||
User copyWith({ | ||
String? id, | ||
String? name, | ||
String? email, | ||
String? imageUrl, | ||
}) { | ||
return User( | ||
id: id ?? this.id, | ||
name: name ?? this.name, | ||
email: email ?? this.email, | ||
imageUrl: imageUrl ?? this.imageUrl, | ||
); | ||
} | ||
|
||
Map<String, dynamic> toMap() { | ||
return <String, dynamic>{ | ||
'id': id, | ||
'name': name, | ||
'email': email, | ||
'image_url': imageUrl, | ||
}; | ||
} | ||
|
||
factory User.fromMap(Map<String, dynamic> map) { | ||
return User( | ||
id: map['id'] as String, | ||
name: map['name'] as String, | ||
email: map['email'] as String, | ||
imageUrl: map['image_url'] != null ? map['image_url'] as String : null, | ||
); | ||
} | ||
|
||
String toJson() => json.encode(toMap()); | ||
|
||
factory User.fromJson(String source) => User.fromMap(json.decode(source) as Map<String, dynamic>); | ||
|
||
@override | ||
bool get stringify => true; | ||
} |