-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from mrunix00/new-note-screen
Add support for creating new notes
- Loading branch information
Showing
24 changed files
with
650 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
extensions: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import 'package:bloc/bloc.dart'; | ||
import 'package:equatable/equatable.dart'; | ||
import 'package:multiple_result/multiple_result.dart'; | ||
|
||
import '../../data/interfaces/notes_repository.dart'; | ||
import '../../models/failure.dart'; | ||
import '../../models/note.dart'; | ||
|
||
part 'fetch_notes_event.dart'; | ||
part 'fetch_notes_state.dart'; | ||
|
||
class FetchNotesBloc extends Bloc<FetchNotesEvent, FetchNotesState> { | ||
final NotesRepository repository; | ||
FetchNotesBloc({ | ||
required this.repository, | ||
}) : super(FetchNotesInitial()) { | ||
on<FetchNotes>((event, emit) async { | ||
emit(FetchNotesInProgress()); | ||
final result = await repository.getNotes(); | ||
if (result.isSuccess()) { | ||
emit(FetchNotesSuccess((result as Success).success)); | ||
} else { | ||
emit(FetchNotesFailure((result as Error).error)); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
part of 'fetch_notes_bloc.dart'; | ||
|
||
sealed class FetchNotesEvent extends Equatable { | ||
const FetchNotesEvent(); | ||
|
||
@override | ||
List<Object> get props => []; | ||
} | ||
|
||
final class FetchNotes extends FetchNotesEvent {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
part of 'fetch_notes_bloc.dart'; | ||
|
||
sealed class FetchNotesState extends Equatable { | ||
const FetchNotesState(); | ||
|
||
@override | ||
List<Object> get props => []; | ||
} | ||
|
||
final class FetchNotesInitial extends FetchNotesState {} | ||
|
||
final class FetchNotesInProgress extends FetchNotesState {} | ||
|
||
final class FetchNotesSuccess extends FetchNotesState { | ||
final List<Note> notes; | ||
|
||
const FetchNotesSuccess(this.notes); | ||
|
||
@override | ||
List<Object> get props => [notes]; | ||
} | ||
|
||
final class FetchNotesFailure extends FetchNotesState { | ||
final Failure failure; | ||
|
||
const FetchNotesFailure(this.failure); | ||
|
||
@override | ||
List<Object> get props => [failure]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import 'package:bloc/bloc.dart'; | ||
import 'package:equatable/equatable.dart'; | ||
|
||
import '../../data/interfaces/notes_repository.dart'; | ||
import '../../models/failure.dart'; | ||
import '../../models/note.dart'; | ||
|
||
part 'save_new_note_event.dart'; | ||
part 'save_new_note_state.dart'; | ||
|
||
class SaveNewNoteBloc extends Bloc<SaveNewNoteEvent, SaveNewNoteState> { | ||
final NotesRepository repository; | ||
SaveNewNoteBloc({required this.repository}) : super(SaveNewNoteInitial()) { | ||
on<SaveNewNote>((event, emit) async { | ||
emit(SaveNewNoteInProgress()); | ||
final result = await repository.addNote(event.note); | ||
emit(result == null ? SaveNewNoteSuccess() : SaveNewNoteFailure(result)); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
part of 'save_new_note_bloc.dart'; | ||
|
||
sealed class SaveNewNoteEvent extends Equatable { | ||
const SaveNewNoteEvent(); | ||
|
||
@override | ||
List<Object> get props => []; | ||
} | ||
|
||
class SaveNewNote extends SaveNewNoteEvent { | ||
final Note note; | ||
|
||
const SaveNewNote({required this.note}); | ||
|
||
@override | ||
List<Object> get props => [note]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
part of 'save_new_note_bloc.dart'; | ||
|
||
sealed class SaveNewNoteState extends Equatable { | ||
const SaveNewNoteState(); | ||
|
||
@override | ||
List<Object> get props => []; | ||
} | ||
|
||
final class SaveNewNoteInitial extends SaveNewNoteState {} | ||
|
||
final class SaveNewNoteInProgress extends SaveNewNoteState {} | ||
|
||
final class SaveNewNoteSuccess extends SaveNewNoteState {} | ||
|
||
final class SaveNewNoteFailure extends SaveNewNoteState { | ||
final Failure failure; | ||
|
||
const SaveNewNoteFailure(this.failure); | ||
|
||
@override | ||
List<Object> get props => [failure]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import 'package:get_it/get_it.dart'; | ||
import 'package:noteapp/bloc/fetch_notes/fetch_notes_bloc.dart'; | ||
|
||
import '../bloc/save_new_note/save_new_note_bloc.dart'; | ||
import '../data/implementation/local_notes_repository.dart'; | ||
import '../data/interfaces/notes_repository.dart'; | ||
|
||
void setupLocator() { | ||
final getIt = GetIt.I; | ||
|
||
getIt.registerLazySingleton<NotesRepository>( | ||
() => LocalNotesRepository(), | ||
); | ||
|
||
getIt.registerFactory( | ||
() => SaveNewNoteBloc(repository: getIt()), | ||
); | ||
|
||
getIt.registerFactory( | ||
() => FetchNotesBloc(repository: getIt()), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import 'package:multiple_result/multiple_result.dart'; | ||
import 'package:noteapp/models/failure.dart'; | ||
|
||
import 'package:noteapp/models/note.dart'; | ||
import 'package:sqflite/sqflite.dart'; | ||
|
||
import '../interfaces/notes_repository.dart'; | ||
|
||
class LocalNotesRepository implements NotesRepository { | ||
static const _name = "NotesDatabase.db"; | ||
static const tableName = 'notes'; | ||
static const _version = 1; | ||
|
||
Database? _database; | ||
|
||
initDatabase() async { | ||
if (_database != null) return; | ||
_database = await openDatabase(_name, version: _version, | ||
onCreate: (Database db, int version) async { | ||
await db.execute( | ||
'''CREATE TABLE $tableName ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
title TEXT, | ||
content TEXT | ||
)''', | ||
); | ||
}); | ||
} | ||
|
||
@override | ||
Future<Failure?> addNote(Note note) async { | ||
await initDatabase(); | ||
await _database!.insert( | ||
tableName, | ||
note.toMap(), | ||
conflictAlgorithm: ConflictAlgorithm.replace, | ||
); | ||
return null; | ||
} | ||
|
||
@override | ||
Future<Failure?> deleteNote(Note note) async { | ||
await _database!.delete( | ||
tableName, | ||
where: 'id = ?', | ||
whereArgs: [note.id], | ||
); | ||
return null; | ||
} | ||
|
||
@override | ||
Future<Result<List<Note>, Failure>> getNotes() async { | ||
await initDatabase(); | ||
final query = await _database!.query(tableName); | ||
final notes = query.map((note) => Note.fromMap(note)).toList(); | ||
return Success(notes); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import 'package:multiple_result/multiple_result.dart'; | ||
|
||
import '../../models/note.dart'; | ||
import '../../models/failure.dart'; | ||
|
||
abstract interface class NotesRepository { | ||
Future<Result<List<Note>, Failure>> getNotes(); | ||
Future<Failure?> addNote(Note note); | ||
Future<Failure?> deleteNote(Note note); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class Failure { | ||
final String message; | ||
|
||
Failure(this.message); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
class Note { | ||
final int? id; | ||
final String title; | ||
final String content; | ||
|
||
Note({ | ||
this.id, | ||
required this.title, | ||
required this.content, | ||
}); | ||
|
||
Note copyWith({ | ||
int? id, | ||
String? title, | ||
String? content, | ||
}) { | ||
return Note( | ||
id: id ?? this.id, | ||
title: title ?? this.title, | ||
content: content ?? this.content, | ||
); | ||
} | ||
|
||
Map<String, dynamic> toMap() { | ||
Map<String, dynamic> map = <String, dynamic>{}; | ||
if (id != null) map['id'] = id; | ||
map['title'] = title; | ||
map['content'] = content; | ||
return map; | ||
} | ||
|
||
static Note fromMap(Map<String, dynamic> map) { | ||
return Note( | ||
id: map['id'] as int, | ||
title: map['title'] as String, | ||
content: map['content'] as String, | ||
); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.