From ecb9bffe70c195b4fcb3ce3883645a3f8e4708e2 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Thu, 8 Feb 2024 16:34:33 +0800 Subject: [PATCH] :sparkles: Directory --- lib/auth.dart | 2 +- lib/firebase.dart | 4 ++- lib/layouts/navigation.dart | 4 +-- lib/main.dart | 47 ++++++++++++++---------- lib/screens/application.dart | 23 ++++++++++++ lib/screens/auth.dart | 9 ++--- lib/screens/dashboard.dart | 70 ++++++++++++++++++++++++++++++++---- 7 files changed, 125 insertions(+), 34 deletions(-) create mode 100644 lib/screens/application.dart diff --git a/lib/auth.dart b/lib/auth.dart index 381484a..7cbeb89 100644 --- a/lib/auth.dart +++ b/lib/auth.dart @@ -75,7 +75,7 @@ class AuthGuard { // Use WebView to get authorization url var responseUrl = await Navigator.of(context).push(MaterialPageRoute( - builder: (context) => AuthorizationPage(authorizationUrl), + builder: (context) => AuthorizationScreen(authorizationUrl), )); var responseUri = Uri.parse(responseUrl); diff --git a/lib/firebase.dart b/lib/firebase.dart index 86c51de..5d21938 100644 --- a/lib/firebase.dart +++ b/lib/firebase.dart @@ -6,7 +6,7 @@ import 'package:firebase_core/firebase_core.dart'; import 'package:flutter/material.dart'; import 'firebase_options.dart'; -void initializeFirebase() async { +Future initializeFirebase() async { await Firebase.initializeApp( options: DefaultFirebaseOptions.currentPlatform, ); @@ -28,6 +28,8 @@ void initializeFirebase() async { FirebaseCrashlytics.instance.recordError(error, stack, fatal: true); return true; }; + + return true; } Future initializeFirebaseMessaging() async { diff --git a/lib/layouts/navigation.dart b/lib/layouts/navigation.dart index 967b770..ade6c32 100644 --- a/lib/layouts/navigation.dart +++ b/lib/layouts/navigation.dart @@ -33,7 +33,7 @@ class _AgentNavigationState extends State { Future initMessage(BuildContext context) async { void navigate() { - widget.router.push("/notifications"); + widget.router.replace("/notifications"); setState(() { _selected = 1; }); @@ -64,7 +64,7 @@ class _AgentNavigationState extends State { labelBehavior: NavigationDestinationLabelBehavior.onlyShowSelected, destinations: AgentNavigation.items, onDestinationSelected: (index) { - widget.router.push(AgentNavigation.destinations[index]); + widget.router.replace(AgentNavigation.destinations[index]); setState(() { _selected = index; }); diff --git a/lib/main.dart b/lib/main.dart index ecb1e94..2eb5e95 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,4 +1,3 @@ -import 'package:firebase_messaging/firebase_messaging.dart'; import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart'; import 'package:goatagent/auth.dart'; @@ -13,7 +12,6 @@ void main() async { WidgetsFlutterBinding.ensureInitialized(); AuthGuard().pickClient(); - initializeFirebase(); runApp(GoatAgent()); } @@ -41,23 +39,34 @@ class GoatAgent extends StatelessWidget { @override Widget build(BuildContext context) { - return MaterialApp.router( - routerConfig: _router, - title: 'GoatAgent', - theme: ThemeData( - colorScheme: ColorScheme.fromSeed(seedColor: Colors.indigo), - useMaterial3: true, - ), - builder: (BuildContext context, Widget? child) { - return Overlay(initialEntries: [ - OverlayEntry( - builder: (context) => Scaffold( - body: child, - // bottomNavigationBar: const AgentBottomNavigation() - bottomNavigationBar: AgentNavigation(router: _router), - ), - ) - ]); + return FutureBuilder( + future: initializeFirebase(), + builder: (context, snapshot) { + if (!snapshot.hasData) { + return const Center( + child: CircularProgressIndicator(), + ); + } + + return MaterialApp.router( + routerConfig: _router, + title: 'GoatAgent', + theme: ThemeData( + colorScheme: ColorScheme.fromSeed(seedColor: Colors.indigo), + useMaterial3: true, + ), + builder: (BuildContext context, Widget? child) { + return Overlay(initialEntries: [ + OverlayEntry( + builder: (context) => Scaffold( + body: child, + // bottomNavigationBar: const AgentBottomNavigation() + bottomNavigationBar: AgentNavigation(router: _router), + ), + ) + ]); + }, + ); }, ); } diff --git a/lib/screens/application.dart b/lib/screens/application.dart new file mode 100644 index 0000000..5bbd08c --- /dev/null +++ b/lib/screens/application.dart @@ -0,0 +1,23 @@ +import 'package:flutter/material.dart'; +import 'package:webview_flutter/webview_flutter.dart'; + +class AuthorizationScreen extends StatelessWidget { + final Uri link; + + const AuthorizationScreen(this.link, {super.key}); + + @override + Widget build(BuildContext context) { + return Scaffold( + body: SafeArea( + child: WebViewWidget( + controller: WebViewController() + ..setJavaScriptMode(JavaScriptMode.unrestricted) + ..setBackgroundColor(Colors.white) + ..loadRequest(link) + ..clearCache(), + ), + ), + ); + } +} diff --git a/lib/screens/auth.dart b/lib/screens/auth.dart index c1ed315..3c1980d 100644 --- a/lib/screens/auth.dart +++ b/lib/screens/auth.dart @@ -1,10 +1,10 @@ import 'package:flutter/material.dart'; import 'package:webview_flutter/webview_flutter.dart'; -class AuthorizationPage extends StatelessWidget { +class AuthorizationScreen extends StatelessWidget { final Uri authorizationUrl; - const AuthorizationPage(this.authorizationUrl, {super.key}); + const AuthorizationScreen(this.authorizationUrl, {super.key}); @override Widget build(BuildContext context) { @@ -15,7 +15,7 @@ class AuthorizationPage extends StatelessWidget { body: WebViewWidget( controller: WebViewController() ..setJavaScriptMode(JavaScriptMode.unrestricted) - ..setBackgroundColor(Colors.indigo) + ..setBackgroundColor(Colors.white) ..setNavigationDelegate(NavigationDelegate( onNavigationRequest: (NavigationRequest request) { if (request.url.startsWith('goatagent://auth')) { @@ -25,7 +25,8 @@ class AuthorizationPage extends StatelessWidget { return NavigationDecision.navigate; }, )) - ..loadRequest(authorizationUrl), + ..loadRequest(authorizationUrl) + ..clearCache(), ), ); } diff --git a/lib/screens/dashboard.dart b/lib/screens/dashboard.dart index 807d522..11f859c 100644 --- a/lib/screens/dashboard.dart +++ b/lib/screens/dashboard.dart @@ -1,15 +1,71 @@ -import 'package:flutter/material.dart'; +import 'dart:convert'; -class DashboardScreen extends StatelessWidget { +import 'package:flutter/material.dart'; +import 'package:http/http.dart' as http; + +import 'application.dart'; + +class DashboardScreen extends StatefulWidget { const DashboardScreen({super.key}); + @override + State createState() => _DashboardScreenState(); +} + +class _DashboardScreenState extends State { + final client = http.Client(); + final directoryEndpoint = + Uri.parse('https://id.smartsheep.studio/.well-known'); + + List directory = List.empty(); + + @override + void initState() { + super.initState(); + _pullDirectory(); + } + + Future _pullDirectory() async { + var response = await client.get(directoryEndpoint); + if (response.statusCode == 200) { + setState(() { + directory = jsonDecode(utf8.decode(response.bodyBytes))["directory"]; + }); + } + } + @override Widget build(BuildContext context) { - return const Scaffold( - body: Center( - child: Text("你好"), + return Scaffold( + body: SafeArea( + child: Padding( + padding: const EdgeInsets.only(left: 20, right: 20, top: 30), + child: GridView.count( + crossAxisCount: 2, + children: directory.map((element) { + return Card( + child: InkWell( + onTap: () async { + await Navigator.of(context).push(MaterialPageRoute( + builder: (context) => AuthorizationScreen( + Uri.parse(element["link"]), + ), + )); + }, + splashColor: Colors.indigo.withAlpha(30), + child: Padding( + padding: const EdgeInsets.all(10), + child: ListTile( + title: Text(element['name']), + subtitle: Text(element['description']), + ), + ), + ), + ); + }).toList(), + ), + ), ), ); } - -} \ No newline at end of file +}