Skip to content

Commit

Permalink
Launch Screen with a cipher
Browse files Browse the repository at this point in the history
  • Loading branch information
12fahed committed Jul 21, 2024
1 parent 0d8f9ca commit 1259d7a
Show file tree
Hide file tree
Showing 6 changed files with 328 additions and 100 deletions.
Binary file added assets/images/devs-dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
176 changes: 176 additions & 0 deletions lib/new_ui/screens/launch_screen/launch_screen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'dart:async';
import 'package:bcrypt/bcrypt.dart';
import 'package:go_router/go_router.dart';
import 'package:tsec_app/new_ui/colors.dart';
// import 'package:tsec_app/new_ui/screens/login_screen/login_screen.dart';

class LaunchScreen extends StatefulWidget {
@override
_LaunchScreenState createState() => _LaunchScreenState();
}

class _LaunchScreenState extends State<LaunchScreen> {
Timer? _timer;
Duration _duration = Duration();
DateTime? _launchDate;
final TextEditingController _controller = TextEditingController();

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

Future<void> _fetchLaunchDate() async {
try {
DocumentSnapshot<Map<String, dynamic>> snapshot = await FirebaseFirestore.instance
.collection('Launch')
.doc('launch')
.get();
Timestamp? timestamp = snapshot.data()?['date'] as Timestamp?;
setState(() {
_launchDate = timestamp?.toDate();
if (_launchDate != null) {
_startTimer();
}
});
} catch (e) {
print('Error fetching launch date: $e');
}
}

void _startTimer() {
_timer = Timer.periodic(Duration(milliseconds: 1), (timer) {
if (_launchDate != null) {
setState(() {
_duration = _launchDate!.difference(DateTime.now());
if (_duration.inMilliseconds <= 0) {
_timer?.cancel();
_navigateToLogin();
}
});
}
});
}

void _navigateToLogin() {
print('Navigating to LoginScreen');
context.go('/login');
}

Future<void> _checkAnswer() async {
try {
DocumentSnapshot<Map<String, dynamic>> snapshot = await FirebaseFirestore.instance
.collection('Launch')
.doc('launch')
.get();
String hashedAnswer = snapshot.data()?['ans'] ?? '';

// Compare the hashed answer with the user input
if (BCrypt.checkpw(_controller.text.trim(), hashedAnswer)) {
_navigateToLogin();
} else {
// Optionally show an error message
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Incorrect code. Please try again.')),
);
}
} catch (e) {
print('Error checking answer: $e');
}
}

