Skip to content

Commit

Permalink
terms
Browse files Browse the repository at this point in the history
  • Loading branch information
mo7amedaliEbaid committed Jul 27, 2024
1 parent aae98ca commit a60b19d
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/application/application.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export 'blocs/sign_up/sign_up_bloc.dart';
export 'blocs/restaurants/restaurants_bloc.dart';
export 'blocs/tags/tags_bloc.dart';
export 'blocs/about_us/about_us_bloc.dart';
export 'blocs/terms/terms_bloc.dart';

//cubits
export 'cubits/connectivity/connectivity_cubit.dart';
Expand Down
25 changes: 25 additions & 0 deletions lib/application/blocs/terms/terms_bloc.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:garcon/models/models.dart';
import 'package:garcon/repositories/terms/base_terms_repo.dart';
import 'package:meta/meta.dart';

part 'terms_event.dart';

part 'terms_state.dart';

class TermsBloc extends Bloc<TermsEvent, TermsState> {
final BaseTermsRepository _termsRepository;

TermsBloc(this._termsRepository) : super(TermsInitial()) {
on<GetTermsEvent>((event, emit) async {
emit(TermsLoading());
try {
final terms = await _termsRepository.getTerms().first;
emit(TermsLoaded(terms));
} catch (e) {
emit(TermsError('Failed to fetch terms info: $e'));
}
});
}
}
11 changes: 11 additions & 0 deletions lib/application/blocs/terms/terms_event.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
part of 'terms_bloc.dart';

@sealed
abstract class TermsEvent extends Equatable {
const TermsEvent();

@override
List<Object> get props => [];
}

class GetTermsEvent extends TermsEvent {}
30 changes: 30 additions & 0 deletions lib/application/blocs/terms/terms_state.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
part of 'terms_bloc.dart';

abstract class TermsState extends Equatable {
const TermsState();

@override
List<Object> get props => [];
}

class TermsInitial extends TermsState {}

class TermsLoading extends TermsState {}

class TermsLoaded extends TermsState {
final Terms terms;

const TermsLoaded(this.terms);

@override
List<Object> get props => [terms];
}

class TermsError extends TermsState {
final String error;

const TermsError(this.error);

@override
List<Object> get props => [error];
}
3 changes: 2 additions & 1 deletion lib/models/models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export 'tag.dart';
export 'pickup.dart';
export 'reservation.dart';
export 'about_us.dart';
export 'order.dart';
export 'order.dart';
export 'terms.dart';
34 changes: 34 additions & 0 deletions lib/models/terms.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:equatable/equatable.dart';

class Terms extends Equatable {
final String header1;
final String subheader1;
final String header2;
final String subheader2;

const Terms({
required this.header1,
required this.subheader1,
required this.header2,
required this.subheader2,
});

static Terms fromSnapShot(DocumentSnapshot snap) {
Terms terms = Terms(
header1: snap["header1"],
subheader1: snap["subheader1"],
header2: snap["header2"],
subheader2: snap["subheader2"],
);
return terms;
}

@override
List<Object?> get props => [
header1,
subheader1,
header2,
subheader2,
];
}
3 changes: 2 additions & 1 deletion lib/repositories/repositories.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export 'pickups/pickups_repo.dart';
export 'reservation/reservation_repo.dart';
export 'about/about_repo.dart';
export 'cart/cart_repo.dart';
export 'orders/orders_repo.dart';
export 'orders/orders_repo.dart';
export 'terms/terms_repo.dart';
5 changes: 5 additions & 0 deletions lib/repositories/terms/base_terms_repo.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import '../../models/models.dart';

abstract class BaseTermsRepository{
Stream<Terms> getTerms();
}
19 changes: 19 additions & 0 deletions lib/repositories/terms/terms_repo.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:garcon/models/terms.dart';
import 'package:garcon/repositories/terms/base_terms_repo.dart';

class TermsRepository extends BaseTermsRepository {
final FirebaseFirestore _firebaseFirestore;

TermsRepository({FirebaseFirestore? firebaseFirestore})
: _firebaseFirestore = firebaseFirestore ?? FirebaseFirestore.instance;

@override
Stream<Terms> getTerms() {
return _firebaseFirestore
.collection("info")
.doc("terms")
.snapshots()
.map((snap) => Terms.fromSnapShot(snap));
}
}

0 comments on commit a60b19d

Please sign in to comment.