Skip to content

Commit

Permalink
Merge pull request #431 from bounswe/mobile/note-taking
Browse files Browse the repository at this point in the history
Mobile/note taking
  • Loading branch information
salimtirit authored Dec 6, 2022
2 parents 6f64cf1 + 8d60fc0 commit e7b85c8
Show file tree
Hide file tree
Showing 12 changed files with 167 additions and 29 deletions.
8 changes: 6 additions & 2 deletions app/mobile/lib/classes/course/course.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:math';

import 'package:bucademy/classes/topic/topic.dart';
import 'package:bucademy/classes/discussion/discussion.dart';
import 'package:bucademy/classes/note/note.dart';
import 'package:bucademy/classes/user/user.dart';
import 'package:bucademy/resources/custom_colors.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -36,10 +37,13 @@ class CourseDetailed extends Course {
List<Topic> topics = [];
List<String> badges = [];
List<DiscussionShortened> discussions = [];
List<Note>? notes = [];

CourseDetailed(super.name, super.id, super.info, super.tags, super.image, super.creator, super.numberOfEnrolled, this.topics, this.badges, this.discussions);
CourseDetailed(super.name, super.id, super.info, super.tags, super.image,
super.creator, super.numberOfEnrolled, this.topics, this.badges, this.discussions, this.notes);

factory CourseDetailed.fromJson(Map<String, dynamic> json) => _$CourseDetailedFromJson(json);
factory CourseDetailed.fromJson(Map<String, dynamic> json) =>
_$CourseDetailedFromJson(json);

@override
Map<String, dynamic> toJson() => _$CourseDetailedToJson(this);
Expand Down
4 changes: 4 additions & 0 deletions app/mobile/lib/classes/course/course.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions app/mobile/lib/classes/note/note.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import 'package:json_annotation/json_annotation.dart';

part 'note.g.dart';

