This commit is contained in:
JMARyA 2024-09-08 17:45:48 +02:00
parent bb4fab6a11
commit 84cf6a0dda
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
7 changed files with 226 additions and 5 deletions

View file

@ -1,13 +1,39 @@
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:shared_preferences/shared_preferences.dart';
class API {
late SharedPreferences pref;
static final API _instance = API._internal();
factory API() {
return _instance;
}
API._internal();
Future<void> init() async {
pref = await SharedPreferences.getInstance();
instance = pref.getString("instance") ?? "";
}
bool isInit() {
return pref.containsKey("token") && pref.containsKey("instance");
}
void save(String instance, String token) {
pref.setString("instance", instance);
pref.setString("token", token);
this.instance = instance;
}
// todo : rework with auth
static String instance = "http://192.168.2.20:8080";
String instance = "";
Future<String> getRequest(String url) async {
var resp = await http.get(Uri.parse(url), headers: <String, String>{
'Access-Control-Allow-Origin': '*',
'Token': pref.getString("token")!
});
return resp.body;
@ -17,7 +43,8 @@ class API {
var resp = await http.post(Uri.parse(url),
headers: <String, String>{
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json'
'Content-Type': 'application/json',
'Token': pref.getString("token")!
},
body: jsonEncode(data));

View file

@ -1,8 +1,10 @@
import 'package:cdb_ui/api.dart';
import 'package:cdb_ui/pages/itemview.dart';
import 'package:cdb_ui/pages/setup.dart';
import 'package:flutter/material.dart';
void main() {
Future<void> main() async {
await API().init();
runApp(const MyApp());
}
@ -19,11 +21,13 @@ class MyApp extends StatelessWidget {
seedColor: Colors.deepPurple, brightness: Brightness.dark),
useMaterial3: true,
),
home: const MyHomePage(),
home: API().isInit() ? MyHomePage() : SetupPage(),
);
}
}
// todo : add setup with instance and token auth (api)
class StatsPage extends StatelessWidget {
const StatsPage({super.key});

View file

@ -21,6 +21,9 @@ class _ItemViewState extends State<ItemView> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.item.name),
),
body: Column(children: [
Row(
children: [

72
lib/pages/setup.dart Normal file
View file

@ -0,0 +1,72 @@
import 'package:cdb_ui/api.dart';
import 'package:cdb_ui/main.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
class SetupPage extends StatefulWidget {
@override
_SetupPageState createState() => _SetupPageState();
}
class _SetupPageState extends State<SetupPage> {
final TextEditingController _urlController = TextEditingController();
final TextEditingController _tokenController = TextEditingController();
Future<void> _saveSettings() async {
String instanceUrl = _urlController.text.trim();
String token = _tokenController.text.trim();
if (instanceUrl.isEmpty || token.isEmpty) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Please fill in all fields')),
);
return;
}
API().save(instanceUrl, token);
// Indicate that the setup is complete
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Setup Complete!')),
);
// Navigate or close the setup screen
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => MyHomePage(),
));
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Setup Page'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
TextField(
controller: _urlController,
decoration: InputDecoration(labelText: 'Instance URL'),
keyboardType: TextInputType.url,
),
SizedBox(height: 16.0),
TextField(
controller: _tokenController,
decoration: InputDecoration(labelText: 'Token'),
obscureText: true,
),
SizedBox(height: 32.0),
ElevatedButton(
onPressed: _saveSettings,
child: Text('Complete Setup'),
),
],
),
),
);
}
}

View file

@ -6,7 +6,9 @@ import FlutterMacOS
import Foundation
import qr_bar_code
import shared_preferences_foundation
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
QrBarCodePlugin.register(with: registry.registrar(forPlugin: "QrBarCodePlugin"))
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
}

View file

