Skip to content

Commit

Permalink
format + minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
joske committed Dec 6, 2023
1 parent 70cf3a8 commit f665755
Show file tree
Hide file tree
Showing 19 changed files with 61 additions and 115 deletions.
5 changes: 2 additions & 3 deletions lib/add_ascent_screen-ios.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
Expand Down Expand Up @@ -93,7 +92,7 @@ class _CupertinoAddAscentScreenState extends State<CupertinoAddAscentScreen> {
future: DatabaseHelper.getCrags(),
initialData: List.empty(),
builder: (context, snapshot) {
if (snapshot.data == null || snapshot.data!.length == 0) return Center();
if (snapshot.data == null || snapshot.data!.length == 0) return CircularProgressIndicator();
crags = snapshot.data;
if (passedAscent != null) {
Crag c = crags!.firstWhere((element) => element.id == cragId);
Expand Down Expand Up @@ -145,7 +144,7 @@ class _CupertinoAddAscentScreenState extends State<CupertinoAddAscentScreen> {
child: FutureBuilder<List<String>>(
future: DatabaseHelper.getGrades(),
builder: (context, snapshot) {
if (!snapshot.hasData) return Center();
if (!snapshot.hasData) CircularProgressIndicator();
grades = snapshot.data;
var fixedExtentScrollController;
String c = grades!.firstWhere((element) => element == grade);
Expand Down
5 changes: 2 additions & 3 deletions lib/add_ascent_screen.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

Expand Down Expand Up @@ -86,7 +85,7 @@ class _AddAscentScreenState extends State<AddAscentScreen> {
future: DatabaseHelper.getCrags(),
initialData: List.empty(),
builder: (context, snapshot) {
if (!snapshot.hasData) return Center();
if (!snapshot.hasData) return CircularProgressIndicator();
return new DropdownButton(
value: cragId,
hint: Text("Select Crag"),
Expand Down Expand Up @@ -129,7 +128,7 @@ class _AddAscentScreenState extends State<AddAscentScreen> {
FutureBuilder<List>(
future: DatabaseHelper.getGrades(),
builder: (context, snapshot) {
if (!snapshot.hasData) return Center();
if (!snapshot.hasData) return CircularProgressIndicator();
return new DropdownButton(
value: grade,
items: buildGradeList(snapshot),
Expand Down
1 change: 0 additions & 1 deletion lib/add_crag-ios.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import 'package:flutter/cupertino.dart';

import 'crag.dart';
Expand Down
1 change: 0 additions & 1 deletion lib/add_crag.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import 'package:flutter/material.dart';

import 'crag.dart';
Expand Down
1 change: 0 additions & 1 deletion lib/ascent.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import 'package:intl/intl.dart';

import 'crag.dart';
Expand Down
1 change: 0 additions & 1 deletion lib/crag.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

class Crag {
int? id = -1;
String? name;
Expand Down
72 changes: 22 additions & 50 deletions lib/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ class DatabaseHelper {
if (cragId == null) {
cragId = await addCrag(crag);
} else {
print(
"got existing crag ('${crag.name}', '${crag.country}') with id $cragId");
print("got existing crag ('${crag.name}', '${crag.country}') with id $cragId");
}
} else {
// crag id but no name? probably import scenario
Expand All @@ -48,8 +47,7 @@ class DatabaseHelper {
if (list.isNotEmpty) {
crag.name = list[0].name;
crag.country = list[0].country;
print(
"got existing crag ('${crag.name}', '${crag.country}') with id $cragId");
print("got existing crag ('${crag.name}', '${crag.country}') with id $cragId");
}
}
}
Expand Down Expand Up @@ -83,39 +81,32 @@ class DatabaseHelper {
static Future<int> addCrag(Crag crag) async {
await init();
int id = await _db!.insert("crag", crag.toMap());
print("inserted crag ('${crag.name}', '${crag.country}') at id " +
id.toString());
print("inserted crag ('${crag.name}', '${crag.country}') at id " + id.toString());
crag.id = id;
return id;
}

static Future<int> updateCrag(Crag crag) async {
await init();
var id = await _db!
.update("crag", crag.toMap(), where: '_id = ?', whereArgs: [crag.id]);
print("updated crag ('${crag.name}', '${crag.country}') at id " +
id.toString());
var id = await _db!.update("crag", crag.toMap(), where: '_id = ?', whereArgs: [crag.id]);
print("updated crag ('${crag.name}', '${crag.country}') at id " + id.toString());
return id;
}

static Future<List<Crag>> getCrags() async {
await init();
final List<Map<String, Object?>> queryResult =
await _db!.query('crag', orderBy: "name");
final List<Map<String, Object?>> queryResult = await _db!.query('crag', orderBy: "name");
return queryResult.map((e) => Crag.fromMap(e)).toList();
}

static Future<int?> getCrag(String? name, String? country) async {
await init();
return Sqflite.firstIntValue(await _db!.rawQuery(
'select _id from crag where name = ? and country = ?',
[name, country]));
return Sqflite.firstIntValue(await _db!.rawQuery('select _id from crag where name = ? and country = ?', [name, country]));
}

static Future<List<Crag>> getCragFromId(int id) async {
await init();
final List<Map<String, Object?>> queryResult =
await _db!.rawQuery('select * from crag where _id = ? ', [id]);
final List<Map<String, Object?>> queryResult = await _db!.rawQuery('select * from crag where _id = ? ', [id]);
return queryResult.map((e) => Crag.fromMap(e)).toList();
}

Expand Down Expand Up @@ -150,17 +141,15 @@ class DatabaseHelper {

static Future<List<String>> getYearsWithAscents() async {
await init();
List<Map<String, Object?>> res = await _db!
.query('ascent_routes', columns: ["date"], orderBy: "date ASC");
List<Map<String, Object?>> res = await _db!.query('ascent_routes', columns: ["date"], orderBy: "date ASC");
DateTime firstYear = DateTime.parse(res.first["date"] as String);
int numYears = new DateTime.now().year - firstYear.year + 1;
var list = List.generate(numYears, (i) => (firstYear.year + i).toString());
list.insert(0, "All");
return list;
}

static Future<List<Ascent>> getAscentsForCrag(
String? year, int cragId) async {
static Future<List<Ascent>> getAscentsForCrag(String? year, int cragId) async {
await init();
String where;
List<Object?> args = List.empty(growable: true);
Expand All @@ -176,13 +165,11 @@ class DatabaseHelper {
return getAscentsWhere(where, args);
}

static Future<List<Ascent>> getAscentsWhere(
String? where, List<Object?>? args) async {
static Future<List<Ascent>> getAscentsWhere(String? where, List<Object?>? args) async {
await init();
List<Map<String, Object?>> queryResult;
if (where != null) {
queryResult = await _db!.query('ascent_routes',
where: where, whereArgs: args, orderBy: "date desc");
queryResult = await _db!.query('ascent_routes', where: where, whereArgs: args, orderBy: "date desc");
} else {
queryResult = await _db!.query('ascent_routes', orderBy: "date desc");
}
Expand All @@ -206,8 +193,7 @@ class DatabaseHelper {
} else {
where = notTried;
}
queryResult = await _db!.query('ascent_routes',
orderBy: "score desc, date desc", where: where, limit: 10);
queryResult = await _db!.query('ascent_routes', orderBy: "score desc, date desc", where: where, limit: 10);
return queryResult.map((e) => Ascent.fromMap(e)).toList();
}

Expand All @@ -232,13 +218,8 @@ class DatabaseHelper {
} else {
where = notTried;
}
queryResult = await _db!.query('ascent_routes',
columns: ["score"],
orderBy: "score desc, date desc",
where: where,
limit: 10);
int score =
queryResult.map((e) => e["score"]).fold(0, (p, n) => p + (n as int));
queryResult = await _db!.query('ascent_routes', columns: ["score"], orderBy: "score desc, date desc", where: where, limit: 10);
int score = queryResult.map((e) => e["score"]).fold(0, (p, n) => p + (n as int));
return score;
}

Expand All @@ -247,10 +228,8 @@ class DatabaseHelper {
int gradeScore = (await getGradeScore(ascent.route!.grade))!;
int styleScore = (await getStyleScore(ascent.style!.id))!;
ascent.score = gradeScore + styleScore;
await _db!.update("routes", ascent.route!.toMap(),
where: '_id = ?', whereArgs: [ascent.route!.id]);
await _db!.update("ascents", ascent.toMap(),
where: '_id = ?', whereArgs: [ascent.id]);
await _db!.update("routes", ascent.route!.toMap(), where: '_id = ?', whereArgs: [ascent.route!.id]);
await _db!.update("ascents", ascent.toMap(), where: '_id = ?', whereArgs: [ascent.id]);
}

static Future<void> deleteAscent(Ascent ascent) async {
Expand All @@ -260,8 +239,7 @@ class DatabaseHelper {

static Future<int?> getGradeScore(String? grade) async {
await init();
return Sqflite.firstIntValue(await _db!.query("grades",
columns: ["score"], where: "grade = ?", whereArgs: [grade]));
return Sqflite.firstIntValue(await _db!.query("grades", columns: ["score"], where: "grade = ?", whereArgs: [grade]));
}

static Future<String> getScore() async {
Expand All @@ -275,12 +253,10 @@ class DatabaseHelper {

static Future<int?> getStyleScore(int? style) async {
await init();
return Sqflite.firstIntValue(await _db!.query("styles",
columns: ["score"], where: "_id = ?", whereArgs: [style]));
return Sqflite.firstIntValue(await _db!.query("styles", columns: ["score"], where: "_id = ?", whereArgs: [style]));
}

static int calculateScore(
int attempts, int style, int gradeScore, int styleScore) {
static int calculateScore(int attempts, int style, int gradeScore, int styleScore) {
int totalScore = gradeScore + styleScore;
if (style == 2 && attempts == 2) {
totalScore += 2;
Expand All @@ -296,10 +272,7 @@ class DatabaseHelper {
groupBy: "route_grade",
orderBy: "route_grade desc");
var tried = await _db!.query("ascent_routes",
columns: ["route_grade", "count(*) as tried"],
where: "style_id = 7",
groupBy: "route_grade",
orderBy: "route_grade desc");
columns: ["route_grade", "count(*) as tried"], where: "style_id = 7", groupBy: "route_grade", orderBy: "route_grade desc");
List<String> grades = await getGrades();
List<Stats> stats = [];
Map<String?, int?> doneMap = Map();
Expand All @@ -308,8 +281,7 @@ class DatabaseHelper {
doneMap.putIfAbsent(e["route_grade"] as String?, () => e["done"] as int?);
}
for (var e in tried) {
triedMap.putIfAbsent(
e["route_grade"] as String?, () => e["tried"] as int?);
triedMap.putIfAbsent(e["route_grade"] as String?, () => e["tried"] as int?);
}
for (var g in grades.reversed) {
int? done = 0;
Expand Down
25 changes: 9 additions & 16 deletions lib/home-ios.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ class CupertinoHomeState extends State<CupertinoHome> {
trailing: CupertinoButton(
child: Icon(Icons.add),
onPressed: () async {
await showMaterialDialog(
context, "Add Crag", CupertinoAddCragScreen(), [], 200, 400);
await showMaterialDialog(context, "Add Crag", CupertinoAddCragScreen(), [], 200, 400);
setState(() {});
},
),
Expand All @@ -118,8 +117,7 @@ class CupertinoHomeState extends State<CupertinoHome> {
onPressed: () async {
await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => CupertinoAddAscentScreen()),
MaterialPageRoute(builder: (context) => CupertinoAddAscentScreen()),
);
setState(() {});
},
Expand All @@ -131,7 +129,6 @@ class CupertinoHomeState extends State<CupertinoHome> {
}

Widget buildBody(BuildContext context) {
Future<List<Ascent>> ascents = DatabaseHelper.getAscents(query);
return Column(
children: [
Container(
Expand All @@ -149,10 +146,10 @@ class CupertinoHomeState extends State<CupertinoHome> {
Container(
color: Colors.grey[200],
child: FutureBuilder(
future: ascents,
future: DatabaseHelper.getAscents(query),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Center();
return CircularProgressIndicator();
}
final data = snapshot.data as List<Ascent>;
int len = data.length;
Expand All @@ -176,7 +173,7 @@ class CupertinoHomeState extends State<CupertinoHome> {
),
),
Flexible(
child: createScrollView(context, ascents, _buildRow),
child: createScrollView(context, DatabaseHelper.getAscents(query), _buildRow),
)
],
);
Expand Down Expand Up @@ -207,17 +204,15 @@ class CupertinoHomeState extends State<CupertinoHome> {
),
],
),
trailing:
createPopup(ascent, ['edit', 'delete'], [editAscent, deleteAscent]),
trailing: createPopup(ascent, ['edit', 'delete'], [editAscent, deleteAscent]),
),
);
}

void editAscent(Ascent ascent) async {
await Navigator.push(
context,
CupertinoPageRoute(
builder: (context) => CupertinoAddAscentScreen(passedAscent: ascent)),
CupertinoPageRoute(builder: (context) => CupertinoAddAscentScreen(passedAscent: ascent)),
);
setState(() {});
}
Expand All @@ -240,10 +235,8 @@ class CupertinoHomeState extends State<CupertinoHome> {
BottomNavigationBarItem(icon: Icon(CupertinoIcons.home), label: "Home"),
BottomNavigationBarItem(icon: Icon(CupertinoIcons.map), label: "Crags"),
BottomNavigationBarItem(icon: Icon(CupertinoIcons.sum), label: "Summary"),
BottomNavigationBarItem(
icon: Icon(CupertinoIcons.chart_bar), label: "Statistics"),
BottomNavigationBarItem(
icon: Icon(CupertinoIcons.floppy_disk), label: "Import/Export"),
BottomNavigationBarItem(icon: Icon(CupertinoIcons.chart_bar), label: "Statistics"),
BottomNavigationBarItem(icon: Icon(CupertinoIcons.floppy_disk), label: "Import/Export"),
];
}
}
2 changes: 1 addition & 1 deletion lib/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class MaterialHomeState extends State<MaterialHome> {
future: ascents,
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Center();
return CircularProgressIndicator();
}
final data = snapshot.data as List<Ascent>;
var len = data.length;
Expand Down
1 change: 0 additions & 1 deletion lib/import.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import 'dart:convert';
import 'dart:io';

Expand Down
1 change: 0 additions & 1 deletion lib/importscreen-ios.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import 'package:ascent/util.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
Expand Down
Loading

0 comments on commit f665755

Please sign in to comment.