@JsonSerializable()
class Note {
@JsonKey(name: 'title')
final String title;

@JsonKey(name: 'body')
String body; // body of a note may change

@JsonKey(name: '_id')
final String id;

@JsonKey(name: 'resource')
final String resourceId;

Note(this.title, this.body, this.id, this.resourceId);
factory Note.fromJson(Map<String, dynamic> json) => _$NoteFromJson(json);

Map<String, dynamic> toJson() => _$NoteToJson(this);
}
21 changes: 21 additions & 0 deletions app/mobile/lib/classes/note/note.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions app/mobile/lib/locator.config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:bucademy/services/content_service.dart';
import 'package:bucademy/services/dio_service.dart';
import 'package:bucademy/services/discussion_service.dart';
import 'package:bucademy/services/navigator_service.dart';
import 'package:bucademy/services/note_service.dart';
import 'package:bucademy/services/persistence_service.dart';
import 'package:bucademy/services/profile_service.dart';
import 'package:bucademy/services/user_service.dart';
Expand All @@ -23,5 +24,6 @@ GetIt $initGetIt(
gh.lazySingleton<DiscussionService>((() => DiscussionService()));
gh.lazySingleton<MockContentService>((() => MockContentService()));
gh.lazySingleton<ProfileService>((() => ProfileService()));
gh.lazySingleton<NoteService>((() => NoteService()));
return get;
}
2 changes: 1 addition & 1 deletion app/mobile/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void main() {
navigatorService.controller = PersistentTabController(initialIndex: 0);
WidgetsFlutterBinding.ensureInitialized();
//persistenceService.clear();
// userService.login(email: 'dotedi9105@probdd.com', password: "Password123*");
userService.login(email: 'enmuhammet23@gmail.com', password: "Password123*");
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]).then(
(_) => runApp(const MyApp()),
);
Expand Down
8 changes: 6 additions & 2 deletions app/mobile/lib/services/course_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,15 @@ class CourseService {
Future<CourseDetailed?> getCourseDetails({required String id}) async {
try {
Response response = await dioService.dio.get('/space/$id');
return CourseDetailed.fromJson(response.data['space']);
CourseDetailed c = CourseDetailed.fromJson(response.data['space']);
if(response.data['enrolled']) {
c.notes = await noteService.notesOfSpace(spaceId: id);
}
return c;
} catch (e) {
print(e);
return null;
}
return null;
}

Future<bool> enrollToSpace({required String spaceId}) async {
Expand Down
2 changes: 2 additions & 0 deletions app/mobile/lib/services/locator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:bucademy/services/content_service.dart';
import 'package:bucademy/services/dio_service.dart';
import 'package:bucademy/services/discussion_service.dart';
import 'package:bucademy/services/navigator_service.dart';
import 'package:bucademy/services/note_service.dart';
import 'package:bucademy/services/persistence_service.dart';
import 'package:bucademy/services/profile_service.dart';
import 'package:bucademy/services/user_service.dart';
Expand All @@ -28,3 +29,4 @@ DioService get dioService => GetIt.I<DioService>();
UserService get userService => GetIt.I<UserService>();
DiscussionService get discussionService => GetIt.I<DiscussionService>();
ProfileService get profileService => GetIt.I<ProfileService>();
NoteService get noteService => GetIt.I<NoteService>();
80 changes: 80 additions & 0 deletions app/mobile/lib/services/note_service.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import 'package:bucademy/classes/discussion/comment.dart';
import 'package:bucademy/classes/discussion/discussion.dart';
import 'package:bucademy/classes/note/note.dart';
import 'package:bucademy/services/locator.dart';
import 'package:dio/dio.dart';
import 'package:injectable/injectable.dart';

@lazySingleton
class NoteService {
Future<Note?> postNote({
required String title,
required String body,
required String resourceId,
}) async {
try {
Response response = await dioService.dio.post(
'/note',
data: {"resource_id": resourceId, "title": title, "body": body},
);

Map json = response.data;
return Note.fromJson(json['note']);
} catch (e) {
print(e);
}
return null;
}

Future<Note?> getNote({
required String noteId,
}) async {
try {
Response response = await dioService.dio.get('/note/$noteId');
if (response.statusCode != 200) {
return null;
}
Map json = response.data;
return Note.fromJson(json['note']);
} catch (e) {
print(e);
}
return null;
}

Future<List<Note>> notesOfSpace({
required String spaceId,
}) async {
if (userService.user == null) {
return []; // cannot get notes if the user is not logged in
}
try {
Response response = await dioService.dio.post(
'/note/getNoteList',
data: {"space_id": spaceId},
);
Map json = response.data;
return json['notes'].map<Note>((e) => Note.fromJson(e)).toList();
} catch (e) {
print(e);
}
return [];
}

Future<Note?> updateNote({
required String noteId,
required String body,
}) async {
try {
Response response = await dioService.dio.put(
'/note/update',
data: {"note_id": noteId, "body": body},
);
Map json = response.data;
return Note.fromJson(json['note']);
} catch (e) {
print(e);
}
return null;
}
}
10 changes: 5 additions & 5 deletions app/mobile/lib/view/course/coursepage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:convert';

import 'package:bucademy/classes/topic/topic.dart';
import 'package:bucademy/classes/discussion/discussion.dart';
import 'package:bucademy/classes/note/note.dart';
import 'package:bucademy/resources/constants.dart';
import 'package:bucademy/classes/course/course.dart';
import 'package:bucademy/resources/custom_colors.dart';
Expand Down Expand Up @@ -226,13 +227,12 @@ Widget coursePageView(Course c) => ViewModelBuilder<
shrinkWrap: true,
padding: const EdgeInsets.all(10.0),
children: [
...contentService
.contents("Note")
.map((MockContent m) => GestureDetector(
child: mockTile(m.name),
...(viewModel.course!.notes ?? [])
.map((Note n) => GestureDetector(
child: mockTile(n.title ),
onTap: () => PersistentNavBarNavigator
.pushNewScreen(context,
screen: noteView(noteId: ""),
screen: noteView(note: n ),
withNavBar: false),
))
],
Expand Down
31 changes: 14 additions & 17 deletions app/mobile/lib/view/course/note/note_view.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import 'package:bucademy/classes/note/note.dart';
import 'package:bucademy/resources/custom_colors.dart';
import 'package:bucademy/services/locator.dart';
import 'package:bucademy/view/widgets/markdown_input.dart';
import 'package:flutter/material.dart';
import 'package:stacked/stacked.dart';

String initialText = "## Realism \n- Photorealism\n- Abstract\n- Surrealism ";

Widget noteView({required String noteId}) =>
Widget noteView({required Note note}) =>
ViewModelBuilder<NoteViewModel>.reactive(
viewModelBuilder: () => NoteViewModel(noteId),
onModelReady: (model) => model.init(),
viewModelBuilder: () => NoteViewModel(note),
builder: (context, viewModel, child) => Scaffold(
appBar: AppBar(
title: const Text("My Note"),
title: Text(note.title),
backgroundColor: CustomColors.main,
),
body: viewModel.loading
Expand All @@ -31,27 +32,23 @@ Widget noteView({required String noteId}) =>
);

class NoteViewModel extends ChangeNotifier {
final String noteId;
String? body = initialText;
final Note note;
bool loading = false;
TextEditingController controller = TextEditingController();

NoteViewModel(this.noteId);

Future updateBody() async {
body = controller.text;
await Future.delayed(const Duration(milliseconds: 1));
notifyListeners();
NoteViewModel(this.note) {
controller.text = note.body;
}

init() async {
Future updateBody() async {
loading = true;
controller.text = initialText;
notifyListeners();

// discussion = await discussionService.getDiscussion(discussionId: noteId);
// if (discussion == null) return;

Note? updated =
await noteService.updateNote(noteId: note.id, body: controller.text);
if (updated != null) {
note.body = updated.body;
}
loading = false;
notifyListeners();
}
Expand Down
5 changes: 3 additions & 2 deletions app/mobile/lib/view/home/course_tile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,12 @@ GestureDetector courseTile(Course c, BuildContext context, {bool isClickable = t
],
),
Text(
'${NumberFormat.compactCurrency(
// ignore: prefer_interpolation_to_compose_strings
(c.numberOfEnrolled < 1000 ? c.numberOfEnrolled.toString():NumberFormat.compactCurrency(
decimalDigits: 2,
locale: 'en_US',
symbol: '',
).format(c.numberOfEnrolled)} Learners',
).format(c.numberOfEnrolled)) + " Learners",
style: TextStyles.bodyWhite),
]),
],
Expand Down

0 comments on commit e7b85c8

Please sign in to comment.