Skip to content

Commit

Permalink
Made PagesActivity a NotebookListener (finally!)
Browse files Browse the repository at this point in the history
Added Compacter

Page-search is now performed on a separate thread.
  • Loading branch information
aiman-al-masoud committed Jul 31, 2021
1 parent 818665c commit ef93659
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 61 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package com.luxlunaris.noadpadlight.control.classes;


import com.luxlunaris.noadpadlight.control.interfaces.NotebookListener;
import com.luxlunaris.noadpadlight.control.interfaces.PageListener;
import com.luxlunaris.noadpadlight.control.interfaces.Pageable;
import com.luxlunaris.noadpadlight.model.classes.Compacter;
import com.luxlunaris.noadpadlight.model.classes.SinglePage;
import com.luxlunaris.noadpadlight.model.classes.comparators.LastModifiedComparator;
import com.luxlunaris.noadpadlight.model.interfaces.Page;
import com.luxlunaris.noadpadlight.model.services.FileIO;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
Expand Down Expand Up @@ -61,21 +59,12 @@ public class Notebook implements Pageable, PageListener {
*/
private static NotebookListener listener;

/**
* Tmp. keeps track of all of the changes made to
* the Pages list (created, modified, deleted).
*/
private static ProxyNotebookListener proxyNtBkListener;



private Notebook() {
pagesList = new ArrayList<>();
selectedPagesList = new ArrayList<>();
loadPages();
currentPage = 0;
proxyNtBkListener = new ProxyNotebookListener();
this.setListener(proxyNtBkListener);
}

/**
Expand Down Expand Up @@ -215,18 +204,29 @@ public Page[] getNext(int amount) {
* @param query
* @return
*/
public Page[] getByKeywords(String query) {

String[] keywords = query.split("\\s+");
ArrayList<Page> result = new ArrayList<>(pagesList);

for(Page page : pagesList) {
if(!page.contains(keywords)) {
result.remove(page);
}
}
public void getByKeywords(String query) {

return result.toArray(new Page[0]);
Thread t = new Thread() {
@Override
public void run() {

String[] keywords = query.split("\\s+");
ArrayList<Page> result = new ArrayList<>(pagesList);

for (Page page : pagesList) {
if (page.contains(keywords)) {

//as soon as you find a page that fits the keywords tell the
//listener to display it.
listener.onCreated(page);

}
}

}
};

t.start();
}


Expand Down Expand Up @@ -296,23 +296,13 @@ public void setListener(NotebookListener listener){
this.listener = listener;
}

/**
* Get the object that keeps track of changes.
* @return
*/
public ProxyNotebookListener getChanges(){
return proxyNtBkListener;
}


/**
* The next batch of pages to deliver is reset to the initial one.
*/
public void rewind(){
currentPage = 0;
}


/**
* Generate and return a zipped backup file that contains
* all of the pages' contents.
Expand Down Expand Up @@ -344,8 +334,22 @@ public void importPages(String sourcePath){
}


public void compactSelection(){

//create a new blank page
Page page = newPage();

//write the contents of the selected pages onto the blank page
new Compacter().compact(selectedPagesList, page);

//copy due to concurrent modification exception
ArrayList<Page> copy = new ArrayList<>(selectedPagesList);
//delete the old pages
for(int i=0; i<copy.size(); i++){
copy.get(i).delete();
}

}



Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.luxlunaris.noadpadlight.model.classes;

import com.luxlunaris.noadpadlight.model.interfaces.Page;

import java.util.Date;
import java.util.List;

/**
* Combines old pages into a single new one.
*/
public class Compacter {


/**
* Takes a blank Page and a list of pages,
* writes the "aggregate" content of the list of Pages
* onto the blank page.
* @param pages
* @param blankPage
*/
public void compact(List<Page> pages, Page blankPage){

String textBlob = "";
String head = "";
String tail ="--------\n";

for(Page page : pages){

head = new Date(page.getLastModifiedTime()).toString()+"\n";
textBlob+=head;
textBlob+=page.getText();
textBlob+=tail;
}

blankPage.setText(textBlob);
}




}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Lifecycle;

import com.luxlunaris.noadpadlight.R;
import com.luxlunaris.noadpadlight.control.classes.SETTINGS_TAGS;
Expand Down Expand Up @@ -115,5 +116,14 @@ public void onTagUpdated(SETTINGS_TAGS tag) {
}


/**
* True if the activity is currently visible.
* @return
*/
public boolean isInForeground(){
return getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED);
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -140,5 +141,4 @@ public void onTagUpdated(SETTINGS_TAGS tag) {
}



}
Loading

0 comments on commit ef93659

Please sign in to comment.