Skip to content

Commit

Permalink
Mixing notes and bugfixes
Browse files Browse the repository at this point in the history
The application now mixes notes between local and remote list of notes.
Also it has been solved various bugs when using File API and Events API

Add: Mix of local/remotes notes comparing both list of notes
* New local note not present in remote list -> Send webinos event with
the new note
* New remote note not present in local list -> Save it both in memory
and file (webinos File API)
* Existing updated note:
* Updated only in local -> Send webinos event with the updated note
* Updated only in remote -> Save it both in memory and file (webinos
File API)
* Updated in both sides -> Make a note with all the content of the local
and remote note. Send event and save it in local.
Add: Ask other PZP for an updated notes list with "cn-getNotes" event.
Add: otherPZP variable updated when searching File API services to be
used in the future. It won't have sense when fixing
webinos.session.getOtherPZP()
Fix: Loading foreign file API service. Now it loads only the File API
from the own device
Fix: File API in webinos 0.8.0 for Android is not working, so it
retrieves the note of list from other connected PZP with cn-getNotes
event.
  • Loading branch information
IvanAlegre committed Apr 18, 2013
1 parent 987c83e commit 9171555
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 15 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
## Creative Notes
=================
**NOTE: This Creative Notes version works on Webinos platform until 0.8.0**

### Installing

Expand Down
1 change: 1 addition & 0 deletions editNote.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script src="js/beforeJQM.js"></script>
<script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
<script src="js/sugar-1.3.9-custom.min.js"></script>
<!-- Weinre loading for testing purpouses:-->
<!--<script src="http://192.168.43.238:8009/target/target-script-min.js"></script>-->
<!---->
Expand Down
Binary file added favicon.ico
Binary file not shown.
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script src="js/beforeJQM.js"></script>
<script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
<script src="js/sugar-1.3.9-custom.min.js"></script>
<!-- Weinre loading for testing purpouses:-->
<!--<script src="http://192.168.43.238:8009/target/target-script-min.js"></script>-->
<!---->
Expand Down
61 changes: 61 additions & 0 deletions js/dataManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ function dataManager(){
return noteIndex;
}
}

// Ends iteration, not found
return -1;
}

this.addNote = function (note, notesList){
Expand All @@ -90,6 +93,64 @@ function dataManager(){
fileMgr.writeNotesList(notesList);
}

/***
Use SugarJS to work with sets
***/
this.mixNotesList = function(localNotesList, remoteNotesList){
var notesList1 = localNotesList;
var notesList2 = remoteNotesList;

// Function that mixes two notes with the same id and changes in both.
var mixNotes = function(localNote, remoteNote){
var note = localNote;
var note2 = remoteNote;
var onlyLocalContent = note.content.subtract(note2.content); // [] if only remote changes
var onlyRemoteContent = note2.content.subtract(note.content); // [] if only local changes
if (onlyLocalContent.length == 0){
// The note has not been updated locally, only remotely
note.content = note2.content;
console.log("Note modified in remote, update only in local");
dataMgr.addNote(note, notesList);
} else if (onlyRemoteContent.length == 0){
// The note only has local updates
console.log("Note modified in local, send webinos event");
webinosMgr.sendNoteEvent(note);
} else {
console.log("Note modified in both sides, send webinos event");
note.content = note.content.union(note2.content);
webinosMgr.sendNoteEvent(note);
}
console.log(note);
console.log(note2);
}

var modifiedLocalNotes = notesList1.subtract(notesList2);
var modifiedRemoteNotes = notesList2.subtract(notesList1);

for (noteI in modifiedLocalNotes){
var note = modifiedLocalNotes[noteI];
if (dataMgr.getNoteById(note.id, modifiedRemoteNotes) == -1){
// Inexistent note in remote. Brand new local note
console.log("New local note, send webinos event");
webinosMgr.sendNoteEvent(note);
} else {
// Modified in both sides
console.log("Updated note: Mix local and remote notes");
var note2 = dataMgr.getNoteById(note.id, notesList2);
mixNotes(note, note2);
}
}

for (noteI in modifiedRemoteNotes){
var note = modifiedRemoteNotes[noteI];
if (dataMgr.getNoteById(note.id, modifiedLocalNotes) == -1){
// Inexistent note in local. Brand new remote note
console.log("New remote note, add it locally");
dataMgr.addNote(note, notesList);
}
}
}

/* DATA MODEL OF NOTESLIST
[
{
Expand Down
11 changes: 10 additions & 1 deletion js/fileManager.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
function fileManager(successCB, errorCB){
var fileService;
var fileSystem;
var otherFileServices = [];

var notesListFileName = "CreativeNotes.json";

Expand All @@ -11,9 +12,17 @@ function fileManager(successCB, errorCB){
function errorWithCB(log){
console.log(log);
errorCB(log);
}
}

function onServiceFound(ser){
// Test if it's fileAPI from another PZP
if (ser.serviceAddress != webinos.session.getPZPId()){
// Add it to otherPZP list, to otherFileServices and exit (we do not bind at the moment)
otherPZP.push(ser.serviceAddress);
otherFileServices.push(ser);
return;
}

fileService = ser;
ser.bindService({
onBind: function(){
Expand Down
34 changes: 23 additions & 11 deletions js/graphicalManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ var defaultUntitledTitle = "Untitled Note";
var noteWidth = "95%";
// Show a spinning wheel while testing everything

var defaultGroup = "WebinosTelefonica"
var defaultGroup = "WebinosTelefonica";

var pzhName;
var otherPZP = []; // Filled when searching fileAPI services (fileManager.js)

var noteIdLength = 32;

Expand Down Expand Up @@ -98,7 +99,7 @@ function saveNote(){
currentNote.content = currentNote.content.concat(noteData);

// Just for backup
serverMgr.sendNote(currentNote);
// serverMgr.sendNote(currentNote);

// If new note
if (dataMgr.getId(currentNote) == -1){
Expand Down Expand Up @@ -241,20 +242,21 @@ function initIndex(){
if (!notesList){
// First, try to get notes from the local file. If something bad happens, download/ask

// If fileAPI is not ready, there is no file, or it's corrupted
// If fileAPI is not ready (or not supported), there is no file, or it's corrupted
var errorCB = function(error){
// Override InvalidStateError
if (!error || error.name != "InvalidStateError"){
// Get a list of notes from other PZP through webinos
webinosMgr.getNotesFromOtherPZP(
function(notes){
// Get a list of notes from other PZP through webinos
webinosMgr.getNotesFromOtherPZP(
function(notes){
// This function can be called more than one time (various responses)
// Test if notesList has been already loaded
if (!notesList){
console.log("NOTES RETRIEVED FROM OTHER PZP");
notesList = notes;
fileMgr.writeNotesList(notesList);
updateNotesList();
}
)
}
}
)
}

// If fileAPI is ready
Expand All @@ -264,10 +266,20 @@ function initIndex(){
var gotNotesList = function(nl){
notesList = nl;
updateNotesList();

// Now retrieve remote NotesList, and look for changes
var gotRemoteNL = false;
webinosMgr.getNotesFromOtherPZP(
function(remoteNotesList){
if (!gotRemoteNL){
gotRemoteNL = true;
dataMgr.mixNotesList(notesList, remoteNotesList);
}
}
);
}

// If there is no file to read, same actions as fileAPI not ready.

fileMgr.readNotesList(gotNotesList, errorCB);

}
Expand Down
Loading

0 comments on commit 9171555

Please sign in to comment.