Skip to content

Commit

Permalink
Fixed some issue UI
Browse files Browse the repository at this point in the history
  • Loading branch information
kekko7072 committed Feb 7, 2023
1 parent fc1c7e9 commit 5cfdfd7
Show file tree
Hide file tree
Showing 9 changed files with 232 additions and 172 deletions.
284 changes: 135 additions & 149 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import 'package:lms/services/database_local.dart';
import 'package:lms/services/imports.dart';
import 'package:about/about.dart';
import 'package:flutter/cupertino.dart';
import 'package:lms/models/link.dart';
import 'package:url_launcher/url_launcher_string.dart';

const int kDBVersion = 1;

Expand Down Expand Up @@ -30,6 +32,7 @@ class MyApp extends StatelessWidget {
primarySwatch: Colors.pink,
),
home: const MyHomePage(),
builder: EasyLoading.init(),
);
}
}
Expand All @@ -54,68 +57,6 @@ class _MyHomePageState extends State<MyHomePage> {
return prefs!.getBool(kDBConfigured) ?? false;
}

Future<void> openDB() async {
DatabaseFactory databaseFactory = databaseFactoryFfi;

databaseFactory.setDatabasesPath(kDBPath);

db = await databaseFactory.openDatabase(kDBPath);
if (kDebugMode) {
print('DB VERSION:');
print(await db?.getVersion());
}

if (await dbConfigured() && await db?.getVersion() == kDBVersion) {
// if (db.getVersion()) expect(await db.getVersion(), 0);
} else {
sqfliteFfiInit();
await prefs?.setBool('db_configured', true);

await db?.setVersion(kDBVersion);

await db?.execute('''
CREATE TABLE Links (
id INTEGER PRIMARY KEY,
title TEXT,
description TEXT,
url TEXT
)
''');
}
if (kDebugMode) {
print('QUERY FROM DATABASE');
print(await db?.query('Links'));
print('\n\n');
}
links = Link.fromJson(body: await db?.query('Links'));

if (kDebugMode) {
print('first read DB');
}
setState(() {});
}

Future<void> closeDB() async {
if (db != null && db!.isOpen) {
await db!.close();
}
}

Future<bool> addLink(
{required String title,
required String description,
required String url}) async {
await db?.insert('Links', <String, Object?>{
'title': title,
'description': description,
'url': url
});
if (await db?.query('Links', where: 'url = ?', whereArgs: [url]) != null) {
return true;
}
return false;
}

Future<void> deleteLink({required String id}) async {
await db?.delete('Links', where: 'id = ?', whereArgs: [id]);
setState(() {});
Expand Down Expand Up @@ -145,31 +86,33 @@ class _MyHomePageState extends State<MyHomePage> {
}

void _showMaterialDialog() async {
if (!_isEnabled && !await dbConfigured()) {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: const Text('Apertura al login'),
content: const Text('Vuoi che LMS si apra al login?'),
actions: <Widget>[
TextButton(
await dbConfigured().then((value) {
if (!_isEnabled && !value) {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: const Text('Open at login'),
content: const Text('Do you want LMS open at login?'),
actions: <Widget>[
TextButton(
onPressed: () async {
await _handleDisable();
Navigator.of(context).pop();
},
child: const Text('No')),
TextButton(
onPressed: () async {
await _handleDisable();
await _handleEnable();
Navigator.of(context).pop();
},
child: const Text('No')),
TextButton(
onPressed: () async {
await _handleEnable();
Navigator.of(context).pop();
},
child: const Text('Si'),
)
],
);
});
}
child: const Text('Si'),
)
],
);
});
}
});
}