@ -81,6 +81,22 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.3.1"
ffi:
dependency: transitive
description:
name: ffi
sha256: "16ed7b077ef01ad6170a3d0c57caa4a112a38d7a2ed5602e0aca9ca6f3d98da6"
url: "https://pub.dev"
source: hosted
version: "2.1.3"
file:
dependency: transitive
description:
name: file
sha256: "5fc22d7c25582e38ad9a8515372cd9a93834027aacf1801cf01164dac0ffa08c"
url: "https://pub.dev"
source: hosted
version: "7.0.0"
fixnum:
dependency: transitive
description:
@ -208,6 +224,38 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.9.0"
path_provider_linux:
dependency: transitive
description:
name: path_provider_linux
sha256: f7a1fe3a634fe7734c8d3f2766ad746ae2a2884abe22e241a8b301bf5cac3279
url: "https://pub.dev"
source: hosted
version: "2.2.1"
path_provider_platform_interface:
dependency: transitive
description:
name: path_provider_platform_interface
sha256: "88f5779f72ba699763fa3a3b06aa4bf6de76c8e5de842cf6f29e2e06476c2334"
url: "https://pub.dev"
source: hosted
version: "2.1.2"
path_provider_windows:
dependency: transitive
description:
name: path_provider_windows
sha256: bd6f00dbd873bfb70d0761682da2b3a2c2fccc2b9e84c495821639601d81afe7
url: "https://pub.dev"
source: hosted
version: "2.3.0"
platform:
dependency: transitive
description:
name: platform
sha256: "9b71283fc13df574056616011fb138fd3b793ea47cc509c189a6c3fa5f8a1a65"
url: "https://pub.dev"
source: hosted
version: "3.1.5"
plugin_platform_interface:
dependency: transitive
description:
@ -248,6 +296,62 @@ packages:
url: "https://pub.dev"
source: hosted
version: "3.0.1"
shared_preferences:
dependency: "direct main"
description:
name: shared_preferences
sha256: "746e5369a43170c25816cc472ee016d3a66bc13fcf430c0bc41ad7b4b2922051"
url: "https://pub.dev"
source: hosted
version: "2.3.2"
shared_preferences_android:
dependency: transitive
description:
name: shared_preferences_android
sha256: "480ba4345773f56acda9abf5f50bd966f581dac5d514e5fc4a18c62976bbba7e"
url: "https://pub.dev"
source: hosted
version: "2.3.2"
shared_preferences_foundation:
dependency: transitive
description:
name: shared_preferences_foundation
sha256: c4b35f6cb8f63c147312c054ce7c2254c8066745125264f0c88739c417fc9d9f
url: "https://pub.dev"
source: hosted
version: "2.5.2"
shared_preferences_linux:
dependency: transitive
description:
name: shared_preferences_linux
sha256: "580abfd40f415611503cae30adf626e6656dfb2f0cee8f465ece7b6defb40f2f"
url: "https://pub.dev"
source: hosted
version: "2.4.1"
shared_preferences_platform_interface:
dependency: transitive
description:
name: shared_preferences_platform_interface
sha256: "57cbf196c486bc2cf1f02b85784932c6094376284b3ad5779d1b1c6c6a816b80"
url: "https://pub.dev"
source: hosted
version: "2.4.1"
shared_preferences_web:
dependency: transitive
description:
name: shared_preferences_web
sha256: d2ca4132d3946fec2184261726b355836a82c33d7d5b67af32692aff18a4684e
url: "https://pub.dev"
source: hosted
version: "2.4.2"
shared_preferences_windows:
dependency: transitive
description:
name: shared_preferences_windows
sha256: "94ef0f72b2d71bc3e700e025db3710911bd51a71cefb65cc609dd0d9a982e3c1"
url: "https://pub.dev"
source: hosted
version: "2.4.1"
sky_engine:
dependency: transitive
description: flutter
@ -349,6 +453,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.5.1"
xdg_directories:
dependency: transitive
description:
name: xdg_directories
sha256: faea9dee56b520b55a566385b84f2e8de55e7496104adada9962e0bd11bcff1d
url: "https://pub.dev"
source: hosted
version: "1.0.4"
sdks:
dart: ">=3.4.0 <4.0.0"
flutter: ">=3.18.0-18.0.pre.54"
flutter: ">=3.22.0"

View file

@ -34,6 +34,7 @@ dependencies:
qr_bar_code: ^1.3.0
qr_bar_code_scanner_dialog: ^0.0.5
intl: ^0.18.0
shared_preferences: ^2.1.0
# The following adds the Cupertino Icons font to your application.