Skip to content

Commit

Permalink
Some housekeeping.
Browse files Browse the repository at this point in the history
  • Loading branch information
aiman-al-masoud committed Aug 11, 2021
1 parent 2f817f4 commit eb31b46
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,6 @@ private int getLine(int pos){

upTillPos = text.substring(0, pos);


int newLines = upTillPos.split("\n").length;

return newLines;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,56 @@
package com.luxlunaris.noadpadlight.ui;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.content.Intent;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.luxlunaris.noadpadlight.R;

import java.io.File;

/**
*
*/
public class DoodleActivity extends AppCompatActivity {


/**
* Allows drawing on it and converts the bitmap to a suitable image file.
*/
DoodleView doodleView;

/**
* Name of the doodle file extra.
*/
public static final String DOODLE_FILE_EXTRA = "DOODLE_FILE";


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//set title, actionbar theme and layout.
setTitle(R.string.doodle_activity_title);
setTheme(R.style.Theme_AppCompat_DayNight_DarkActionBar);
setContentView(R.layout.activity_doodle);

//create and add a doodle view
ConstraintLayout l = findViewById(R.id.doodle_activity_layout);

doodleView = new DoodleView(this);

l.addView(doodleView);

}


/**
* Inflate the toolbar menu.
* @param menu
* @return
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {

Expand All @@ -44,20 +60,32 @@ public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}

/**
* Handle toolbar menu commands.
* @param item
* @return
*/
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {

switch (item.getItemId()){
case R.id.done_doodling:

//get an image file from the doodleView
File doodleFile = doodleView.getSnapshot();
//make an intent w/ the file as an extra.
Intent resultIntent = new Intent();
resultIntent.putExtra("DOODLE_FILE", doodleFile);
resultIntent.putExtra(DOODLE_FILE_EXTRA, doodleFile);
//set result for caller activity
setResult(RESULT_OK, resultIntent);
//return to caller activity.
finish();
break;

}

return super.onOptionsItemSelected(item);
}


}
46 changes: 32 additions & 14 deletions app/src/main/java/com/luxlunaris/noadpadlight/ui/DoodleView.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,19 @@
import java.io.FileOutputStream;
import java.io.IOException;

