-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(user): add user view model (#31)
* feat(user): add local and remote repository providers * feat(user): add user view model * feat(user): update the screens to use the view model * chore: add devtools options * refact: handle conflicts * feat(user): add username functionality
- Loading branch information
Showing
17 changed files
with
1,191 additions
and
297 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
description: This file stores settings for Dart & Flutter DevTools. | ||
documentation: https://docs.flutter.dev/tools/devtools/extensions#configure-extension-enablement-states | ||
extensions: | ||
- provider: true |
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 |
---|---|---|
|
@@ -2,7 +2,7 @@ import 'package:telware_cross_platform/core/models/user_model.dart'; | |
|
||
final UserModel userMock = UserModel( | ||
username: 'mock.user', | ||
screenName: 'Mocka Mocka', | ||
screenName: 'Mocka Mocker', | ||
email: '[email protected]', | ||
status: 'online', | ||
bio: 'I am a mocking user', | ||
|
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
110 changes: 110 additions & 0 deletions
110
lib/features/user/repository/user_local_repository.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,110 @@ | ||
import 'package:fpdart/fpdart.dart'; | ||
import 'package:riverpod_annotation/riverpod_annotation.dart'; | ||
import 'package:hive/hive.dart'; | ||
import 'package:telware_cross_platform/core/models/user_model.dart'; | ||
import 'package:telware_cross_platform/core/models/app_error.dart'; | ||
|
||
part 'user_local_repository.g.dart'; | ||
|
||
@Riverpod(keepAlive: true) | ||
UserLocalRepository userLocalRepository(UserLocalRepositoryRef ref) { | ||
return UserLocalRepository( | ||
userBox: Hive.box<UserModel>('auth-user'), // Same box as in auth_local_repository | ||
ref: ref, | ||
); | ||
} | ||
|
||
class UserLocalRepository { | ||
final Box<UserModel> _userBox; | ||
|
||
UserLocalRepository({ | ||
required Box<UserModel> userBox, | ||
required ProviderRef<UserLocalRepository> ref, | ||
}) : _userBox = userBox; | ||
|
||
Future<AppError?> changeNumber(String newPhoneNumber) async { | ||
try { | ||
final user = _userBox.get('user'); | ||
if (user != null) { | ||
final updatedUser = user.copyWith(phone: newPhoneNumber); | ||
await _userBox.put('user', updatedUser); | ||
} else { | ||
return AppError("User not found."); | ||
} | ||
} catch (error) { | ||
return AppError("Couldn't update phone number. Try again later."); | ||
} | ||
return null; | ||
} | ||
|
||
Future<AppError?> updateBio(String newBio) async { | ||
try { | ||
final user = _userBox.get('user'); | ||
if (user != null) { | ||
final updatedUser = user.copyWith(bio: newBio); | ||
await _userBox.put('user', updatedUser); | ||
} else { | ||
return AppError("User not found."); | ||
} | ||
} catch (error) { | ||
return AppError("Couldn't update bio. Try again later."); | ||
} | ||
return null; | ||
} | ||
|
||
Future<AppError?> updateScreenName(String newScreenName) async { | ||
try { | ||
final user = _userBox.get('user'); | ||
if (user != null) { | ||
final updatedUser = user.copyWith(screenName: newScreenName); | ||
await _userBox.put('user', updatedUser); | ||
} else { | ||
return AppError("User not found."); | ||
} | ||
} catch (error) { | ||
return AppError("Couldn't update screen name. Try again later."); | ||
} | ||
return null; | ||
} | ||
|
||
Future<AppError?> changeUsername(String newUsername) async { | ||
try { | ||
final user = _userBox.get('user'); | ||
if (user != null) { | ||
final updatedUser = user.copyWith(username: newUsername); | ||
await _userBox.put('user', updatedUser); | ||
} else { | ||
return AppError("User not found."); | ||
} | ||
} catch (error) { | ||
return AppError("Couldn't update username. Try again later."); | ||
} | ||
return null; | ||
} | ||
|
||
Either<AppError, bool> checkUsernameUniqueness(String username) { | ||
final user = _userBox.get('user'); | ||
if (user != null) { | ||
if (user.username == username) { | ||
return Left(AppError("Username is already taken.")); | ||
} | ||
return Right(true); | ||
} | ||
return Left(AppError("User not found.")); | ||
} | ||
|
||
// Retrieves the current user | ||
UserModel? getUser() { | ||
return _userBox.get('user'); | ||
} | ||
|
||
// Saves the updated user data | ||
Future<void> setUser(UserModel user) async { | ||
await _userBox.put('user', user); | ||
} | ||
|
||
// Deletes user data | ||
Future<void> deleteUser() async { | ||
await _userBox.delete('user'); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.