Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow tags#58 #130

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
22 changes: 16 additions & 6 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:taskly/providers/tags_provider.dart';
import 'package:taskly/providers/theme_provider.dart';
import 'package:taskly/screens/home_screen.dart';
import 'package:taskly/screens/intro_screen.dart';
import 'package:taskly/screens/splash_screen.dart';
import 'package:taskly/service/local_db_service.dart';
import 'package:taskly/service/speech_service.dart';
import 'package:taskly/storage/tags_storage.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();
Expand All @@ -28,20 +30,27 @@ class TasklyApp extends StatefulWidget {

class _TasklyAppState extends State<TasklyApp> {
late ThemeProvider themeProvider;

TagsProvider tagsProvider = TagsProvider();

@override
void initState() {
super.initState();
themeProvider =
ThemeProvider();
themeProvider = ThemeProvider();
SpeechService.intialize();
_loadTags();
}

void _loadTags() async {
tagsProvider.updateTags(await TagsStorage.loadTags());
}

@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (context) => themeProvider,
return MultiProvider(
providers: [
ChangeNotifierProvider(create: (_) => themeProvider),
ChangeNotifierProvider(create: (_) => tagsProvider),
],
child: Consumer<ThemeProvider>(
builder: (context, value, child) => MaterialApp(
debugShowCheckedModeBanner: false,
Expand All @@ -59,7 +68,8 @@ class _TasklyAppState extends State<TasklyApp> {
// home: widget.isFirstTime ? const IntroScreen() : const HomeScreen(),
initialRoute: '/',
routes: {
'/': (context) => SplashScreen(), // Add Splash Screen as the initial route
'/': (context) =>
SplashScreen(), // Add Splash Screen as the initial route
'/main': (context) =>
widget.isFirstTime ? const IntroScreen() : const HomeScreen(),
},
Expand Down
61 changes: 61 additions & 0 deletions lib/models/tag.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import 'dart:convert';
import 'dart:math';

import 'package:flutter/material.dart';

class Tag {
String id;
String title;
Color color;

Tag({
Color? color,
String? id,
required this.title,
}) : color = color ??
Colors.primaries[Random().nextInt(Colors.primaries.length)],
id = id ?? DateTime.now().millisecondsSinceEpoch.toString();

Tag copyWith({
String? id,
String? title,
Color? color,
}) {
return Tag(
id: id ?? this.id,
title: title ?? this.title,
color: color ?? this.color,
);
}

Map<String, dynamic> toMap() {
return {
'id': id,
'title': title,
'color': color.value,
};
}

factory Tag.fromMap(Map<String, dynamic> map) {
return Tag(
id: map['id'] ?? '',
title: map['title'] ?? '',
color: Color(map['color']),
);
}

String toJson() => json.encode(toMap());

factory Tag.fromJson(String source) => Tag.fromMap(json.decode(source));

@override
String toString() => 'Tag(id: $id, title: $title, color: $color)';

@override
bool operator ==(Object other) {
return other is Tag && other.id == id;
}

@override
int get hashCode => id.hashCode ^ title.hashCode ^ color.hashCode;
}
7 changes: 6 additions & 1 deletion lib/models/task.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Task {
int? recurringDays;
Task? dependency;
List<Subtask> subtasks;
List<String> tags; // the tag ids

bool get isRecurring => recurringDays != null;

Expand All @@ -27,8 +28,10 @@ class Task {
this.dependency,
String? id,
List<Subtask>? subtasks,
List<String>? tags,
}) : id = id ?? DateTime.now().microsecondsSinceEpoch.toString(),
subtasks = subtasks ?? [];
subtasks = subtasks ?? [],
tags = tags ?? [];

// Convert a Task object to JSON
Map<String, dynamic> toJson() {
Expand All @@ -41,6 +44,7 @@ class Task {
'recurringDays': recurringDays,
'color': color.value,
'id': id,
'tags': tags,
'subtasks': subtasks.map((e) => e.toMap()).toList(),
};
}
Expand All @@ -65,6 +69,7 @@ class Task {
recurringDays: json['recurringDays'],
color: Color(json['color']),
id: json['id'] ?? DateTime.now().millisecondsSinceEpoch.toString(),
tags: List.from(json['tags']),
subtasks: subtasks,
);
}
Expand Down
11 changes: 11 additions & 0 deletions lib/providers/tags_provider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'package:flutter/material.dart';
import 'package:taskly/models/tag.dart';

class TagsProvider extends ChangeNotifier {
List<Tag> allTags = [];

void updateTags(List<Tag> tags) {
allTags = tags;
notifyListeners();
}
}
20 changes: 12 additions & 8 deletions lib/screens/home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,12 @@ class _HomeScreenState extends State<HomeScreen> {
await TaskStorage.saveTasks(tasks);
}

void kudosForMeditation(int scoreChange, String mssg) async{
kudos.score += scoreChange;
kudos.history.add([mssg, scoreChange.toString()]);
await KudosStorage.saveKudos(kudos);
}
void kudosForMeditation(int scoreChange, String mssg) async {
kudos.score += scoreChange;
kudos.history.add([mssg, scoreChange.toString()]);
await KudosStorage.saveKudos(kudos);
}

// Handle task options, now using the enum
void _onOptionSelected(TaskOption option) {
setState(() {
Expand Down Expand Up @@ -190,13 +191,16 @@ void kudosForMeditation(int scoreChange, String mssg) async{
TaskStorage.saveTasks(tasks);
});
} else if (option == TaskOption.launchMeditationScreen) {
Navigator.push(context,
MaterialPageRoute(builder: (context) => MeditationScreen(kudosForMeditation: kudosForMeditation)));
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
MeditationScreen(kudosForMeditation: kudosForMeditation)));
} else if (option == TaskOption.toggleTipVisibility) {
showtip = !showtip;
} else if (option == TaskOption.exportToCSV) {
exportToCSV(tasks);
} else if (option == TaskOption.defaultColor){
} else if (option == TaskOption.defaultColor) {
showColorPickerDialog(context);
}
});
Expand Down
2 changes: 1 addition & 1 deletion lib/screens/task_box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class _TaskBoxWidgetState extends State<TaskBoxWidget> {
),
IconButton(
icon: const Icon(Icons.share_rounded),
onPressed: onShare,
onPressed: widget.onShare,
),
IconButton(
icon: const Icon(Icons.edit, color: Colors.blue),
Expand Down
Loading