From 3a59bf9a6adbeef1b03b1fd5d7fdb74c600d2e6e Mon Sep 17 00:00:00 2001 From: Mo Bitar Date: Sat, 9 Sep 2017 15:45:06 -0500 Subject: [PATCH] Abstract controller --- src/containers/NoteList.js | 7 +-- src/lib/modelManager.js | 4 +- src/models/api/item.js | 1 + src/screens/Abstract.js | 31 +++++++++++++ src/screens/Notes.js | 90 ++++++++++++++++++++++++++++---------- 5 files changed, 106 insertions(+), 27 deletions(-) create mode 100644 src/screens/Abstract.js diff --git a/src/containers/NoteList.js b/src/containers/NoteList.js index a7db9421..31ab9ed5 100644 --- a/src/containers/NoteList.js +++ b/src/containers/NoteList.js @@ -10,9 +10,9 @@ export default class NoteList extends Component { return ( @@ -29,6 +29,7 @@ export default class NoteList extends Component { text={item.text} tags={item.tags} pinned={item.pinned} + deleted={item.deleted} /> ) diff --git a/src/lib/modelManager.js b/src/lib/modelManager.js index 76f08e0d..7eba72df 100644 --- a/src/lib/modelManager.js +++ b/src/lib/modelManager.js @@ -317,7 +317,7 @@ export default class ModelManager { getNotes(options = {}) { var notes; if(options.selectedTags && options.selectedTags.length > 0) { - var tags = ModelManager.getInstance().getItemsWithIds(this.selectedTags); + var tags = ModelManager.getInstance().getItemsWithIds(options.selectedTags); if(tags.length > 0) { var taggedNotes = new Set(); for(var tag of tags) { @@ -346,7 +346,7 @@ export default class ModelManager { } else { return !note.archived; } - }.bind(this)) + }) notes = notes.sort(function(a, b){ if(a.pinned) { return -1; } diff --git a/src/models/api/item.js b/src/models/api/item.js index f0216738..d8e96cff 100644 --- a/src/models/api/item.js +++ b/src/models/api/item.js @@ -7,6 +7,7 @@ let AppDomain = "org.standardnotes.sn"; export default class Item { constructor(json_obj) { + this.appData = {}; this.updateFromJSON(json_obj); this.observers = []; } diff --git a/src/screens/Abstract.js b/src/screens/Abstract.js new file mode 100644 index 00000000..ae1a4ce0 --- /dev/null +++ b/src/screens/Abstract.js @@ -0,0 +1,31 @@ +import React, { Component } from 'react'; + +export default class Abstract extends Component { + + constructor(props) { + super(props); + this.props.navigator.setOnNavigatorEvent(this.onNavigatorEvent.bind(this)); + } + + configureNavBar() { + + } + + onNavigatorEvent(event) { + + switch(event.id) { + case 'willAppear': + this.configureNavBar(); + break; + case 'didAppear': + this.visible = true; + break; + case 'willDisappear': + break; + case 'didDisappear': + this.visible = false; + break; + } + } + +} diff --git a/src/screens/Notes.js b/src/screens/Notes.js index 3c43fb49..1aca39a0 100644 --- a/src/screens/Notes.js +++ b/src/screens/Notes.js @@ -8,11 +8,12 @@ import GlobalStyles from "../Styles" import Keychain from "../lib/keychain" import {iconsMap, iconsLoaded} from '../Icons'; import NoteList from "../containers/NoteList" +import Abstract from "./Abstract" -export default class Notes extends Component { +export default class Notes extends Abstract { defaultOptions() { - return {selectedTags: [], sortBy: "created_at"}; + return {selectedTags: [], sortBy: "created_at", notes: ModelManager.getInstance().notes}; } constructor(props) { @@ -47,8 +48,6 @@ export default class Notes extends Component { this.options = this.defaultOptions(); this.loadNotes(); }.bind(this)); - - this.props.navigator.setOnNavigatorEvent(this.onNavigatorEvent.bind(this)); } loadTabbarIcons() { @@ -82,6 +81,8 @@ export default class Notes extends Component { } configureNavBar() { + super.configureNavBar(); + var notesTitle = "Notes"; var filterTitle = "Filter"; var numFilters = this.options.selectedTags.length; @@ -122,23 +123,15 @@ export default class Notes extends Component { onNavigatorEvent(event) { + super.onNavigatorEvent(event); + switch(event.id) { case 'willAppear': - this.activeScreen = true; - this.forceUpdate(); - this.configureNavBar(); - if(this.needsLoadNotes) { - this.needsLoadNotes = false; + if(this.loadNotesOnVisible) { + this.loadNotesOnVisible = false; this.loadNotes(); } break; - case 'didAppear': - break; - case 'willDisappear': - this.activeScreen = false; - break; - case 'didDisappear': - break; } if (event.type == 'NavBarButtonPress') { @@ -146,7 +139,7 @@ export default class Notes extends Component { this.presentNewComposer(); } else if (event.id == 'sideMenu') { - this.presentNewComposer(); + this.presentFilterScreen(); } } } @@ -158,7 +151,7 @@ export default class Notes extends Component { }); } - this.presentFilterScreen() { + presentFilterScreen() { this.props.navigator.showModal({ screen: 'sn.Filter', title: 'Options', @@ -174,14 +167,59 @@ export default class Notes extends Component { loadNotes = (reloadNavBar = true) => { - if(!this.activeScreen) { - this.needsLoadNotes = true; + if(!this.visible) { + this.loadNotesOnVisible = true; return; } console.log("===Load Notes==="); - this.notes = ModelManager.getInstance().getNotes(this.options); + var notes; + if(this.options.selectedTags && this.options.selectedTags.length > 0) { + var tags = ModelManager.getInstance().getItemsWithIds(this.options.selectedTags); + if(tags.length > 0) { + var taggedNotes = new Set(); + for(var tag of tags) { + taggedNotes = new Set([...taggedNotes, ...new Set(tag.notes)]) + } + notes = Array.from(taggedNotes); + } + } + + if(!notes) { + notes = ModelManager.getInstance().notes; + } + + var searchTerm = this.options.searchTerm; + if(searchTerm) { + notes = notes.filter(function(note){ + return note.safeTitle().includes(searchTerm) || note.safeText().includes(searchTerm); + }) + } + + var sortBy = this.options.sortBy; + + notes = notes.filter(function(note){ + if(this.options.archivedOnly) { + return note.archived; + } else { + return !note.archived; + } + }.bind(this)) + + this.notes = notes.sort(function(a, b){ + if(a.pinned) { return -1; } + if(b.pinned) { return 1; } + + let vector = sortBy == "title" ? -1 : 1; + var aValue = a[sortBy] || ""; + var bValue = b[sortBy] || ""; + if(aValue > bValue) { return -1 * vector;} + else if(aValue < bValue) { return 1 * vector;} + return 0; + }) + + this.reloadList(); this.reloadList(); // this function may be triggled asyncrounsly even when on a different screen @@ -233,7 +271,15 @@ export default class Notes extends Component { Decrypting notes... {this.notes && - + } );