Skip to content

Commit

Permalink
changes from #3964
Browse files Browse the repository at this point in the history
  • Loading branch information
VishnuSanal committed Oct 24, 2023
1 parent 817f0f9 commit 0674a20
Showing 1 changed file with 37 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@
import android.widget.Toast;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.appcompat.widget.AppCompatImageButton;
import androidx.constraintlayout.widget.ConstraintLayout;
Expand Down Expand Up @@ -133,8 +135,12 @@ public void onCreate(Bundle savedInstanceState) {
return;
}

getSupportActionBar().setDisplayHomeAsUpEnabled(!getBoolean(PREFERENCE_TEXTEDITOR_NEWSTACK));
getSupportActionBar().setTitle(viewModel.getFile().name);
ActionBar actionBar = getSupportActionBar();

if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(!getBoolean(PREFERENCE_TEXTEDITOR_NEWSTACK));
actionBar.setTitle(viewModel.getFile().name);
}

mainTextView.addTextChangedListener(this);

Expand Down Expand Up @@ -170,12 +176,13 @@ public void onCreate(Bundle savedInstanceState) {
}

@Override
protected void onSaveInstanceState(Bundle outState) {
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
final TextEditorActivityViewModel viewModel =
new ViewModelProvider(this).get(TextEditorActivityViewModel.class);

outState.putString(KEY_MODIFIED_TEXT, mainTextView.getText().toString());
outState.putString(
KEY_MODIFIED_TEXT, mainTextView.getText() != null ? mainTextView.getText().toString() : "");
outState.putInt(KEY_INDEX, mainTextView.getScrollY());
outState.putString(KEY_ORIGINAL_TEXT, viewModel.getOriginal());
outState.putBoolean(KEY_MONOFONT, inputTypefaceMono.equals(mainTextView.getTypeface()));
Expand All @@ -187,6 +194,7 @@ private void checkUnsavedChanges() {

if (viewModel.getOriginal() != null
&& mainTextView.isShown()
&& mainTextView.getText() != null
&& !viewModel.getOriginal().equals(mainTextView.getText().toString())) {
new MaterialDialog.Builder(this)
.title(R.string.unsaved_changes)
Expand Down Expand Up @@ -287,10 +295,13 @@ public boolean onOptionsItemSelected(MenuItem item) {
break;
case R.id.save:
// Make sure EditText is visible before saving!
saveFile(this, mainTextView.getText().toString());
if (mainTextView.getText() != null) {
saveFile(this, mainTextView.getText().toString());
}
break;
case R.id.details:
if (editableFileAbstraction.scheme.equals(FILE)
&& editableFileAbstraction.hybridFileParcelable.getFile() != null
&& editableFileAbstraction.hybridFileParcelable.getFile().exists()) {
GeneralDialogCreation.showPropertiesDialogWithoutPermissions(
editableFileAbstraction.hybridFileParcelable, this, getAppTheme());
Expand All @@ -310,7 +321,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
case R.id.openwith:
if (editableFileAbstraction.scheme.equals(FILE)) {
File currentFile = editableFileAbstraction.hybridFileParcelable.getFile();
if (currentFile.exists()) {
if (currentFile != null && currentFile.exists()) {
boolean useNewStack = getBoolean(PREFERENCE_TEXTEDITOR_NEWSTACK);
FileUtils.openWith(currentFile, this, useNewStack);
} else {
Expand Down Expand Up @@ -349,7 +360,8 @@ public void onDestroy() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
// condition to check if callback is called in search editText
if (searchEditText != null && charSequence.hashCode() == searchEditText.getText().hashCode()) {
if (searchEditText.getText() != null
&& charSequence.hashCode() == searchEditText.getText().hashCode()) {
final TextEditorActivityViewModel viewModel =
new ViewModelProvider(this).get(TextEditorActivityViewModel.class);

Expand All @@ -365,7 +377,8 @@ public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3)

@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
if (charSequence.hashCode() == mainTextView.getText().hashCode()) {
if (mainTextView.getText() != null
&& charSequence.hashCode() == mainTextView.getText().hashCode()) {
final TextEditorActivityViewModel viewModel =
new ViewModelProvider(this).get(TextEditorActivityViewModel.class);
final Timer oldTimer = viewModel.getTimer();
Expand Down Expand Up @@ -394,11 +407,12 @@ public void run() {
new ViewModelProvider(textEditorActivity).get(TextEditorActivityViewModel.class);

modified =
!textEditorActivity
.mainTextView
.getText()
.toString()
.equals(viewModel.getOriginal());
textEditorActivity.mainTextView.getText() != null
&& !textEditorActivity
.mainTextView
.getText()
.toString()
.equals(viewModel.getOriginal());
if (viewModel.getModified() != modified) {
viewModel.setModified(modified);
invalidateOptionsMenu();
Expand All @@ -414,7 +428,8 @@ public void run() {
@Override
public void afterTextChanged(Editable editable) {
// searchBox callback block
if (searchEditText != null && editable.hashCode() == searchEditText.getText().hashCode()) {
if (searchEditText.getText() != null
&& editable.hashCode() == searchEditText.getText().hashCode()) {
final WeakReference<TextEditorActivity> textEditorActivityWR = new WeakReference<>(this);

final OnProgressUpdate<SearchResultIndex> onProgressUpdate =
Expand Down Expand Up @@ -454,6 +469,8 @@ public void afterTextChanged(Editable editable) {
}
};

if (mainTextView.getText() == null) return;

searchTextTask =
new SearchTextTask(
mainTextView.getText().toString(),
Expand Down Expand Up @@ -566,6 +583,8 @@ private void highlightCurrentSearchResult(final TextEditorActivityViewModel view
colorSearchResult(keyValueNew, getAccent());

// scrolling to the highlighted element
if (getSupportActionBar() != null) return;

scrollView.scrollTo(
0,
(Integer) keyValueNew.getLineNumber()
Expand All @@ -575,6 +594,8 @@ private void highlightCurrentSearchResult(final TextEditorActivityViewModel view
}

private void colorSearchResult(SearchResultIndex resultIndex, @ColorInt int color) {
if (mainTextView.getText() == null) return;

mainTextView
.getText()
.setSpan(
Expand All @@ -591,6 +612,8 @@ private void cleanSpans(TextEditorActivityViewModel viewModel) {
viewModel.setLine(0);

// clearing textView spans
if (mainTextView.getText() == null) return;

BackgroundColorSpan[] colorSpans =
mainTextView.getText().getSpans(0, mainTextView.length(), BackgroundColorSpan.class);
for (BackgroundColorSpan colorSpan : colorSpans) {
Expand Down

0 comments on commit 0674a20

Please sign in to comment.