Skip to content

Commit

Permalink
update home UI - kebab menu, total counts,
Browse files Browse the repository at this point in the history
  • Loading branch information
tentamdin committed Jan 20, 2025
1 parent 6e19603 commit 054919d
Show file tree
Hide file tree
Showing 7 changed files with 238 additions and 47 deletions.
9 changes: 9 additions & 0 deletions lib/repo/database_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ class DatabaseRepository<T> {
);
}

// get to total number of items in the database
Future<int> getCount() async {
final db = await dbHelper.database;
final count = Sqflite.firstIntValue(
await db.rawQuery('SELECT COUNT(*) FROM $tableName'),
);
return count ?? 0;
}

Future<List<T>> getSortedPaginatedOrganization(
int page, int pageSize, String category) async {
final db = await dbHelper.database;
Expand Down
10 changes: 10 additions & 0 deletions lib/states/deties_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class DeityListState {
final int page;
final int pageSize;
final String? error;
final int total;

DeityListState({
required this.deities,
Expand All @@ -19,6 +20,7 @@ class DeityListState {
required this.page,
required this.pageSize,
this.error,
required this.total,
});

factory DeityListState.initial() {
Expand All @@ -28,6 +30,7 @@ class DeityListState {
hasReachedMax: false,
page: 0,
pageSize: 20,
total: 0,
);
}

Expand All @@ -38,6 +41,7 @@ class DeityListState {
int? page,
int? pageSize,
String? error,
int? total,
}) {
return DeityListState(
deities: deities ?? this.deities,
Expand All @@ -46,6 +50,7 @@ class DeityListState {
page: page ?? this.page,
pageSize: pageSize ?? this.pageSize,
error: error ?? this.error,
total: total ?? this.total,
);
}
}
Expand Down Expand Up @@ -120,6 +125,11 @@ class DeityNotifier extends StateNotifier<DeityListState> {
}
}

// get the total number of deties in the database
Future<int> getDeitiesCount() async {
return await repository.getCount();
}

void clearSearchResults() {
state = DeityListState.initial();
}
Expand Down
6 changes: 6 additions & 0 deletions lib/states/festival_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ class FestivalNotifier extends StateNotifier<FestivalListState> {
}
}

// to get the total number of festivals
Future<int> getFestivalCount() async {
final totalFestivals = await repository.getCount();
return totalFestivals;
}

void clearSearchResults() {
state = FestivalListState.initial();
}
Expand Down
5 changes: 5 additions & 0 deletions lib/states/organization_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ class OrganizationNotifier extends StateNotifier<OrganizationListState> {
}
}

// get total number of organizations
Future<int> getOrganizationCount() async {
return await repository.getCount();
}

void clearSearchResults() {
state = OrganizationListState.initial();
}
Expand Down
159 changes: 122 additions & 37 deletions lib/ui/screen/home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,76 @@ import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:gompa_tour/states/bottom_nav_state.dart';
import 'package:gompa_tour/states/deties_state.dart';
import 'package:gompa_tour/states/festival_state.dart';
import 'package:gompa_tour/states/organization_state.dart';
import 'package:gompa_tour/states/recent_search.dart';
import 'package:gompa_tour/states/search_state.dart';
import 'package:gompa_tour/ui/screen/deities_list_screen.dart';
import 'package:gompa_tour/ui/screen/festival_list_screen.dart';
import 'package:gompa_tour/ui/screen/orginatzations_screen.dart';
import 'package:gompa_tour/ui/screen/qr_screen.dart';
import 'package:gompa_tour/util/enum.dart';
import 'package:gompa_tour/util/search_debouncer.dart';

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

