Compare commits

...

1 Commits

Author SHA1 Message Date
06212a0d72 fix auth for user with no activity. (#191) 2023-04-01 10:06:32 -07:00
5 changed files with 28 additions and 9 deletions

View File

@ -41,7 +41,12 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
await _authRepository.loggedIn.then((bool loggedIn) async {
if (loggedIn) {
final String? username = await _authRepository.username;
final User user = await _storiesRepository.fetchUser(id: username!);
User? user = await _storiesRepository.fetchUser(id: username!);
/// According to Hacker News' API documentation,
/// if user has no public activity (posting a comment or story),
/// then it will not be available from the API.
user ??= User.emptyWithId(username);
emit(
state.copyWith(
@ -84,10 +89,10 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
);
if (successful) {
final User user = await _storiesRepository.fetchUser(id: event.username);
final User? user = await _storiesRepository.fetchUser(id: event.username);
emit(
state.copyWith(
user: user,
user: user ?? User.emptyWithId(event.username),
isLoggedIn: true,
status: AuthStatus.loaded,
),

View File

@ -16,8 +16,13 @@ class UserCubit extends Cubit<UserState> {
void init({required String userId}) {
emit(state.copyWith(status: UserStatus.loading));
_storiesRepository.fetchUser(id: userId).then((User user) {
emit(state.copyWith(user: user, status: UserStatus.loaded));
_storiesRepository.fetchUser(id: userId).then((User? user) {
emit(
state.copyWith(
user: user ?? User.emptyWithId(userId),
status: UserStatus.loaded,
),
);
}).onError((_, __) {
emit(state.copyWith(status: UserStatus.failure));
return;

View File

@ -17,6 +17,12 @@ class User extends Equatable {
id = '',
karma = 0;
const User.emptyWithId(this.id)
: about = '',
created = 0,
delay = 0,
karma = 0;
User.fromJson(Map<String, dynamic> json)
: about = json['about'] as String? ?? '',
created = json['created'] as int? ?? 0,

View File

@ -74,11 +74,14 @@ class StoriesRepository {
/// Fetch a [User] by its [id].
/// Hacker News uses user's username as [id].
Future<User> fetchUser({required String id}) async {
final User user = await _firebaseClient
Future<User?> fetchUser({required String id}) async {
final User? user = await _firebaseClient
.get('${_baseUrl}user/$id.json')
.then((dynamic val) {
final Map<String, dynamic> json = val as Map<String, dynamic>;
final Map<String, dynamic>? json = val as Map<String, dynamic>?;
if (json == null) return null;
final User user = User.fromJson(json);
return user;
});

View File

@ -1,6 +1,6 @@
name: hacki
description: A Hacker News reader.
version: 1.4.0+104
version: 1.4.1+105
publish_to: none
environment: