Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 58 additions & 19 deletions lib/pages/editNotePage.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:notes/models/note.dart';
import 'package:notes/providers/providers.dart';
import 'package:notes/utils/constants.dart';
import 'package:notes/widgets/last_edited_label.dart';
import 'package:notes/widgets/note_pin.dart';
import 'package:share_plus/share_plus.dart';

Expand All @@ -17,28 +20,42 @@ class EditNotePage extends StatefulWidget {
class _EditNotePageState extends State<EditNotePage> {
TextEditingController t1;
TextEditingController t2;
bool isEdited;
Note currentNote;
int isPinned;

bool isEdited;
@override
void initState() {
super.initState();
isEdited = false;

t1 = TextEditingController();
t2 = TextEditingController();
currentNote = context.read(NoteProvider(widget.id));
isPinned = currentNote.isPinned;

t1 = TextEditingController();
t2 = TextEditingController();

// make sure to add listeners after the intial value has been set
// because we don't to notify listeners when the initial value is set

t1.text = currentNote.title;
t1.addListener(() {
// this fixes the issue where all listeners were notified When TextField was focused
t1.value = TextEditingValue(text: t1.text,selection: TextSelection.collapsed(offset: t1.text.length));
});

t2.text = currentNote.content;
t2.addListener(() {
// this fixes the issue where all listeners were notified When TextField was focused
t2.value = TextEditingValue(text: t2.text,selection: TextSelection.collapsed(offset: t2.text.length));
});

isEdited = a.value;
}

@override
void dispose() {
t1.dispose();
t2.dispose();
a.value = false;
super.dispose();
}

Expand Down Expand Up @@ -70,9 +87,10 @@ class _EditNotePageState extends State<EditNotePage> {
bool isDiscarded = false;
if (isEdited) {
isDiscarded = await _updateOrDiscard(
context, t1.text, t2.text, currentNote,isPinned);
context, t1.text, t2.text, currentNote, isPinned);
}
Navigator.pop(context, isDiscarded);

}),
actions: [
// Consumer(
Expand All @@ -96,12 +114,18 @@ class _EditNotePageState extends State<EditNotePage> {
// }
// },
// ),
NotePin(isPinned: isPinned,onChanged: (val){
NotePin(
isPinned: isPinned,
onChanged: (val) {
isPinned = val;
isEdited = true;
},),
IconButton(icon: Icon(Icons.share), onPressed: (){
Share.share(t1.text + '\n' + t2.text,subject: t1.text);
a.value = true;
},
),
IconButton(
icon: Icon(Icons.share),
onPressed: () {
Share.share(t1.text + '\n' + t2.text, subject: t1.text);
})
],
),
Expand All @@ -110,9 +134,10 @@ class _EditNotePageState extends State<EditNotePage> {
bool isDiscarded = false;
if (isEdited) {
isDiscarded = await _updateOrDiscard(
context, t1.text, t2.text, currentNote,isPinned);
context, t1.text, t2.text, currentNote, isPinned);
}
Navigator.pop(context, isDiscarded);

return false;
},
child: SingleChildScrollView(
Expand All @@ -126,10 +151,11 @@ class _EditNotePageState extends State<EditNotePage> {
Column(
children: [
TextField(
onChanged: (_) {
onChanged: (val) {
isEdited = true;
a.value = true;
},
showCursor: true,
// showCursor: true,
autofocus: true,
style: const TextStyle(
fontSize: 25,
Expand All @@ -144,8 +170,9 @@ class _EditNotePageState extends State<EditNotePage> {
border: InputBorder.none),
),
TextField(
onChanged: (_) {
onChanged: (val) {
isEdited = true;
a.value = true;
},
style: const TextStyle(color: Colors.white),
maxLines: null,
Expand All @@ -163,6 +190,20 @@ class _EditNotePageState extends State<EditNotePage> {
),
),
),
bottomSheet: Container(
height: 50.0,
// padding: EdgeInsets.only(bottom: 8.0),
color: kBackgroundColor,
child: Column(
children: [
Center(
child: LastEditedLabel(
last_updated: currentNote.last_updated,
),
)
],
),
),
),
);
}
Expand All @@ -189,20 +230,18 @@ Future<bool> _updateOrDiscard(
newNote.id = currentNote.id;
// if isPinned is changed update the note and also update HomePage
bool _shouldUpdateHomePage = false;
if(currentNote.isPinned != isPinned){
if (currentNote.isPinned != isPinned) {
_shouldUpdateHomePage = true;
}
// update note
await context.read(NoteProvider(currentNote.id)).update(newNote);

// setPin() and unsetPin methods will update the homepage if it should be updated
if(_shouldUpdateHomePage && isPinned == 1){
if (_shouldUpdateHomePage && isPinned == 1) {
context.read(NoteListViewModelProvider).setPin([newNote]);
}
else if(_shouldUpdateHomePage && isPinned == 0){
} else if (_shouldUpdateHomePage && isPinned == 0) {
context.read(NoteListViewModelProvider).unsetPin([newNote]);
}

}
// if both title and content are note empty then discard the note
else if (title.trim() == "" && content.trim() == "") {
Expand Down
11 changes: 6 additions & 5 deletions lib/providers/providers.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:flutter/foundation.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:notes/models/note.dart';
import 'package:notes/models/search_result.dart';
Expand All @@ -19,7 +20,6 @@ final AllNotesProvider = FutureProvider<List<Note>>((ref) async {
});

final PinnedNotesProvider = FutureProvider<List<Note>>((ref) async {

var notes = ref.watch(AllNotesProvider)?.data?.value;
List<Note> pinnednotes = [];
notes?.forEach((e) {
Expand All @@ -28,7 +28,6 @@ final PinnedNotesProvider = FutureProvider<List<Note>>((ref) async {
return pinnednotes;
});


final UnPinnedNotesProvider = FutureProvider<List<Note>>((ref) async {
var notes = ref.watch(AllNotesProvider)?.data?.value;
List<Note> unpinnednotes = [];
Expand All @@ -43,7 +42,7 @@ final UnPinnedNotesProvider = FutureProvider<List<Note>>((ref) async {
final NoteProvider = ChangeNotifierProvider.family<Note, int>((ref, id) {
var notelist = ref.watch(AllNotesProvider)?.data?.value;
ref.onDispose(() {
print('note with id: '+id.toString()+' disposed');
print('note with id: ' + id.toString() + ' disposed');
});
return notelist?.firstWhere((element) => element.id == id);
});
Expand All @@ -59,14 +58,13 @@ final SearchTextProvider = StateProvider<String>((ref) {
return "";
});


// The [SearchResultClassProvider] provides [SearchResult] instance.
// It Takes List of Notes from [AllNotesProvider] and
// The Search Text From [SearchTextProvider]
final SearchResultClassProvider = ChangeNotifierProvider<SearchResult>((ref) {
var notelist = ref.watch(AllNotesProvider)?.data?.value;
String str = ref.watch(SearchTextProvider).state;
return SearchResult(notes: notelist,str: str);
return SearchResult(notes: notelist, str: str);
});

// The [AllSearchResultProvider] provides List of Notes From [SearchResult]
Expand All @@ -86,3 +84,6 @@ final SingleSearchResultProvider =
// .notes_list
// .any((element) => element.id == noteId);
//});

/// isNoteEdited
ValueNotifier<bool> a = ValueNotifier<bool>(false);
24 changes: 24 additions & 0 deletions lib/utils/helper_functions.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:another_flushbar/flushbar.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:notes/utils/constants.dart';

Flushbar emptyNoteDiscardedFlushbar = Flushbar(
Expand All @@ -15,3 +16,26 @@ Flushbar emptyNoteDiscardedFlushbar = Flushbar(
forwardAnimationCurve: Curves.easeInOut,
reverseAnimationCurve: Curves.easeInOut,
);


String getLastEdited(String lastEdited){
DateTime ledited = DateTime.parse(lastEdited);
String result = "";
DateTime now = DateTime.now();
if(ledited.year < now.year){
result = "Edited ${DateFormat().add_d().add_MMM().add_y().format(ledited)}";
}
else if(ledited.month < now.month){
result = "Edited ${DateFormat().add_d().add_MMM().format(ledited)}";
}
else if((now.day - ledited.day) == 1){
result = "Edited Yesterday, ${DateFormat().add_jm().format(ledited)}";
}
else if(ledited.day < now.day){
result = "Edited ${DateFormat().add_d().add_MMM().format(ledited)}";
}
else if(ledited.hour < now.hour || ledited.second < now.second){
result = "Edited ${DateFormat().add_jm().format(ledited)}";
}
return result;
}
32 changes: 32 additions & 0 deletions lib/widgets/last_edited_label.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import 'package:flutter/material.dart';
import 'package:notes/providers/providers.dart';
import 'package:notes/utils/helper_functions.dart';


class LastEditedLabel extends StatefulWidget {
final String last_updated;
LastEditedLabel({this.last_updated});
@override
_LastEditedLabelState createState() => _LastEditedLabelState();
}

class _LastEditedLabelState extends State<LastEditedLabel> {
@override
Widget build(BuildContext context) {
return ValueListenableBuilder(
valueListenable: a,
builder: (context, isEdited, child) {
print(isEdited);
if (!isEdited) {
return Text(
getLastEdited(widget.last_updated),
style: TextStyle(
fontSize: 15.0,
),
);
}
return Container();
},
);
}
}
7 changes: 7 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.14.2"
intl:
dependency: "direct main"
description:
name: intl
url: "https://pub.dartlang.org"
source: hosted
version: "0.17.0"
js:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ dependencies:
sliver_tools: ^0.2.2
another_flushbar: ^1.10.23
share_plus: ^2.1.4
intl: ^0.17.0


# The following adds the Cupertino Icons font to your application.
Expand Down