@override
Widget build(BuildContext context, WidgetRef ref) {
ConsumerState<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends ConsumerState<HomeScreen> {
final TextEditingController _searchController = TextEditingController();
final FocusNode _searchFocusNode = FocusNode();
final _searchDebouncer = SearchDebouncer();

int totalDeity = 0;
int totalOrganization = 0;
int totalFestival = 0;

@override
void initState() {
super.initState();
_loadCounts();
}

void _loadCounts() async {
totalDeity =
await ref.read(detiesNotifierProvider.notifier).getDeitiesCount();
totalOrganization = await ref
.read(organizationNotifierProvider.notifier)
.getOrganizationCount();
totalFestival =
await ref.read(festivalNotifierProvider.notifier).getFestivalCount();
}

void _performSearch(String query) async {
if (query.isEmpty || query.length < 3) {
_clearSearchResults();
return;
}

_searchDebouncer.run(
query,
onSearch: (q) =>
ref.read(searchNotifierProvider.notifier).searchAcrossTables(q),
onSaveSearch: (q) =>
ref.read(recentSearchesProvider.notifier).addSearch(q),
onClearResults: _clearSearchResults,
);
}

@override
Widget build(BuildContext context) {
final searchState = ref.watch(searchNotifierProvider);

return Column(
children: [
_buildHeader(context),
_buildSearchBar(context),
const Divider(),
Expanded(
child: GridView.count(
crossAxisCount: 2,
padding: EdgeInsets.all(8),
mainAxisSpacing: 16,
crossAxisSpacing: 16,
children: [
_buildCard(
MenuType.deities,
'assets/images/buddha.png',
context,
),
_buildCard(
MenuType.organization,
'assets/images/potala2.png',
context,
),
_buildCard(
MenuType.pilgrimage,
'assets/images/duchen.png',
context,
),
_buildCard(
MenuType.festival,
'assets/images/duchen.png',
context,
),
],
),
),
_buildCategoryCards(context),
const SizedBox(height: 32),
],
);
Expand Down Expand Up @@ -84,20 +108,69 @@ class HomeScreen extends ConsumerWidget {
backgroundColor: WidgetStateProperty.resolveWith<Color>(
(states) => Colors.white,
),
controller: TextEditingController(),
controller: _searchController,
focusNode: _searchFocusNode,
leading: Icon(Icons.search),
trailing: [
if (_searchController.text.isNotEmpty)
IconButton(
icon: const Icon(Icons.clear),
onPressed: () {
_searchController.clear();
_clearSearchResults();
},
),
IconButton(
icon: Icon(Icons.qr_code),
onPressed: () {},
onPressed: () {
ref.read(bottomNavProvider.notifier).setAndPersistValue(2);
},
)
],
hintText: 'Search here....',
),
);
}

Widget _buildCard(MenuType type, String imagePath, BuildContext context) {
Widget _buildCategoryCards(BuildContext context) {
return Expanded(
child: GridView.count(
crossAxisCount: 2,
padding: EdgeInsets.all(8),
mainAxisSpacing: 16,
crossAxisSpacing: 16,
children: [
_buildCard(
MenuType.deities,
'assets/images/buddha.png',
context,
totalDeity,
),
_buildCard(
MenuType.organization,
'assets/images/potala2.png',
context,
totalOrganization,
),
_buildCard(
MenuType.pilgrimage,
'assets/images/duchen.png',
context,
totalFestival,
),
_buildCard(
MenuType.festival,
'assets/images/duchen.png',
context,
totalFestival,
),
],
),
);
}

Widget _buildCard(
MenuType type, String imagePath, BuildContext context, int count) {
return GestureDetector(
onTap: () {
switch (type) {
Expand Down Expand Up @@ -132,7 +205,7 @@ class HomeScreen extends ConsumerWidget {
fontWeight: FontWeight.bold,
),
),
Text("199"),
Text(count.toString()),
const SizedBox(height: 8),
],
),
Expand All @@ -152,4 +225,16 @@ class HomeScreen extends ConsumerWidget {
return "Pilgrimage";
}
}

@override
void dispose() {
_searchController.dispose();
_searchFocusNode.dispose();
_searchDebouncer.dispose();
super.dispose();
}

void _clearSearchResults() {
ref.read(searchNotifierProvider.notifier).clearSearchResults();
}
}
Loading

0 comments on commit 054919d

Please sign in to comment.