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

✨ added loading of organisers to the app #56

Merged
merged 2 commits into from
Aug 11, 2024
Merged
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
6 changes: 6 additions & 0 deletions lib/bootstrap.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:fluttercon/common/repository/hive_repository.dart';
import 'package:fluttercon/core/di/injectable.dart';
import 'package:fluttercon/features/auth/cubit/google_sign_in_cubit.dart';
import 'package:fluttercon/features/auth/cubit/social_auth_sign_in_cubit.dart';
import 'package:fluttercon/features/home/cubit/fetch_organisers_cubit.dart';
import 'package:fluttercon/firebase_options.dart';

class AppBlocObserver extends BlocObserver {
Expand Down Expand Up @@ -48,6 +49,11 @@ Future<void> bootstrap(FutureOr<Widget> Function() builder) async {
authRepository: getIt(),
),
),
BlocProvider(
create: (_) => FetchOrganisersCubit(
apiRepository: getIt(),
),
),
BlocProvider<SocialAuthSignInCubit>(
create: (_) => SocialAuthSignInCubit(
authRepository: getIt(),
Expand Down
1 change: 1 addition & 0 deletions lib/common/data/models/models.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export 'auth.dart';
export 'failure.dart';
export 'organisers.dart';
export 'room.dart';
export 'session.dart';
export 'speaker.dart';
26 changes: 26 additions & 0 deletions lib/common/data/models/organisers.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'package:freezed_annotation/freezed_annotation.dart';

part 'organisers.g.dart';
part 'organisers.freezed.dart';

@freezed
class Organiser with _$Organiser {
factory Organiser({
required int id,
required String logo,
required String name,
}) = _Organiser;

factory Organiser.fromJson(Map<String, Object?> json) =>
_$OrganiserFromJson(json);
}

@freezed
class OrganiserResponse with _$OrganiserResponse {
const factory OrganiserResponse({
required List<Organiser> data,
}) = _OrganiserResponse;

factory OrganiserResponse.fromJson(Map<String, Object?> json) =>
_$OrganiserResponseFromJson(json);
}
12 changes: 12 additions & 0 deletions lib/common/repository/api_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,16 @@ class ApiRepository {

return SessionResponse.fromJson(response).data;
}

Future<List<Organiser>> fetchOrganisers({
int perPage = 20,
int page = 1,
}) async {
final response = await _networkUtil.getReq(
'/organizers',
queryParameters: {'per_page': perPage, 'page': page},
);

return OrganiserResponse.fromJson(response).data;
}
}
28 changes: 28 additions & 0 deletions lib/features/home/cubit/fetch_organisers_cubit.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:fluttercon/common/data/models/models.dart';
import 'package:fluttercon/common/repository/api_repository.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

part 'fetch_organisers_cubit.freezed.dart';
part 'fetch_organisers_state.dart';

class FetchOrganisersCubit extends Cubit<FetchOrganisersState> {
FetchOrganisersCubit({required ApiRepository apiRepository})
: super(const FetchOrganisersState.initial()) {
_apiRepository = apiRepository;
}

late ApiRepository _apiRepository;

Future<void> fetchOrganisers() async {
emit(const FetchOrganisersState.loading());
try {
final organisers = await _apiRepository.fetchOrganisers();
emit(FetchOrganisersState.loaded(organisers: organisers));
} on Failure catch (e) {
emit(FetchOrganisersState.error(e.message));
} catch (e) {
emit(FetchOrganisersState.error(e.toString()));
}
}
}
11 changes: 11 additions & 0 deletions lib/features/home/cubit/fetch_organisers_state.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
part of 'fetch_organisers_cubit.dart';

@freezed
class FetchOrganisersState with _$FetchOrganisersState {
const factory FetchOrganisersState.initial() = _Initial;
const factory FetchOrganisersState.loading() = _Loading;
const factory FetchOrganisersState.loaded({
required List<Organiser> organisers,
}) = _Loaded;
const factory FetchOrganisersState.error(String message) = _Error;
}
41 changes: 39 additions & 2 deletions lib/features/home/widgets/organizers_card.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';

import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:fluttercon/common/data/models/models.dart';
import 'package:fluttercon/core/theme/theme_colors.dart';
import 'package:fluttercon/features/home/cubit/fetch_organisers_cubit.dart';

class OrganizersCard extends StatefulWidget {
const OrganizersCard({super.key});
Expand All @@ -10,10 +13,17 @@ class OrganizersCard extends StatefulWidget {
}

class _OrganizersCardState extends State<OrganizersCard> {
@override
void initState() {
context.read<FetchOrganisersCubit>().fetchOrganisers();
super.initState();
}

@override
Widget build(BuildContext context) {
final isDark = Theme.of(context).brightness == Brightness.light;
final size = MediaQuery.of(context).size;
final size =
MediaQuery.sizeOf(context); //Widget to only rebuild when size changes

return Container(
width: double.infinity,
Expand All @@ -36,6 +46,33 @@ class _OrganizersCardState extends State<OrganizersCard> {
),
const Spacer(),
//sponsors list
BlocBuilder<FetchOrganisersCubit, FetchOrganisersState>(
builder: (context, state) => state.maybeWhen(
loaded: (organisers) => Wrap(
spacing: 10,
children: [
for (final Organiser organiser in organisers)
CircleAvatar(
radius: 50,
backgroundImage: CachedNetworkImageProvider(
organiser.logo,
),
),
],
),
error: (message) => Text(
message,
style: Theme.of(context).textTheme.titleMedium?.copyWith(
fontWeight: FontWeight.bold,
color: isDark ? Colors.white : ThemeColors.blueColor,
fontSize: 18,
),
),
orElse: () => const Center(
child: CircularProgressIndicator(),
),
),
),
const Spacer(),
],
),
Expand Down
Loading