/**
*
*/
public class DoodleView extends View {


/**
* Paint is responsible for the styling.
*/
Paint paint;

/**
* Path keeps track of the drawn coordinates.
*/
Path path;


Expand All @@ -33,23 +41,40 @@ public DoodleView(Context context) {
}



/**
* Set the color.
* @param color
*/
public void setColor(int color){
paint.setColor(color);
}

/**
* Set the width of the stroke.
* @param width
*/
public void setStrokeWidth(int width){
paint.setStrokeWidth(width);
}


/**
* On draw redraws the background and the Path.
* @param canvas
*/
@Override
protected void onDraw(Canvas canvas) {
setBackgroundColor(Color.WHITE);
canvas.drawPath(path, paint);
}


/**
* Keeps track of the user's gestures on the screen
* and converts them to "pencil"-strokes.
* @param event
* @return
*/
@Override
public boolean onTouchEvent(MotionEvent event) {

Expand Down Expand Up @@ -77,6 +102,11 @@ public boolean onTouchEvent(MotionEvent event) {
}


/**
* Converts the contents of the view (the doodle)
* to an image file and returns it.
* @return
*/
public File getSnapshot(){

File doodleFile = new File(getContext().getFilesDir(), "doodle.png");
Expand All @@ -97,16 +127,4 @@ public File getSnapshot(){
return doodleFile;
}













}
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package com.luxlunaris.noadpadlight.ui;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
Expand All @@ -17,11 +13,12 @@
import android.widget.SearchView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.luxlunaris.noadpadlight.R;
import com.luxlunaris.noadpadlight.control.classes.SETTINGS_TAGS;
import com.luxlunaris.noadpadlight.control.classes.Settings;
import com.luxlunaris.noadpadlight.model.interfaces.Page;
import com.luxlunaris.noadpadlight.model.services.FileIO;

import java.io.File;

Expand Down Expand Up @@ -58,6 +55,11 @@ public class ReaderActivity extends ColorActivity implements ImportFileFragment.
*/
private boolean HTML_EDIT_MODE = false;

/**
* Code used to request a doodle from DoodleActivity.
*/
private static final int REQUEST_DOODLE = 1;


@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -66,7 +68,6 @@ protected void onCreate(Bundle savedInstanceState) {

setTitle(R.string.reader_activity_title_normal);


//get the text view
textView = findViewById(R.id.reader_text_view);
//set the initial text size
Expand Down Expand Up @@ -107,13 +108,10 @@ private void reloadText(){
* Overwrite the page's text contents.
*/
private void saveToPage(){
//in case the page got deleted because the user exited the app while the page was empty, re-create the page
//page.create();
String edited = getEdited();
page.setText(edited);
}


/**
* Get the html source that is currently being rendered.
* @return
Expand All @@ -128,7 +126,6 @@ private String getEdited(){

}


/**
* Switch between editing html source directly to
* editing "text".
Expand All @@ -146,7 +143,6 @@ private void switchEditMode(){

}


/**
* jump to a position in the text
* @param position
Expand All @@ -161,7 +157,6 @@ private void jumpToPosition(int position){
}
}


/**
* Save the progress when exiting from the activity
*/
Expand All @@ -188,7 +183,6 @@ private void saveProgress(){
Toast.makeText(this, R.string.saved_page_changed_toast, Toast.LENGTH_SHORT).show();
}


/**
* Create the toolbar for this activity
* @param menu
Expand Down Expand Up @@ -260,12 +254,10 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
Toast.makeText(this, currentMode, Toast.LENGTH_LONG).show();
break;
case R.id.make_doodle:

//launch the DoodleActivity requesting a doodle.
Intent intent = new Intent(this, DoodleActivity.class);
startActivityForResult(intent, 1);

startActivityForResult(intent, REQUEST_DOODLE);
break;

case R.id.make_bold:
applyTag("b");
break;
Expand All @@ -275,7 +267,6 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
case R.id.make_italics:
applyTag("i");
break;

case R.id.make_plain:
int currentPos = textView.getSelectionStart();
saveToPage();
Expand All @@ -301,6 +292,17 @@ private void applyTag(String tag){
jumpToPosition(currentPos);
}

/**
* Add an image to the current page at the position
* pointed to by the cursor.
* @param imagePath
*/
private void addImage(String imagePath){
saveToPage();
page.addImage(imagePath, textView.getSelectionStart());
reloadText();
}


/**
* Uses volume keys to navigate up and down between token positions.
Expand Down Expand Up @@ -328,33 +330,32 @@ public boolean onKeyDown(int keyCode, KeyEvent event) {
@Override
public void onFileObtained(File file) {
Toast.makeText(this, "image imported!", Toast.LENGTH_SHORT).show();
saveToPage();
page.addImage(file.getPath(), textView.getSelectionStart());
reloadText();
addImage(file.getPath());
}

/**
* Receive requested results from called activities.
* @param requestCode
* @param resultCode
* @param data
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

//halt if result code is not "ok"
if(resultCode != Activity.RESULT_OK){
return;
}

//halt if "data" is null
if(data==null){
//halt if result code is not "ok", or if data is null
if(resultCode != Activity.RESULT_OK || data==null){
return;
}

switch (requestCode){

case 1:
Uri uri = data.getData();
File doodleFile = (File)data.getSerializableExtra("DOODLE_FILE");
saveToPage();
page.addImage(doodleFile.getPath(), textView.getSelectionStart());
reloadText();
//in case a doodle was requested from DoodleActivity
case REQUEST_DOODLE:
//get the doodle file extra
File doodleFile = (File)data.getSerializableExtra(DoodleActivity.DOODLE_FILE_EXTRA);
//put this image file in the page
addImage(doodleFile.getPath());
break;

}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/menu/doodle_activity_toolbar.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<item
android:id="@+id/done_doodling"
android:icon="@android:drawable/checkbox_on_background"
android:title="done"
android:title="@string/done"
app:showAsAction="always" />


Expand Down
Loading

0 comments on commit eb31b46

Please sign in to comment.