Compare commits

...

5 Commits

7 changed files with 268 additions and 148 deletions

@ -1,129 +1,144 @@
/*
TODO: add license header here, when the code is replaced with actual code, and not flutter's default code
*/
import 'dart:async';
import 'package:firka/helpers/db/models/token_model.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:isar/isar.dart';
import 'package:path_provider/path_provider.dart';
import 'screens/login/login_screen.dart';
import 'screens/debug/debug_screen.dart';
import 'screens/home/home_screen.dart';
import 'pages/error/error_page.dart';
void main() {
runApp(const MyApp());
late Isar isar;
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
class AppInitialization {
final Isar isarInstance;
final int tokenCount;
AppInitialization({
required this.isarInstance,
required this.tokenCount,
});
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
Future<Isar> initDB() async {
final dir = await getApplicationDocumentsDirectory();
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// TRY THIS: Try running your application with "flutter run". You'll see
// the application has a purple toolbar. Then, without quitting the app,
// try changing the seedColor in the colorScheme below to Colors.green
// and then invoke "hot reload" (save your changes or press the "hot
// reload" button in a Flutter-supported IDE, or press "r" if you used
// the command line to start the app).
//
// Notice that the counter didn't reset back to zero; the application
// state is not lost during the reload. To reset the state, use hot
// restart instead.
//
// This works for code too, not just values: Most code changes can be
// tested with just a hot reload.
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
return Isar.open(
inspector: true,
schemas: [TokenModelSchema],
directory: dir.path,
);
}
Future<AppInitialization> initializeApp() async {
final isarInstance = await initDB();
final tokenCount = isarInstance.tokenModels.count();
if (kDebugMode) {
print('Token count: $tokenCount');
}
return AppInitialization(
isarInstance: isarInstance,
tokenCount: tokenCount,
);
}
void main() async {
//TODO: fix the error handling currently not pushing to the error page
runZonedGuarded(() async {
WidgetsFlutterBinding.ensureInitialized();
// Run App Initialization
runApp(InitializationScreen());
if (kDebugMode) {
var isar = await initDB();
var count = isar.tokenModels.count();
print(count);
}
}, (error, stackTrace) {
debugPrint('Caught error: $error');
debugPrint('Stack trace: $stackTrace');
navigatorKey.currentState?.push(
MaterialPageRoute(
builder: (context) => ErrorPage(exception: error.toString()),
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
});
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
class InitializationScreen extends StatelessWidget {
InitializationScreen({super.key});
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
}
// Place to store the initialization future
final Future<AppInitialization> _initialization = initializeApp();
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// TRY THIS: Try changing the color here to a specific color (to
// Colors.amber, perhaps?) and trigger a hot reload to see the AppBar
// change color while the other colors stay the same.
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also a layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
//
// TRY THIS: Invoke "debug painting" (choose the "Toggle Debug Paint"
// action in the IDE, or press "p" in the console), to see the
// wireframe for each widget.
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
return FutureBuilder<AppInitialization>(
future: _initialization,
builder: (context, snapshot) {
// Check if initialization is complete
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasError) {
// Handle initialization error
return Scaffold(
body: Center(
child: Text(
'Error initializing app: ${snapshot.error}',
style: TextStyle(color: Colors.red),
),
),
);
}
// Initialization successful, determine which screen to show
StatelessWidget screen;
if (kDebugMode) {
print('Debug mode: using DebugScreen');
screen = DebugScreen();
} else {
if (snapshot.data!.tokenCount == 0) {
screen = LoginScreen();
} else {
screen = HomeScreen();
}
}
return MaterialApp(
title: 'Firka',
navigatorKey: navigatorKey, // Use the global navigator key
theme: ThemeData(
primarySwatch: Colors.lightGreen,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
home: screen,
routes: {
'/login': (context) => const LoginScreen(),
'/debug': (context) => const DebugScreen(),
},
);
}
return MaterialApp(
home: Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
color: const Color(0xFF7CA021),
)
],
),
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
),
);
},
);
}
}

@ -0,0 +1,58 @@
import 'package:flutter/material.dart';
class ErrorPage extends StatelessWidget {
final String exception;
const ErrorPage({super.key, required this.exception});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Error Occurred'),
backgroundColor: Colors.red,
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.error,
size: 80,
color: Colors.red,
),
const SizedBox(height: 20),
Text(
'An error occurred!',
style: Theme.of(context).textTheme.headlineMedium?.copyWith(
color: Colors.red,
),
),
const SizedBox(height: 10),
Text(
'Details:',
style: Theme.of(context).textTheme.titleLarge?.copyWith(
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 10),
Text(
exception,
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
color: Colors.redAccent,
),
),
const SizedBox(height: 30),
ElevatedButton(
onPressed: () {
Navigator.pop(context);
},
child: const Text('Go Back'),
),
],
),
),
);
}
}

@ -0,0 +1,36 @@
import 'package:flutter/material.dart';
class DebugScreen extends StatelessWidget {
const DebugScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Debug'),
centerTitle: true,
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
'Debug Screen',
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 20),
ElevatedButton(
onPressed: () {
throw 0 / 0;
},
child: const Text('Throw Exception'),
),
],
),
),
);
}
}

@ -0,0 +1,24 @@
import 'package:flutter/material.dart';
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Home'),
centerTitle: true,
),
body: const Center(
child: Text(
'Home Screen',
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
),
);
}
}

@ -0,0 +1,24 @@
import 'package:flutter/material.dart';
class LoginScreen extends StatelessWidget {
const LoginScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Login'),
centerTitle: true,
),
body: const Center(
child: Text(
'Login Screen',
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
),
);
}
}

@ -38,7 +38,6 @@ dependencies:
cupertino_icons: ^1.0.8
flutter_launcher_icons: ^0.14.3
flutter_native_splash: ^2.4.5
dio: ^5.8.0+1
isar: *isar_version
isar_flutter_libs: *isar_version
@ -50,12 +49,6 @@ dev_dependencies:
sdk: flutter
flutter_lints: ^5.0.0
flutter_native_splash:
color: "#7CA021"
image: assets/logos/splash.png
android: true
ios: true
# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec

@ -1,30 +0,0 @@
// This is a basic Flutter widget test.
//
// To perform an interaction with a widget in your test, use the WidgetTester
// utility in the flutter_test package. For example, you can send tap and scroll
// gestures. You can also use WidgetTester to find child widgets in the widget
// tree, read text, and verify that the values of widget properties are correct.
import 'package:firka/main.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(const MyApp());
// Verify that our counter starts at 0.
expect(find.text('0'), findsOneWidget);
expect(find.text('1'), findsNothing);
// Tap the '+' icon and trigger a frame.
await tester.tap(find.byIcon(Icons.add));
await tester.pump();
// Verify that our counter has incremented.
expect(find.text('0'), findsNothing);
expect(find.text('1'), findsOneWidget);
});
}