Skip to content

Commit

Permalink
Minor refactors and renames. Most important was the fixing of 'Quick …
Browse files Browse the repository at this point in the history
…note...' functionality. It seemed the shifting of request code values caused 'EditorInfo.IME_ACTION_DONE' to become redundant, which broke this feature for a while. Debugging revealed that the new int value for the 'Done' action is always 6. Thus, a new constant was created, along with an empty field case
  • Loading branch information
Tom-A-Watson committed Jan 26, 2025
1 parent 31c19c9 commit 223d3a7
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 102 deletions.
70 changes: 26 additions & 44 deletions app/src/main/java/com/example/ultranote/activities/CreateNote.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
package com.example.ultranote.activities;

import static utilities.Utilities.COLLAPSED;
import static utilities.Utilities.EXPANDED;
import static utilities.Utilities.GONE;
import static utilities.Utilities.GRANTED;
import static utilities.Utilities.LONG;
import static utilities.Utilities.VISIBLE;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.annotation.SuppressLint;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.content.Context;
import database.NotesDatabase;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.graphics.drawable.Drawable;
import java.io.InputStream;
import androidx.appcompat.app.AppCompatActivity;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
Expand All @@ -28,7 +31,6 @@
import android.widget.Toast;
import com.example.ultranote.R;
import com.google.android.material.bottomsheet.BottomSheetBehavior;

import entities.Note;
import settings.UserSettings;
import utilities.Utilities;
Expand All @@ -46,24 +48,15 @@ public class CreateNote extends AppCompatActivity implements View.OnClickListene
private ImageView[] colours;
private Context appContext;
private Drawable[] darkBGColours, lightBGColours;
private InputStream inputStream;
private Bitmap bitmap;
private BottomSheetBehavior<LinearLayout> o;
private LinearLayout noteURL, noteOptions, deleteBtn;
private CoordinatorLayout createNoteBG;
private String selectedImagePath, type;
public static String selectedImagePath, type;

// Request codes
private static final int STORAGE_PERM = 1;
private static final int SELECT_IMAGE = 2;

// States
private static final int EXPANDED = BottomSheetBehavior.STATE_EXPANDED;
private static final int COLLAPSED = BottomSheetBehavior.STATE_COLLAPSED;
private static final int GONE = View.GONE;
private static final int VISIBLE = View.VISIBLE;
private static final int GRANTED = PackageManager.PERMISSION_GRANTED;


