Skip to content

Commit

Permalink
Feat: Favorite function (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
namelessto authored Feb 10, 2024
2 parents 063ec50 + ad584e1 commit 2048140
Show file tree
Hide file tree
Showing 39 changed files with 1,560 additions and 428 deletions.
2 changes: 1 addition & 1 deletion assets
1 change: 1 addition & 0 deletions devtools_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
extensions:
2 changes: 2 additions & 0 deletions lib/analytics/analytics_constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const String kHeroPagesClass = 'hero_pages';
const String kBloonPagesClass = 'bloon_pages';
const String kBossPagesClass = 'boss_pages';
const String kMapPagesClass = 'map_pages';
const String kFavoritesClass = 'favorites_page';

// ------- Const for event names -------
const String widgetEngagement = 'widget_engagement';
Expand Down Expand Up @@ -33,5 +34,6 @@ const String kHeroSkinsPage = 'hero_skins_page';
// ---- General ----
const String expansionTile = 'expansion_tile';
const String listTile = 'list_tile';
const String card = 'card';
const String buttonOpen = 'opened';
const String buttonClose = 'closed';
20 changes: 20 additions & 0 deletions lib/hive/favorite_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'package:hive_flutter/hive_flutter.dart';

part 'favorite_model.g.dart';

@HiveType(typeId: 1)
class FavoriteModel extends HiveObject {
@HiveField(0)
late final String id;

@HiveField(1)
late final String name;

@HiveField(2)
late final String image;

@HiveField(3)
late final String type;

FavoriteModel(this.id, this.name, this.image, this.type);
}
50 changes: 50 additions & 0 deletions lib/hive/favorite_model.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions lib/hive/favorite_model_adapter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import 'package:hive_flutter/hive_flutter.dart';
import '/hive/favorite_model.dart';

class FavoriteModelBoxAdapter extends TypeAdapter<FavoriteModel> {
@override
final typeId = 1;

@override
FavoriteModel read(BinaryReader reader) {
return FavoriteModel(
// id
reader.readString(),
// name
reader.readString(),
// image
reader.readString(),
// type
reader.readString(),
);
}

@override
void write(BinaryWriter writer, FavoriteModel obj) {
writer.writeString(obj.id);
writer.writeString(obj.name);
writer.writeString(obj.image);
writer.writeString(obj.type);
}
}
132 changes: 92 additions & 40 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import 'package:firebase_analytics/firebase_analytics.dart';
import 'package:adaptive_theme/adaptive_theme.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:provider/provider.dart';
import 'package:flutter/material.dart';
import '/firebase_options.dart';
import '/hive/favorite_model.dart';
import '/models/base/base_tower.dart';
import '/models/base/base_hero.dart';
import '/models/base/base_map.dart';
import '/models/base_model.dart';
import '/presentation/widgets/drawer_content.dart';
import '/presentation/screens/misc/favorite_screen.dart';
import '/presentation/widgets/misc/drawer_content.dart';
import '/presentation/screens/tower/towers.dart';
import '/presentation/screens/bloon/bloons.dart';
import '/presentation/screens/hero/heroes.dart';
import '/presentation/screens/maps/maps.dart';
import '/presentation/widgets/loader.dart';
import 'analytics/analytics_constants.dart';
import 'presentation/widgets/common/loader.dart';
import '/analytics/analytics_constants.dart';
import '/analytics/analytics.dart';
import '/utilities/favorite_state.dart';
import '/utilities/global_state.dart';
import '/utilities/constants.dart';
import '/utilities/requests.dart';
Expand All @@ -29,29 +33,43 @@ Future<void> main() async {
options: DefaultFirebaseOptions.currentPlatform,
);
final analytics = FirebaseAnalytics.instance;

await Hive.initFlutter();
Hive.registerAdapter(FavoriteModelAdapter());
await Hive.openBox<List<dynamic>>(
kFavorite,
keyComparator: desiredCategoryOrder,
);
runApp(MyApp(analytics: analytics));
}

class MyApp extends StatelessWidget {
const MyApp({super.key, required this.analytics});
final FirebaseAnalytics analytics;

@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (BuildContext context) => GlobalState(),
return MultiProvider(
providers: [
ChangeNotifierProvider<GlobalState>(
create: (BuildContext context) => GlobalState(),
),
ChangeNotifierProvider<FavoriteState>(
create: (BuildContext context) => FavoriteState(),
),
],
child: AdaptiveTheme(
light: Themes.lightTheme,
dark: Themes.darkTheme,
initial: AdaptiveThemeMode.system,
builder: (theme, darkTheme) => MaterialApp(
theme: theme,
darkTheme: darkTheme,
home: MyHomePage(
analytics: analytics,
),
debugShowCheckedModeBanner: false,
)),
light: Themes.lightTheme,
dark: Themes.darkTheme,
initial: AdaptiveThemeMode.system,
builder: (theme, darkTheme) => MaterialApp(
theme: theme,
darkTheme: darkTheme,
home: MyHomePage(
analytics: analytics,
),
debugShowCheckedModeBanner: false,
),
),
);
}
}
Expand Down Expand Up @@ -152,30 +170,64 @@ class _MyHomePageState extends State<MyHomePage> {
: Container();
},
),
Consumer<GlobalState>(builder: (context, globalState, child) {
return IconButton(
onPressed: () {
globalState.switchSearch();
String value;
if (globalState.isSearchEnabled) {
value = searchOn;
} else {
globalState.updateCurrentQuery('');
value = searchOff;
}
analyticsHelper.logEvent(
name: widgetEngagement,
parameters: {
'screen': globalState.activeCategory,
'widget': searchButton,
'value': value,
Consumer<GlobalState>(
builder: (context, globalState, child) {
return IconButton(
onPressed: () {
globalState.switchSearch();
String value;
if (globalState.isSearchEnabled) {
value = searchOn;
} else {
globalState.updateCurrentQuery('');
value = searchOff;
}
analyticsHelper.logEvent(
name: widgetEngagement,
parameters: {
'screen': globalState.activeCategory,
'widget': searchButton,
'value': value,
},
);
},
icon: Icon(
!globalState.isSearchEnabled ? Icons.search : Icons.close),
);
},
),
Consumer<FavoriteState>(
builder: (context, favoriteState, child) {
IconData favIcon =
favoriteState.multiSelect ? Icons.add_task_sharp : Icons.star;
return GestureDetector(
onLongPress: () {
favoriteState.toggleMultiSelect();
},
child: IconButton(
onPressed: () {
if (!favoriteState.multiSelect) {
analyticsHelper.logScreenView(
screenClass: kFavoritesClass,
screenName: kFavoritesClass,
);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => FavoriteScreen(
analyticsHelper: analyticsHelper,
),
),
);
} else {
favoriteState.toggleMultiSelect();
}
},
);
},
icon: Icon(
!globalState.isSearchEnabled ? Icons.search : Icons.close),
);
})
icon: Icon(favIcon),
),
);
},
),
],
),
body: !isLoading
Expand Down
Loading

0 comments on commit 2048140

Please sign in to comment.