Skip to content

Commit

Permalink
Merge branch 'testbranch'
Browse files Browse the repository at this point in the history
  • Loading branch information
ShivanshuKGupta committed Aug 14, 2023
2 parents 356aafb + 2941d52 commit f401f93
Show file tree
Hide file tree
Showing 19 changed files with 341 additions and 243 deletions.
1 change: 1 addition & 0 deletions lib/firebase_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class DefaultFirebaseOptions {
projectId: 'hustle-stay',
authDomain: 'hustle-stay.firebaseapp.com',
storageBucket: 'hustle-stay.appspot.com',
measurementId: 'G-5SHV2ZPSH9',
);

static const FirebaseOptions android = FirebaseOptions(
Expand Down
84 changes: 57 additions & 27 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
Expand Down Expand Up @@ -31,38 +32,24 @@ SharedPreferences? prefs;
final auth = FirebaseAuth.instance;
final firestore = FirebaseFirestore.instance;
final storage = FirebaseStorage.instance;
final firebaseMessaging = FirebaseMessaging.instance;

/// Main function
void main() async {
// Just to show errors not so rudely
ErrorWidget.builder = (FlutterErrorDetails details) {
return Material(
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
':-( Something went wrong!',
style: TextStyle(fontSize: 20),
textAlign: TextAlign.center,
),
Text(
'\n${details.exception}',
style: const TextStyle(color: Colors.red),
textAlign: TextAlign.center,
),
const Text(
'Contact shivanshukgupta or sanidhayasharma141 on linkedin for support\n',
textAlign: TextAlign.center,
),
],
));
};
WidgetsFlutterBinding.ensureInitialized();
/// Just to show errors not so rudely
setErrorWidget();

// Initializing Firebase SDK
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);

/// Setting up firebase messaging
final fcmToken = await firebaseMessaging.getToken();
debugPrint("fcmToken = $fcmToken");
initializeFCM();

// Initializing prefs here in main to avoid any delay in activating settings
prefs = await SharedPreferences.getInstance();
try {
Expand All @@ -86,6 +73,45 @@ void main() async {
runApp(const ProviderScope(child: HustleStayApp()));
}

void initializeFCM() {
firebaseMessaging.onTokenRefresh.listen((fcmToken) {
debugPrint("FCM Token was refreshed. The new token is: $fcmToken");
// TODO: If necessary send token to application server.

// Note: This callback is fired at each app startup and whenever a new
// token is generated.
}).onError((err) {
debugPrint("Error: $err");
// Error getting token.
});
}

void setErrorWidget() {
ErrorWidget.builder = (FlutterErrorDetails details) {
return Material(
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
':-( Something went wrong!',
style: TextStyle(fontSize: 20),
textAlign: TextAlign.center,
),
Text(
'\n${details.exception}',
style: const TextStyle(color: Colors.red),
textAlign: TextAlign.center,
),
const Text(
'Contact shivanshukgupta or sanidhayasharma141 on linkedin for support\n',
textAlign: TextAlign.center,
),
],
));
};
}

/// The main app widget
class HustleStayApp extends ConsumerWidget {
const HustleStayApp({super.key});
Expand Down Expand Up @@ -182,7 +208,11 @@ Future<void> initializeEverything() async {
await initializeComplaints();
everythingInitialized.value = "Fetching requests";
await initializeRequests();
everythingInitialized.value = "Fetching vehicle requests";
await initializeVehicleRequests();

if (currentUser.type == 'warden' ||
currentUser.email == '[email protected]') {
everythingInitialized.value = "Fetching vehicle requests";
await initializeVehicleRequests();
}
everythingInitialized.value = null;
}
40 changes: 40 additions & 0 deletions lib/providers/notifications/notifications.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import 'dart:convert';

import 'package:http/http.dart' as http;

Future<void> sendNotification({
String? title,
String? body,
String to = '/topics/all',
}) async {
/// TODO: Dangerous!!! the fcm API key is here and should be moved to a backend
/// ERR:
const String serverKey =
'AAAAmUIGOT0:APA91bEVQn5IIBwUrIG8Brgf3vzZ-KxaGnDYY_8ElgZq65t909kx_EzFz6l613Kny_4Jh0JTcbm-EE3dvWGWM7dMISwseQ_wF0iYPDX9ti-nJKqrxKOXt3sKtXWh-VXSX_e3fsapadQO';
const String fcmUrl = 'https://fcm.googleapis.com/fcm/send';

final Map<String, dynamic> notification = {
'to': to,
'notification': {
'title': title,
'body': body,
},
};

final headers = {
'Content-Type': 'application/json',
'Authorization': 'key=$serverKey',
};

final response = await http.post(
Uri.parse(fcmUrl),
headers: headers,
body: jsonEncode(notification),
);

if (response.statusCode == 200) {
print('Notification sent successfully');
} else {
print('Failed to send notification: ${response.body}');
}
}
2 changes: 1 addition & 1 deletion lib/providers/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class _Settings {
bool autoDarkMode = true;

/// the last visited page
int currentPage = 3;
int currentPage = 2;

/// Introduction screen visisted
bool introductionScreenVisited = false;
Expand Down
17 changes: 13 additions & 4 deletions lib/screens/admin_panel/admin_panel_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:hustle_stay/screens/admin_panel/manage_categories.dart';
import 'package:hustle_stay/screens/admin_panel/manage_requests.dart';
import 'package:hustle_stay/screens/admin_panel/manage_user_permission.dart';
import 'package:hustle_stay/screens/vehicle/vehicle_screen.dart';

import '../../models/common/operation.dart';
import 'manage_hostel_attendance.dart';
Expand Down Expand Up @@ -30,7 +31,11 @@ class _AdminPanelState extends State<AdminPanel> {
Operations(
cardColor: const Color.fromARGB(255, 0, 146, 69),
operationName: 'Manage Hostels & Attendance',
icon: const Icon(Icons.calendar_month))
icon: const Icon(Icons.calendar_month)),
Operations(
cardColor: const Color.fromARGB(255, 0, 136, 146),
operationName: 'Vehicle Schedule',
icon: const Icon(Icons.airport_shuttle_rounded)),
];

@override
Expand All @@ -41,16 +46,16 @@ class _AdminPanelState extends State<AdminPanel> {

return Scaffold(
appBar: AppBar(
title: Text('Admin Panel'),
title: const Text('Admin Panel'),
),
body: SafeArea(
child: GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: 1,
),
itemBuilder: (context, index) {
final Color cardColor = catList[index].cardColor!;
final Color cardColor = catList[index].cardColor;

final LinearGradient gradient = LinearGradient(
begin: Alignment.topCenter,
Expand Down Expand Up @@ -84,6 +89,10 @@ class _AdminPanelState extends State<AdminPanel> {
Navigator.of(context).push(MaterialPageRoute(
builder: (_) => const ManageHostelPage()));
break;
case 'Vehicle Schedule':
Navigator.of(context).push(MaterialPageRoute(
builder: (_) => const VehicleScreen()));
break;
default:
}
},
Expand Down
Loading

0 comments on commit f401f93

Please sign in to comment.