FaucetHubv2/lib/welcome.dart
2023-08-16 14:26:57 +05:30

348 lines
11 KiB
Dart

import 'dart:async';
import 'package:fhub/backend/DataManager.dart';
import 'package:fhub/backend/Dialogs.dart';
import 'package:fhub/backend/login_mgr.dart';
import 'package:fhub/disabled_mode.dart';
import 'package:fhub/home.dart';
import 'package:fhub/off_screen.dart';
import 'package:fhub/src/CustomWidgets.dart';
import 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'backend/DebugHelper.dart';
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
var subscription;
class _MyHomePageState extends State<MyHomePage> {
int counter = -1;
bool loginLoading = false;
@override
void initState() {
// TODO: implement initState
super.initState();
kickstartAnimations();
init();
subscription = Connectivity().onConnectivityChanged.listen((ConnectivityResult result) async{
// Got a new connectivity status!
try{
final connectivityResult = await (Connectivity().checkConnectivity());
Debug.Log(connectivityResult);
if(connectivityResult != ConnectivityResult.mobile && connectivityResult != ConnectivityResult.mobile && connectivityResult != ConnectivityResult.ethernet){
Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (_)=>OfflinePage(id:0)));
return;
}
}catch(e){
}
});
}
@override
void dispose() {
// TODO: implement dispose
super.dispose();
subscription.cancel();
}
void init() async{
int serverVersion = await DataManager.GetVersion();
if(serverVersion > 0 && serverVersion != DataManager.ClientVersion){
Navigator.of(context).push(MaterialPageRoute(builder: (context)=> UpdatePage()));
return;
}else if(serverVersion <= 0){
Navigator.of(context).push(MaterialPageRoute(builder: (context)=> MaintaincePage()));
return;
}
if(await DataManager.Init() == false){
return;
}
if(await LoginManager.AutoLogin() == 0){
loadHome();
}else{
counter=0;
setState(() {
});
}
}
void kickstartAnimations() async {
await Future.delayed(const Duration(milliseconds: 500));
setState(() {
op1 = 1;
op2 = 1;
op3 = 1;
});
}
void loadHome(){
Navigator.of(context).pushAndRemoveUntil(MaterialPageRoute(builder: (context) =>
Home()), (Route<dynamic> route) => false);
}
@override
Widget build(BuildContext context) {
final screenHeight = MediaQuery
.of(context)
.size
.height;
final screenWidth = MediaQuery
.of(context)
.size
.width;
return Scaffold(
backgroundColor: Colors.black,
extendBody: true,
body: CustomBody(
onAnimEnd: () {
setState(() {});
},
context: context,
child: CurrentScreen(),
));
}
Widget CurrentScreen() {
switch (counter) {
case -1:
return Header(child: const Center(child:Text("Loading...")));
break;
case 1:
return LoginPage();
break;
default:
return LandingPage();
break;
}
}
Widget LandingPage() {
return Header(child: Container(
// color: Colors.black12,
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
const Padding(
padding: EdgeInsets.symmetric(horizontal: 80.0),
child: Text(
"Start Earning Crypto by playing games",
style: TextStyle(fontSize: 18),
textAlign: TextAlign.center,
),
),
const SizedBox(height: 100),
GlassButton(onTap: (){setState(() {
counter=1;
});}, child: Padding(
padding: const EdgeInsets.all(8.0),
child: Row(mainAxisAlignment:MainAxisAlignment.spaceBetween,children: [Container(width: 30,),Text("Get Started",style: TextStyle(fontSize: 20)), Icon(Icons.arrow_circle_right_outlined,size: 30,)],),
), width: 250, height : 50,color: Colors.lightBlueAccent),
// NeonButton(
// onPressed: () {
// setState(() {
// counter = 1;
// });
// },
// text: "Get Started",
// trailing: const Icon(
// Icons.arrow_circle_right_outlined,
// size: 30,
// )),
const SizedBox(height: 120),
],
),
),);
}
bool isLogin = true;
TextEditingController usernameController = TextEditingController();
TextEditingController passwordController = TextEditingController();
Widget LoginPage() {
return Header(child: loginLoading ? const Center(child: Text("Loading...")) : Column(mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: 30),
child: GlassCard(
child: Padding(
padding: const EdgeInsets.all(15.0),
child: Column(children:[
Text("Let's Get you ${isLogin ? "Logged-In" : "Registered"}!",
style: TextStyle(fontSize: 20)),
const SizedBox(height: 30,),
Row(mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.person), SizedBox(width: 15,),
SizedBox(width: 250,
child: TextField(controller: usernameController,
decoration: InputDecoration(hintText: "Username"),),)
],),
Row(mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.key),
SizedBox(width: 15,),
SizedBox(width: 250,
child: TextField(controller:passwordController,obscureText: true,
decoration: InputDecoration(hintText: "Password"),),)
],),
const SizedBox(height: 20,),
GlassButton(height: 40,
width: 200,
onTap: () async{
if(usernameController.text.length < 3){
Dialogs.showAlertDialog(context, "Invalid Input", "Username must be longer than 3 characters");
return;
}
if(passwordController.text.length < 3){
Dialogs.showAlertDialog(context, "Invalid Input", "Password must be longer than 3 characters");
return;
}
if(passwordController.text.contains(" ") || usernameController.text.contains(" ")){
Dialogs.showAlertDialog(context, "Invalid Input", "Neither the username nor password can contain spaces");
return;
}
setState(() {
loginLoading = true;
});
if(isLogin){
int loginResult = await LoginManager.Login(usernameController.text, passwordController.text);
if(loginResult == 0){
loadHome();
}else if(loginResult == 5){
Dialogs.showAlertDialog(context, "Login Failed", "Incorrect username or password");
}else{
Dialogs.showAlertDialog(context, "Login Failed", "Error code : $loginResult");
}
}else{
int regResult = await LoginManager.Register(usernameController.text, passwordController.text);
if(regResult == 0){
await LoginManager.AutoLogin();
await DataManager.GetGamesProgress();
loadHome();
}else if(regResult == 5){
Dialogs.showAlertDialog(context, "Register Failed", "Username Already exists, Please try again with a different username");
}else{
Dialogs.showAlertDialog(context, "Register Failed", "Error code : $regResult");
}
}
setState(() {
loginLoading = false;
});
},
child: isLogin ? Text("Login",style: TextStyle(fontSize: 18)) : Text("Register",style: TextStyle(fontSize: 18))),
const SizedBox(height: 10,),
InkWell(
child: Text(isLogin
? "Don't have an account? Register here"
: "Already have an Account? Login here",
style: TextStyle(color: Colors.blue)), onTap: () {
setState(() {
isLogin = !isLogin;
});
},),]),
),
),
),
const Padding(
padding: EdgeInsets.all(25.0),
child: Center(child: Text("OR")),
),
Row(
mainAxisAlignment: MainAxisAlignment.center, children: [
GlassButton(onTap: () {
// Dialogs.showAlertDialog(context, "Test", "This is a test message");
SignWithGoogle();
},
child: Row(mainAxisAlignment: MainAxisAlignment.spaceBetween,children: [Icon(Icons.g_mobiledata_rounded,size: 40), Text("Sign with Google",style:TextStyle(fontSize: 18)),Container()],),
height: 50,
width: 250,
color: Colors.greenAccent
)])
]));
}
Widget Header({required Widget child, MainAxisAlignment alignment = MainAxisAlignment.start}) {
return Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: alignment,
children: [
const SizedBox(height: 50,),
AnimatedSize(
duration: const Duration(seconds: 1),
curve: Curves.fastLinearToSlowEaseIn,
child: SizedBox(
height: (counter == 1) ? 180 : 250,
child: Center(
child: Image.asset(
'images/fhub_logo.png',
filterQuality: FilterQuality.medium,
),
),
),
),
Expanded(child: child)
],
);
}
Future<void> SignWithGoogle() async {
setState(() {
loginLoading = true;
});
GoogleSignIn _googleSignIn = GoogleSignIn(
scopes: [
'email',
'https://www.googleapis.com/auth/contacts.readonly',
],
);
try {
GoogleSignInAccount? account = await _googleSignIn.signIn();
String email = account!.email;
Debug.LogResponse(email);
int registerResult = await LoginManager.GoogleLogin(email);
if(registerResult == 0){
loadHome();
}else if(registerResult == 1){
Dialogs.showAlertDialog(context, "Failed", "Servers are unreachable. Please check internet connection and try again.");
}else if(registerResult == 5){
Dialogs.showAlertDialog(context, "Failed", "Couldn't Login with this google account");
}
} catch (error) {
print(error);
print("google sign in failed");
}
setState(() {
loginLoading = false;
});
}
}