Skip to content

Commit 0cc6865

Browse files
authored
Merge pull request #1 from bagusok/tes-sub3
Add Random restaurant recomendation notification at 11 o'clock
2 parents 7bd6ff7 + def49ac commit 0cc6865

20 files changed

+460
-44
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"java.configuration.updateBuildConfiguration": "interactive"
3+
}

android/app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ if (flutterVersionName == null) {
2424

2525
android {
2626
namespace "com.example.restaurant_app"
27-
compileSdkVersion 33
27+
compileSdkVersion flutter.compileSdkVersion
2828
ndkVersion flutter.ndkVersion
2929

3030
compileOptions {
18.3 KB
Loading

android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
buildscript {
2-
ext.kotlin_version = '1.7.10'
2+
ext.kotlin_version = '1.8.10'
33
repositories {
44
google()
55
mavenCentral()

lib/common/navigation.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import 'package:flutter/material.dart';
2+
3+
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
4+
5+
class Navigation {
6+
static intentWithData(String routeName, Object arguments) {
7+
navigatorKey.currentState?.pushNamed(routeName, arguments: arguments);
8+
}
9+
10+
static back() => navigatorKey.currentState?.pop();
11+
}

lib/main.dart

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,40 @@
1+
import 'dart:io';
2+
3+
import 'package:android_alarm_manager_plus/android_alarm_manager_plus.dart';
14
import 'package:flutter/material.dart';
5+
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
26
import 'package:provider/provider.dart';
37
import 'package:restaurant_app/common/colors.dart';
8+
import 'package:restaurant_app/common/navigation.dart';
49
import 'package:restaurant_app/data/api/api_service.dart';
510
import 'package:restaurant_app/providers/favorite_provider.dart';
611
import 'package:restaurant_app/providers/restaurant_detail_provider.dart';
712
import 'package:restaurant_app/providers/restaurant_provider.dart';
13+
import 'package:restaurant_app/providers/scheduling_provider.dart';
814
import 'package:restaurant_app/screens/favorite_restaurant.dart';
915
import 'package:restaurant_app/screens/home.dart';
1016
import 'package:restaurant_app/screens/restaurant_detail.dart';
17+
import 'package:restaurant_app/screens/settings.dart';
1118
import 'package:restaurant_app/screens/splash_screen.dart';
19+
import 'package:restaurant_app/utils/background_service.dart';
20+
import 'package:restaurant_app/utils/notification_helper.dart';
21+
22+
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
23+
FlutterLocalNotificationsPlugin();
24+
25+
void main() async {
26+
WidgetsFlutterBinding.ensureInitialized();
27+
// ignore: no_leading_underscores_for_local_identifiers
28+
final NotificationHelper _notificationHelper = NotificationHelper();
29+
// ignore: no_leading_underscores_for_local_identifiers
30+
final BackgroundService _service = BackgroundService();
31+
_service.initializeIsolate();
32+
if (Platform.isAndroid) {
33+
await AndroidAlarmManager.initialize();
34+
}
35+
36+
await _notificationHelper.initNotifications(flutterLocalNotificationsPlugin);
1237

13-
void main() {
1438
runApp(const MyApp());
1539
}
1640

@@ -20,6 +44,7 @@ class MyApp extends StatelessWidget {
2044
@override
2145
Widget build(BuildContext context) {
2246
return MaterialApp(
47+
navigatorKey: navigatorKey,
2348
title: 'Restaurant App',
2449
debugShowCheckedModeBanner: false,
2550
theme: ThemeData(
@@ -62,7 +87,11 @@ class MyApp extends StatelessWidget {
6287
child: const Home()),
6388
'/favorite': (context) => ChangeNotifierProvider(
6489
create: (_) => FavouriteProvider(),
65-
child: FavoriteRestaurant(),
90+
child: const FavoriteRestaurant(),
91+
),
92+
'/settings': (context) => ChangeNotifierProvider(
93+
create: (_) => SchedulingProvider(),
94+
child: const Settings(),
6695
),
6796
RestaurantDetail.routeName: (context) => MultiProvider(
6897
providers: [
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import 'package:android_alarm_manager_plus/android_alarm_manager_plus.dart';
2+
import 'package:flutter/material.dart';
3+
import 'package:restaurant_app/utils/background_service.dart';
4+
import 'package:restaurant_app/utils/date_formatter.dart';
5+
import 'package:shared_preferences/shared_preferences.dart';
6+
7+
class SchedulingProvider with ChangeNotifier {
8+
bool _isScheduled = false;
9+
10+
bool get isScheduled => _isScheduled;
11+
12+
SchedulingProvider() {
13+
_getPref();
14+
}
15+
16+
Future<bool> scheduledNews() async {
17+
_isScheduled = !_isScheduled;
18+
var prefs = await SharedPreferences.getInstance();
19+
20+
if (_isScheduled) {
21+
_isScheduled = true;
22+
await prefs.setBool('isScheduled', true);
23+
notifyListeners();
24+
return await AndroidAlarmManager.periodic(
25+
const Duration(hours: 24),
26+
1,
27+
BackgroundService.callback,
28+
startAt: DateTimeHelper.format(),
29+
exact: true,
30+
wakeup: true,
31+
);
32+
} else {
33+
_isScheduled = false;
34+
prefs.setBool('isScheduled', false);
35+
notifyListeners();
36+
return await AndroidAlarmManager.cancel(1);
37+
}
38+
}
39+
40+
void _getPref() async {
41+
var prefs = await SharedPreferences.getInstance();
42+
_isScheduled = prefs.getBool('isScheduled') ?? false;
43+
notifyListeners();
44+
}
45+
}

lib/screens/favorite_restaurant.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class _FavoriteRestaurantState extends State<FavoriteRestaurant> {
1616
Widget build(BuildContext context) {
1717
return Scaffold(
1818
appBar: AppBar(
19-
title: Text('Favorite Restaurant'),
19+
title: const Text('Favorite Restaurant'),
2020
),
2121
body: Padding(
2222
padding: const EdgeInsets.all(8.0),

lib/screens/home.dart

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import 'package:flutter/material.dart';
22
import 'package:provider/provider.dart';
3+
import 'package:restaurant_app/common/navigation.dart';
34
import 'package:restaurant_app/data/model/list_restaurant.dart';
45
import 'package:restaurant_app/providers/restaurant_provider.dart';
6+
import 'package:restaurant_app/screens/restaurant_detail.dart';
7+
import 'package:restaurant_app/utils/notification_helper.dart';
58
import 'package:restaurant_app/widget/restaurant_item.dart';
69

710
class Home extends StatefulWidget {
@@ -12,6 +15,8 @@ class Home extends StatefulWidget {
1215
}
1316

1417
class _HomeState extends State<Home> {
18+
final NotificationHelper _notificationHelper = NotificationHelper();
19+
1520
var restaurants = <RestaurantElement>[];
1621
bool isLoading = true;
1722

@@ -20,6 +25,8 @@ class _HomeState extends State<Home> {
2025
@override
2126
void initState() {
2227
super.initState();
28+
_notificationHelper
29+
.configureSelectNotificationSubject(RestaurantDetail.routeName);
2330
}
2431

2532
@override
@@ -46,9 +53,11 @@ class _HomeState extends State<Home> {
4653
shadowColor: Colors.transparent,
4754
actions: [
4855
IconButton(
49-
onPressed: () {},
50-
icon: Image.asset('assets/images/user-icon.png',
51-
width: 24, height: 24))
56+
onPressed: () => Navigation.intentWithData('/settings', ''),
57+
icon: const Icon(
58+
Icons.notifications,
59+
color: Colors.black,
60+
))
5261
],
5362
),
5463
body: SafeArea(
@@ -140,6 +149,7 @@ class _HomeState extends State<Home> {
140149

141150
@override
142151
void dispose() {
152+
selectNotificationSubject.close();
143153
super.dispose();
144154
}
145155
}

lib/screens/settings.dart

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
import 'package:flutter/foundation.dart';
1+
import 'dart:io';
2+
3+
import 'package:flutter/cupertino.dart';
24
import 'package:flutter/material.dart';
5+
import 'package:provider/provider.dart';
6+
import 'package:restaurant_app/common/navigation.dart';
7+
import 'package:restaurant_app/providers/scheduling_provider.dart';
38

49
class Settings extends StatefulWidget {
510
const Settings({super.key});
@@ -11,6 +16,74 @@ class Settings extends StatefulWidget {
1116
class _SettingsState extends State<Settings> {
1217
@override
1318
Widget build(BuildContext context) {
14-
return const Placeholder();
19+
return Scaffold(
20+
appBar: AppBar(
21+
title: const Text('Settings'),
22+
),
23+
body: ListView(
24+
children: [
25+
Material(
26+
child: ListTile(
27+
title: const Text('Schedule Restaurant of The Day'),
28+
trailing: Consumer<SchedulingProvider>(
29+
builder: (context, scheduled, _) {
30+
return Switch.adaptive(
31+
value: scheduled.isScheduled,
32+
onChanged: (value) async {
33+
if (Platform.isIOS) {
34+
customDialog(context);
35+
} else {
36+
scheduled.scheduledNews();
37+
}
38+
},
39+
);
40+
},
41+
),
42+
),
43+
),
44+
],
45+
),
46+
);
47+
}
48+
}
49+
50+
customDialog(BuildContext context) {
51+
if (Platform.isIOS) {
52+
showCupertinoDialog(
53+
context: context,
54+
barrierDismissible: true,
55+
builder: (context) {
56+
return CupertinoAlertDialog(
57+
title: const Text('Coming Soon!'),
58+
content: const Text('This feature will be coming soon!'),
59+
actions: [
60+
CupertinoDialogAction(
61+
child: const Text('Ok'),
62+
onPressed: () {
63+
Navigation.back();
64+
},
65+
),
66+
],
67+
);
68+
},
69+
);
70+
} else {
71+
showDialog(
72+
context: context,
73+
builder: (context) {
74+
return AlertDialog(
75+
title: const Text('Coming Soon!'),
76+
content: const Text('This feature will be coming soon!'),
77+
actions: [
78+
TextButton(
79+
onPressed: () {
80+
Navigation.back();
81+
},
82+
child: const Text('Ok'),
83+
),
84+
],
85+
);
86+
},
87+
);
1588
}
1689
}

0 commit comments

Comments
 (0)