@override
void dispose() {
_timer?.cancel();
_controller.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
final int days = _duration.inDays;
final int hours = _duration.inHours % 24;
final int minutes = _duration.inMinutes % 60;
final int seconds = _duration.inSeconds % 60;
final int milliseconds = _duration.inMilliseconds % 1000;

return Scaffold(
backgroundColor: commonbgblack,
body: Center(
child: Padding(
padding: const EdgeInsets.all(5.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.asset(
'assets/images/devs-dark.png',
height: 250,
width: 250,
),
Text(
"App Launch in",
style: TextStyle(
color: Colors.white,
fontSize: 24,
fontWeight: FontWeight.bold,
shadows: [
Shadow(
offset: Offset(1.0, 1.0),
blurRadius: 4.0,
color: Colors.blue,
),
],
),
),
SizedBox(height: 10),
Text(
_launchDate == null
? 'Loading...'
: '$days:${hours.toString().padLeft(2, '0')}:${minutes.toString().padLeft(2, '0')}:${seconds.toString().padLeft(2, '0')}:${milliseconds.toString().padLeft(3, '0')}',
style: TextStyle(
color: Colors.white,
fontSize: 20,
),
),
SizedBox(height: 50),
Text(
r'$2a$10$2FYV.6fyPfD4mO7arFVI3eUC.98haCmPBgSmO21IV6.nYbByoW2Ii',
style: TextStyle(
color: Colors.grey[600],
fontSize: 8,
),
),
SizedBox(height: 10),
Container(
width: MediaQuery.of(context).size.width * 0.8,
child: TextField(
controller: _controller,
decoration: InputDecoration(
filled: true,
fillColor: commonbgLightblack,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0),
borderSide: BorderSide(
color: commonbgLightblack,
width: 1.5,
),
),
hintText: 'Code for pre access',
hintStyle: TextStyle(color: Colors.grey[700], fontSize: 15),
),
style: TextStyle(color: Colors.white),
onSubmitted: (value) {
_checkAnswer();
},
),
),
],
),
),
),
);
}
}
5 changes: 4 additions & 1 deletion lib/new_ui/screens/main_screen/main_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import 'package:tsec_app/new_ui/screens/notes_screen/notes_screen.dart';
import 'package:tsec_app/new_ui/screens/profile_screen/profile_screen.dart';
import 'package:tsec_app/new_ui/screens/railway_screen/railway_screen.dart';
import 'package:tsec_app/new_ui/screens/railway_screen/railwayform.dart';
import 'package:tsec_app/new_ui/screens/launch_screen/launch_screen.dart';
import 'package:tsec_app/new_ui/screens/timetable_screen/timetable_screen.dart';
import 'package:tsec_app/provider/appbar_title_provider.dart';
import 'package:tsec_app/provider/auth_provider.dart';
Expand Down Expand Up @@ -791,7 +792,7 @@ class _MainScreenState extends ConsumerState<MainScreen> {
width: size.width,
padding: EdgeInsets.all(8),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(5),
borderRadius: BorderRadius.circular(10),
color: Color(0xff383838),
),
child: InkWell(
Expand All @@ -803,6 +804,8 @@ class _MainScreenState extends ConsumerState<MainScreen> {
.headlineMedium!
.copyWith(
fontSize: 22,
fontFamily: 'Montserrat',
fontWeight: FontWeight.bold,
color: Theme
.of(context)
.colorScheme
Expand Down
49 changes: 40 additions & 9 deletions lib/new_ui/screens/splash_screen/splash_screen.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:tsec_app/models/user_model/user_model.dart';
import 'package:async/async.dart';
import '/../utils/image_assets.dart';
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:tsec_app/models/student_model/student_model.dart';
import 'package:tsec_app/new_ui/screens/main_screen/main_screen.dart';
import 'package:tsec_app/new_ui/screens/profile_screen/profile_screen.dart';
import 'package:tsec_app/new_ui/screens/launch_screen/launch_screen.dart';
import 'package:tsec_app/provider/app_state_provider.dart';
import 'package:tsec_app/provider/auth_provider.dart';
import '/../utils/image_assets.dart';
import 'package:tsec_app/provider/concession_provider.dart';
import 'package:tsec_app/provider/firebase_provider.dart';
import 'package:tsec_app/utils/notification_type.dart';
Expand All @@ -24,6 +26,8 @@ class SplashScreen extends ConsumerStatefulWidget {

class _SplashScreenState extends ConsumerState<SplashScreen> {
final AsyncMemoizer _memoizer = AsyncMemoizer();
DateTime? _launchDate;
bool _isLaunchDateFetched = false;

fetchUserDataOnce() {
return _memoizer.runOnce(() async {
Expand All @@ -32,6 +36,28 @@ class _SplashScreenState extends ConsumerState<SplashScreen> {
});
}

Future<void> _fetchLaunchDate() async {
try {
DocumentSnapshot<Map<String, dynamic>> snapshot = await FirebaseFirestore.instance
.collection('Launch')
.doc('launch')
.get();
Timestamp? timestamp = snapshot.data()?['date'] as Timestamp?;
setState(() {
_launchDate = timestamp?.toDate();
_isLaunchDateFetched = true;
});
} catch (e) {
print('Error fetching launch date: $e');
}
}

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

//check permissions
void requestpermission() async {
final status = await Permission.storage.status;
Expand All @@ -46,6 +72,18 @@ class _SplashScreenState extends ConsumerState<SplashScreen> {

@override
Widget build(BuildContext context) {
if (!_isLaunchDateFetched) {
return Scaffold(
body: Center(
child: CircularProgressIndicator(),
),
);
}

if (_launchDate != null && DateTime.now().isBefore(_launchDate!)) {
return LaunchScreen();
}

return FutureBuilder(
future: fetchUserDataOnce(),
builder: (context, snapshot) {
Expand All @@ -59,17 +97,10 @@ class _SplashScreenState extends ConsumerState<SplashScreen> {
} else {
return MainScreen();
}

// if (userModel != null) {
// return ProfilePage(justLoggedIn: true);
// } else {
// return MainScreen();
// }
} else {
return Scaffold(
body: Center(
child: SizedBox(
//SECOND SCREEN LOGO
child: Image.asset(ImageAssets.tsecapplogo),
height: 250,
width: 250,
Expand Down
Loading

0 comments on commit 1259d7a

Please sign in to comment.