@override
Expand All @@ -181,7 +124,7 @@ class _MyHomePageState extends State<MyHomePage> {

@override
void dispose() {
closeDB();
DatabaseLocal(db).closeDB();
super.dispose();
}

Expand All @@ -191,55 +134,12 @@ class _MyHomePageState extends State<MyHomePage> {
return Scaffold(
appBar: AppBar(
leading: IconButton(
onPressed: () async {
PackageInfo packageInfo = await PackageInfo.fromPlatform();

showAboutPage(
context: context,
values: {
'version': packageInfo.version,
'year': DateTime.now().year.toString(),
},
applicationLegalese:
'Copyright © Simone Porcari | Riccardo Rettore | Francesco Vezzani, {{ year }}',
applicationDescription:
const Text('Applicazione per la gestione dei link.'),
children: <Widget>[
const MarkdownPageListTile(
icon: Icon(Icons.list),
title: Text('Changelog'),
filename: 'CHANGELOG.md',
),
const LicensesPageListTile(
icon: Icon(Icons.favorite),
),
StatefulBuilder(
builder: (context, setState) => ListTile(
leading: Icon(_isEnabled
? CupertinoIcons.check_mark_circled
: CupertinoIcons.xmark_circle),
title: const Text('Apertura al login'),
onTap: () async {
if (_isEnabled) {
await launchAtStartup.disable();
} else {
await launchAtStartup.enable();
}
setState(() => _isEnabled = !_isEnabled);
}),
),
],
applicationIcon: const SizedBox(
width: 100,
height: 100,
child: Image(
image: AssetImage('assets/icon.png'),
),
),
);
},
onPressed: () => showAboutApp(),
icon: const Icon(CupertinoIcons.infinite)),
title: const Text('Link Management System'),
title: GestureDetector(
child: const Text('Link Management System'),
onTap: () => showAboutApp(),
),
),
body: Center(
child: Column(
Expand Down Expand Up @@ -279,9 +179,9 @@ class _MyHomePageState extends State<MyHomePage> {
style: Theme.of(context).textTheme.subtitle1,
),
TextButton(
child: const Text('Apri link'),
child: const Text('Open link'),
onPressed: () async {
if (!await launch(link.url.toString())) {
if (!await launchUrlString(link.url.toString())) {
throw 'Could not launch ${link.url.toString()}';
}
},
Expand All @@ -306,30 +206,30 @@ class _MyHomePageState extends State<MyHomePage> {
String url = '';

return AlertDialog(
title: const Text('Inserisci link'),
title: const Text('Insert link'),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
TextField(
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Titolo',
labelText: 'Title (es. Monday meeting)',
),
onChanged: (value) => title = value,
),
const SizedBox(height: 10),
TextField(
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Descrizione',
labelText: 'Description (es. Every friday)',
),
onChanged: (value) => description = value,
),
const SizedBox(height: 10),
TextField(
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Link del sito o della videochiamata',
labelText: 'Link (ex. site or meeting)',
),
onChanged: (value) => url = value,
),
Expand All @@ -339,7 +239,7 @@ class _MyHomePageState extends State<MyHomePage> {
actions: <Widget>[
TextButton(
child: Text(
'Annulla',
'Cancel',
style: Theme.of(context)
.textTheme
.button!
Expand All @@ -350,21 +250,107 @@ class _MyHomePageState extends State<MyHomePage> {
},
),
TextButton(
child: const Text('Salva'),
onPressed: () async {
await addLink(
title: title, description: description, url: url)
.then((value) => readDB());
Navigator.of(context).pop();
},
child: const Text('Save'),
onPressed: () async => await DatabaseLocal(db)
.addLink(title: title, description: description, url: url)
.then((value) => readDB())
.whenComplete(() => Navigator.of(context).pop()),
),
],
);
},
),
tooltip: 'Aggiungi link',
tooltip: 'Add link',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
),
);
}

Future<void> openDB() async {
DatabaseFactory databaseFactory = databaseFactoryFfi;

databaseFactory.setDatabasesPath(kDBPath);

db = await databaseFactory.openDatabase(kDBPath);
if (kDebugMode) {
print('DB VERSION:');
print(await db?.getVersion());
}

if (await dbConfigured() && await db?.getVersion() == kDBVersion) {
// if (db.getVersion()) expect(await db.getVersion(), 0);
} else {
sqfliteFfiInit();
await prefs?.setBool('db_configured', true);

await db?.setVersion(kDBVersion);

await db?.execute('''
CREATE TABLE Links (
id INTEGER PRIMARY KEY,
title TEXT,
description TEXT,
url TEXT
)
''');
}
if (kDebugMode) {
print('QUERY FROM DATABASE');
print(await db?.query('Links'));
print('\n\n');
}
links = Link.fromJson(body: await db?.query('Links'));

if (kDebugMode) {
print('first read DB');
}
setState(() {});
}

Future<void> showAboutApp() async => await PackageInfo.fromPlatform()
.then((PackageInfo packageInfo) => showAboutPage(
context: context,
values: {
'version': packageInfo.version,
'year': DateTime.now().year.toString(),
},
applicationLegalese:
'Copyright © Simone Porcari | Riccardo Rettore | Francesco Vezzani, {{ year }}',
applicationDescription:
const Text('Desktop application for links management.'),
children: <Widget>[
const MarkdownPageListTile(
icon: Icon(Icons.list),
title: Text('Changelog'),
filename: 'CHANGELOG.md',
),
const LicensesPageListTile(
icon: Icon(Icons.favorite),
),
StatefulBuilder(
builder: (context, setState) => ListTile(
leading: Icon(_isEnabled
? CupertinoIcons.check_mark_circled
: CupertinoIcons.xmark_circle),
title: const Text('Launch at startup'),
onTap: () async {
if (_isEnabled) {
EasyLoading.showInfo('Disabled launch at startup');
await launchAtStartup.disable();
} else {
EasyLoading.showInfo('Enabled launch at startup');
await launchAtStartup.enable();
}
setState(() => _isEnabled = !_isEnabled);
}),
),
],
applicationIcon: const SizedBox(
width: 100,
height: 100,
child: Image(
image: AssetImage('assets/icon.png'),
),
),
));
}
Loading

0 comments on commit 5cfdfd7

Please sign in to comment.