@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -111,7 +104,7 @@ private void initComponents() {
clearSBtn = findViewById(R.id.removeSubtitle);
clearCBtn = findViewById(R.id.removeContent);
noteOptions = findViewById(R.id.noteOptionsLayout);
noteOptionsText = findViewById(R.id.noteOptionsText);
noteOptionsText = findViewById(R.id.noteOptions);
colourPickerText = findViewById(R.id.colourPickerText);
o = BottomSheetBehavior.from(noteOptions);
urlV = LayoutInflater.from(this).inflate(R.layout.addurl, findViewById(R.id.urlLayout));
Expand All @@ -128,18 +121,18 @@ private void initComponents() {
private void updateView() {
dateTime.setText(Utilities.singleLine);

if (settings.theme().equals(UserSettings.LIGHT_THEME)) {// Set components to light mode colours
//-|Component Colours|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
if (u.appThemeIsLightTheme(settings)) { // Set components to light mode colours \\
//-|Component Colours|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ||
createNoteBG.setBackgroundColor(Utilities.white);
//-|Text Colours|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -//
//-|Text Colours|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -||
title.setTextColor(Utilities.darkGrey); createNoteText.setTextColor(Utilities.black);
subtitle.setTextColor(Utilities.darkGrey); noteOptionsText.setTextColor(Utilities.black);
content.setTextColor(Utilities.black); colourPickerText.setTextColor(Utilities.darkGrey);
dateTime.setTextColor(Utilities.black); url.setLinkTextColor(Utilities.lightModeAccent);
//-|Icon Colours|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -//
//-|Icon Colours|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -||
backBtn.setColorFilter(Utilities.black); addURL.setColorFilter(Utilities.lightGrey);
addImg.setColorFilter(Utilities.lightGrey);
//-|Component Backgrounds|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
//-|Component Backgrounds|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ||
saveBtn.setBackground(Utilities.saveBtnLight); sIndicator.setBackground(Utilities.blue);
noteOptions.setBackground(Utilities.light);

Expand Down Expand Up @@ -180,12 +173,12 @@ private void loadPotentialNoteData() {
if (type != null) {
switch (type) {
case "title": title.setText((getIntent().getStringExtra("quickTitle"))); break;
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
//- = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = //
case "image": selectedImagePath = getIntent().getStringExtra("imagePath");
image.setImageBitmap(BitmapFactory.decodeFile(selectedImagePath));
image.setVisibility(VISIBLE);
findViewById(R.id.deleteImage).setVisibility(VISIBLE); break;
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
//- = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = //
case "URL": url.setText(getIntent().getStringExtra("URL"));
noteURL.setVisibility(VISIBLE); break;
}
Expand Down Expand Up @@ -238,7 +231,7 @@ private void initNoteOptions() {

u.initColourPickerFor(existingNote, this);
u.setNoteColourToSelected(this);
findViewById(R.id.noteOptionsText).setOnClickListener(this);
findViewById(R.id.noteOptions).setOnClickListener(this);
findViewById(R.id.colourIndicator).setOnClickListener(this);
}

Expand Down Expand Up @@ -283,22 +276,12 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten
Uri uri = data.getData();

if (requestCode == SELECT_IMAGE && resultCode == RESULT_OK) {
if (uri != null) {
try {
inputStream = getContentResolver().openInputStream(uri);
bitmap = BitmapFactory.decodeStream(inputStream);
image.setImageBitmap(bitmap); image.setVisibility(VISIBLE);
selectedImagePath = u.getPath(uri);
findViewById(R.id.deleteImage).setVisibility(VISIBLE);
} catch (Exception e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
if (uri != null) { u.attemptEmbedImage(this, this, uri, image); }
}
}

/**
* TextWatcher implementations. Only 'onTextChanged()' is required, for empty field checks :)
/** TextWatcher Implementations
* Only 'onTextChanged()' is required, for empty field checks :)
*/
@Override
public void onTextChanged(CharSequence cs, int i, int i1, int i2) {
Expand All @@ -322,15 +305,14 @@ public void onClick(View view) {
case R.id.removeContent: content.getText().clear(); break;
case R.id.addURL: Utilities.urlDialog = Utilities.showDialog(this, this, this, urlV); break;
case R.id.cancelAddURL: Utilities.urlDialog.dismiss(); break;
case R.id.confirmAddURL: Utilities.validateURL(this, input, Utilities.urlDialog); break;
case R.id.confirmAddURL: Utilities.validateURL(this, input); break;
case R.id.deleteURL: url.setText(""); input.setText(""); noteURL.setVisibility(GONE); break;
case R.id.colourIndicator: case R.id.noteOptionsText: toggleNoteOptions(o); break; // 2 cases
case R.id.colourIndicator:/* AND */case R.id.noteOptions: toggleNoteOptions(o); break;
case R.id.delete: Utilities.deleteDialog = Utilities.showDialog(this, this, deleteV, o); break;
case R.id.cancelDeleteNote: Utilities.deleteDialog.dismiss(); break;
case R.id.confirmDeleteNote: new DeleteNoteTask().execute(); break;
case R.id.addImage: u.reqPermOrSelectImg(this, appContext, STORAGE_PERM, SELECT_IMAGE); break;
case R.id.deleteImage: image.setImageBitmap(null); image.setVisibility(GONE);
findViewById(R.id.deleteImage).setVisibility(GONE); selectedImagePath = ""; break;
case R.id.deleteImage: u.removeImage(this, image); break;
}
}

Expand Down Expand Up @@ -361,10 +343,10 @@ protected Void doInBackground(Void... voids) {
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
Intent intent = new Intent();
intent.putExtra("isNoteDeleted", true);
intent.putExtra("isDeleted", true);
setResult(RESULT_OK, intent);
Toast.makeText(CreateNote.this, "'" + existingNote.getTitle() + "'"
+ " deleted", Toast.LENGTH_LONG).show(); finish();
Toast.makeText(appContext, "'"+ existingNote.getTitle() +"'" + " deleted", LONG).show();
finish();
}
}
}
71 changes: 34 additions & 37 deletions app/src/main/java/com/example/ultranote/activities/Home.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.example.ultranote.activities;

import static utilities.Utilities.ENTER;
import static utilities.Utilities.GRANTED;
import static utilities.Utilities.SHORT;
import static utilities.Utilities.VERTICAL;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.annotation.SuppressLint;
Expand All @@ -9,15 +13,12 @@
import android.text.Editable;
import android.text.TextWatcher;
import database.NotesDatabase;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
Expand Down Expand Up @@ -58,10 +59,7 @@ public class Home extends AppCompatActivity implements NotesListener, View.OnCli
public static final int SHOW_NOTES = 3;
public static final int SELECT_IMAGE = 4;
public static final int STORAGE_PERM = 5;

// States
private static final int GRANTED = PackageManager.PERMISSION_GRANTED;
private static final int VERTICAL = StaggeredGridLayoutManager.VERTICAL;
public static final int ACTION_DONE = 6;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -111,19 +109,19 @@ private void initComponents() {
}

private void updateView() {
if (settings.theme().equals(UserSettings.LIGHT_THEME)) {// Set components to light mode colours
//-|Text Colours|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -//
if (u.appThemeIsLightTheme(settings)) { // Set components to light mode colours \\
//-|Text Colours|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -||
homeText.setTextColor(Utilities.black); qTitleInput.setTextColor(Utilities.black);
searchInput.setTextColor(Utilities.black);
//-|Icon Colours|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -//
//-|Icon Colours|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -||
backBtn.setColorFilter(Utilities.black); settingsBtn.setColorFilter(Utilities.lightGrey);
qAddImgBtn.setColorFilter(Utilities.lightGrey); qAddURLBtn.setColorFilter(Utilities.lightGrey);
searchIcon.setColorFilter(Utilities.darkGrey);
//-|Component Colours|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
//-|Component Colours|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ||
homeView.setBackgroundColor(Utilities.white);
searchBar.setBackgroundColor(Utilities.offWhite);
qActions.setBackgroundColor(Utilities.offWhite);
//-|Component Backgrounds|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
//-|Component Backgrounds|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ||
createNoteBtn.setBackground(Utilities.blue); qTitleInput.setBackground(Utilities.lightInput);
//-|EditText Placeholder Colours|- - - - - - - - - - - - - - - - - - - - - - - - - - - - -//
searchInput.setHintTextColor(Utilities.black); qTitleInput.setHintTextColor(Utilities.black);
Expand All @@ -147,6 +145,7 @@ private void updateView() {
searchInput.setHintTextColor(Utilities.offWhite); qTitleInput.setHintTextColor(Utilities.offWhite);
}

//TODO: Attempt to implement global method for opening activities in Utilities, using switch view.getId()
public void openCreateNote(View view) {
Intent intent = new Intent(this, CreateNote.class); startActivity(intent);
}
Expand All @@ -161,13 +160,19 @@ public void onRequestPermissionsResult(int reqCode, @NonNull String[] perms, @No

if (reqCode == STORAGE_PERM && results.length > 0) {
if (results[0] == GRANTED) { u.selectImage(this, SELECT_IMAGE); }
else { Toast.makeText(this, "Permission Denied!", Toast.LENGTH_SHORT).show(); }
else { Toast.makeText(this, getString(R.string.denied), SHORT).show(); }
}
}

/** -=-= NotesListener Implementation =-=-
* When a note is clicked, its object representation and index are obtained. The object is sent to CreateNote
* for data-mapping. The index is passed to the adapter so that it knows which note widget to update.
* @param note - The note that was clicked
* @param clickedIndex - The note's index in the collection
*/
@Override
public void onNoteClicked(Note note, int clickedPosition) {
noteIndex = clickedPosition;
public void onNoteClicked(Note note, int clickedIndex) {
noteIndex = clickedIndex;
Intent intent = new Intent(appContext, CreateNote.class);
intent.putExtra("viewExisting", true);
intent.putExtra("note", note);
Expand Down Expand Up @@ -201,29 +206,20 @@ protected void onPostExecute(List<Note> notes) {
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
assert data != null;
boolean deleteBool = data.getBooleanExtra("isDeleted", false);

if (resultCode == RESULT_OK) {
switch (requestCode) {
case ADD_NOTE: getNotes(ADD_NOTE, false); break;
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
case UPDATE_NOTE:
getNotes(UPDATE_NOTE, data.getBooleanExtra("isNoteDeleted", false)); break;
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
case SELECT_IMAGE:
Uri uri = data.getData();
if (uri != null) {
try { u.buildNoteWithImage(uri, appContext, this, ADD_NOTE); }
catch (Exception e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
} break;
case UPDATE_NOTE: getNotes(UPDATE_NOTE, deleteBool); break;
case SELECT_IMAGE: u.attemptBuildNoteWithImg(data, this, this); break;
}
}
}

/**
* TextWatcher Implementations. Only 'onTextChanged' and 'afterTextChanged' are required for the search
* feature. Notes can be searched by words/ letters located in any title, subtitle and even content :)
/** -=-= TextWatcher Implementations =-=-
* Only 'onTextChanged' and 'afterTextChanged' are required for the search feature. Notes can be
* searched by words/ letters located in any title, subtitle and even content :)
*/
@Override
public void onTextChanged(CharSequence cs, int i, int i1, int i2) { a.cancelTimer(); }
Expand All @@ -235,16 +231,17 @@ public void beforeTextChanged(CharSequence cs, int i, int i1, int i2) {} // Meth

@Override
public boolean onEditorAction(TextView textView, int a, KeyEvent evt) {
if ((evt != null && (a == EditorInfo.IME_ACTION_DONE ||
evt.getKeyCode() == KeyEvent.KEYCODE_ENTER))) {
Intent noteWithTitle = new Intent(getApplicationContext(), CreateNote.class);
String trimmedTitle = qTitleInput.getText().toString().trim();

if (!trimmedTitle.isEmpty() && (a == ACTION_DONE || evt.getKeyCode() == ENTER)) {
Intent noteWithTitle = new Intent(appContext, CreateNote.class);
noteWithTitle.putExtra("isFromQuickActions", true);
noteWithTitle.putExtra("quickActionType", "title");
noteWithTitle.putExtra("quickTitle", qTitleInput.getText().toString().trim());
startActivityForResult(noteWithTitle, ADD_NOTE);
noteWithTitle.putExtra("quickTitle", trimmedTitle);
startActivityForResult(noteWithTitle, ADD_NOTE); return true;
}

return true;
Toast.makeText(this, getString(R.string.qtitle_error), SHORT).show(); return false;
}

@Override @SuppressLint("NonConstantResourceId")
Expand All @@ -253,7 +250,7 @@ public void onClick(View view) {
case R.id.backButton: onBackPressed(); break;
case R.id.addURL: Utilities.urlDialog = Utilities.showDialog(this, this, this, urlV); break;
case R.id.cancelAddURL: Utilities.urlDialog.dismiss(); break;
case R.id.confirmAddURL: Utilities.validateURL(this, input, Utilities.urlDialog); break;
case R.id.confirmAddURL: Utilities.validateURL(this, input); break;
case R.id.qAddImg: u.reqPermOrSelectImg(this, appContext, STORAGE_PERM, SELECT_IMAGE); break;
}
}
Expand Down
Loading

0 comments on commit 223d3a7

Please sign in to comment.