Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-ishita-g committed Jul 5, 2024
1 parent e99a693 commit 9ef1908
Show file tree
Hide file tree
Showing 5 changed files with 558 additions and 1 deletion.
24 changes: 24 additions & 0 deletions app/lib/ui/flow/timeline/timeline_screen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:yourspace_flutter/ui/components/app_page.dart';

class TimelineScreen extends ConsumerStatefulWidget {
const TimelineScreen({super.key});

@override
ConsumerState createState() => _TimelineScreenState();
}

class _TimelineScreenState extends ConsumerState<TimelineScreen> {
@override
Widget build(BuildContext context) {
return AppPage(
title: 'Timeline',
body: _body(context),
);
}

Widget _body(BuildContext context) {
return Container();
}
}
102 changes: 102 additions & 0 deletions app/lib/ui/flow/timeline/timeline_view_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import 'package:data/api/auth/auth_models.dart';
import 'package:data/api/location/location.dart';
import 'package:data/log/logger.dart';
import 'package:data/service/auth_service.dart';
import 'package:data/service/timeline_service.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:data/service/space_service.dart';

part 'timeline_view_model.freezed.dart';

final timelineViewStateProvider = StateNotifierProvider.autoDispose<
TimelineViewNotifier, TimelineViewState>((ref) {
return TimelineViewNotifier(
ref.read(spaceServiceProvider),
ref.read(timelineServiceProvider),
ref.read(authServiceProvider),
);
});

class TimelineViewNotifier extends StateNotifier<TimelineViewState> {
final SpaceService spaceService;
final TimelineService timelineService;
final AuthService authService;

final String _userId = '';

TimelineViewNotifier(
this.spaceService,
this.timelineService,
this.authService,
) : super(const TimelineViewState()) {
fetchUser();
loadLocation();
}

void fetchUser() {
try {
final ApiUser? currentUser = authService.currentUser;
final ApiUser user = (currentUser != null && currentUser.id == _userId)
? currentUser
: authService.getUser(userId: _userId) as ApiUser;

state = state.copyWith(
selectedUser: user,
isCurrentUserTimeline: currentUser?.id == _userId,
);
} catch (error, stack) {
logger.e(
'TimelineViewNotifier: error while fetching user',
error: error,
stackTrace: stack,
);
}
}

void loadLocation({bool loadMore = false}) async {
try {
if (loadMore && !state.hasMoreLocations) return;
state = state.copyWith(loading: state.locations.isEmpty, appending: loadMore);
final from = state.selectedTimeForm;
final to = state.selectedTimeTo;
final lastJourneyTime = state.locations.isNotEmpty
? state.locations.map((loc) => loc.created_at).reduce((a, b) => a!.isBefore(b!) ? a : b)
: null;

List<ApiLocation> locations;
if (loadMore) {
locations = await timelineService.getMoreTimelineHistory(_userId, lastJourneyTime);
} else {
locations = await timelineService.getTimelineHistory(_userId, from: from, to: to);
}

final hasMoreItems = locations.isNotEmpty;
state = state.copyWith(
locations: [...state.locations, ...locations],
hasMoreLocations: hasMoreItems);

} catch (error, stack) {
logger.e(
'TimelineViewNotifier: error while load locations',
error: error,
stackTrace: stack,
);
}
}
}

@freezed
class TimelineViewState with _$TimelineViewState {
const factory TimelineViewState({
@Default(false) bool isCurrentUserTimeline,
ApiUser? selectedUser,
int? selectedTimeForm,
int? selectedTimeTo,
@Default(false) bool loading,
@Default(false) bool appending,
@Default(false) bool hasMoreLocations,
@Default([]) List<ApiLocation> locations,
Object? error,
}) = _TimelineViewState;
}
Loading

0 comments on commit 9ef1908

Please sign in to comment.