Skip to content

Commit

Permalink
fixed some bugs, created some more.
Browse files Browse the repository at this point in the history
  • Loading branch information
aiman-al-masoud committed Jul 9, 2021
1 parent d4e4c9b commit 934fe2a
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public class Notebook implements Pageable, PageListener {




private Notebook() {
pagesList = new ArrayList<Page>();
selectedPagesList = new ArrayList<Page>();
Expand Down Expand Up @@ -130,6 +131,7 @@ public void onDeleted(Page page) {
selectedPagesList.remove(page);
}
pagesList.remove(page);

}

@Override
Expand All @@ -145,7 +147,7 @@ public void onModified(Page page) {
@Override
public Page[] getNext(int amount) {

amount = Math.min(amount, pagesList.size());
amount = Math.min(amount, pagesList.size() -currentPage );

try{
List<Page> result = pagesList.subList(currentPage, currentPage+amount);
Expand Down Expand Up @@ -232,4 +234,6 @@ public void unselectAll(){





}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;


public class SinglePage extends File implements Page {
Expand All @@ -26,6 +27,8 @@ public class SinglePage extends File implements Page {
boolean selected = false;




public SinglePage(String pathname) {
super(pathname);
metadata = new MetadataFile(getPath()+File.separator+"metadata");
Expand All @@ -35,7 +38,8 @@ public SinglePage(String pathname) {

@Override
public String getText() {
return FileIO.read(textFile.getPath());
String text = FileIO.read(textFile.getPath());
return text ==null? "" : text;
}

@Override
Expand Down Expand Up @@ -75,6 +79,12 @@ public void create() {
}
}

@Override
public int numOfTokens(String token) {
return getText().split(token).length-1;
}




@Override
Expand Down Expand Up @@ -161,11 +171,13 @@ public int previousPosition(String token){

@Override
public int nextPosition() {

return nextPosition(currentToken);
}

@Override
public int previousPosition() {

return previousPosition(currentToken);
}

Expand All @@ -187,11 +199,7 @@ public void addListener(PageListener listener) {

@Override
public String getPreview() {
try{
return getText().substring(0, Math.min(10, getText().length()))+"...";
}catch (NullPointerException e){
}
return "...";
return FileIO.readLine(textFile.getPath())+"...\n" +new Date(getLastModifiedTime()).toString();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ public interface Page extends Serializable {
public void create();


/**
* get the number of tokens
* @param token
* @return
*/
public int numOfTokens(String token);


/**
* Get the next position of a token
* @param token
Expand Down Expand Up @@ -63,8 +71,6 @@ public interface Page extends Serializable {
public boolean contains(String[] keywords);




public boolean isSelected();
public void setSelected(boolean select);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;

public class FileIO {

Expand Down Expand Up @@ -69,6 +71,27 @@ public static synchronized void append(String filePath, String text) {
}

}


/**
* Reads a single line from a file.
* @param filepath
* @return
*/
public static synchronized String readLine(String filepath){
try {
RandomAccessFile raf = new RandomAccessFile(filepath, "r");
String buf = raf.readLine();
raf.close();
return buf;
} catch (IOException e) {
e.printStackTrace();
}
return "";
}





}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public class MainActivity extends AppCompatActivity {
Button goToPagesButton;


/**
* If true, onCreate was called at least once
*/
boolean appStartedFlag;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -49,7 +54,9 @@ protected void onCreate(Bundle savedInstanceState) {

//jump to a blank page
//TODO: replace hardcoded true with persistent setting
if(true){
if(true && !appStartedFlag){
//the launch phase is over
appStartedFlag = true;
Intent intent = new Intent(this, ReaderActivity.class);
intent.putExtra("PAGE", Notebook.getInstance().newPage());
startActivity(intent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.luxlunaris.noadpadlight.R;
import com.luxlunaris.noadpadlight.control.classes.Notebook;
import com.luxlunaris.noadpadlight.control.interfaces.NotebookListener;
import com.luxlunaris.noadpadlight.model.interfaces.Page;

import java.util.ArrayList;
Expand Down Expand Up @@ -52,6 +53,8 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pages);



//get the lin layout that will hold the fragments
pagesLinLayout = findViewById(R.id.pages_linear_layout);

Expand All @@ -64,6 +67,8 @@ protected void onCreate(Bundle savedInstanceState) {
//defines what the activity does when scrolling occurs
setOnScrollAction();



}


Expand All @@ -79,7 +84,14 @@ public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int

//if can't scroll vertically anymore: bottom reached
if(!v.canScrollVertically(1)){
loadNextPagesBlock();

try{
loadNextPagesBlock();
}catch (Exception e){

}


}

}
Expand Down Expand Up @@ -113,6 +125,7 @@ private void addPage(Page page){
PageFragment pgFrag = getFragment(page);
getSupportFragmentManager().beginTransaction().add(pagesLinLayout.getId(),pgFrag,"").commit();
pageFragments.add(pgFrag);

}


Expand Down Expand Up @@ -144,12 +157,21 @@ private void removeAllPages(){
pageFragments.clear();
}


/**
* Removes a fragment without deleting its page
* @param page
*/
private void removeFragment(Page page ){
getSupportFragmentManager().beginTransaction().remove(getFragment(page)).commit();
}

/**
* Deletes a page and its corresponding fragment
* @param page
*/
private void deletePage(Page page){
getSupportFragmentManager().beginTransaction().remove(getFragment(page)).commit();
removeFragment(page);
page.delete();
}

Expand Down Expand Up @@ -250,9 +272,4 @@ public void onBackPressed() {








}
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,19 @@ public class ReaderActivity extends AppCompatActivity {
String textSizeString = Settings.get().getTagValue(Settings.TAGS.TEXT_SIZE.toString());
int TEXT_SIZE = textSizeString==null? 18 : Integer.parseInt(textSizeString.trim());


/**
*This instance
*/
ReaderActivity THIS;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reader);

//reference to this ReaderActivity instance
THIS = this;
//get the text view
textView = findViewById(R.id.reader_text_view);
//set the initial text size
Expand Down Expand Up @@ -123,7 +128,8 @@ public boolean onCreateOptionsMenu(Menu menu) {
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
jumpToPosition(page.nextPosition( query));
jumpToPosition(page.nextPosition(query));
Toast.makeText(THIS, "found "+page.numOfTokens(query)+" occrrs. vol. up/down to nav.", Toast.LENGTH_LONG).show();
return true;
}

Expand Down Expand Up @@ -174,10 +180,10 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode){
case 25: //volume up pressed: back
case 25: //volume down pressed: forth
jumpToPosition(page.nextPosition());
return true; //makes sure volume toast doesn't get displayed
case 24: //volume down pressed: forth
case 24: //volume up pressed: back
jumpToPosition(page.previousPosition());
return true;
}
Expand Down

0 comments on commit 934fe2a

Please sign in to comment.