Skip to content

Commit

Permalink
Fixes Automattic#618 and correctly themes the bottom sheets when dark…
Browse files Browse the repository at this point in the history
… theme is applied. However, this does not fix Automattic#613 and the `RecyclerView` showing the sharing app buttons displays incorrect text colors THE FIRST TIME ONLY, which is caused by the same issue in Automattic#613.
  • Loading branch information
iskandergaba committed Jan 9, 2019
1 parent 9fe1010 commit 517a0c0
Show file tree
Hide file tree
Showing 14 changed files with 225 additions and 141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.automattic.simplenote.models.Note;
Expand All @@ -30,11 +31,11 @@ public class InfoBottomSheetDialog extends BottomSheetDialogBase {
private SwitchCompat mInfoPinSwitch;
private SwitchCompat mInfoMarkdownSwitch;
private ImageButton mCopyButton;
private ImageButton mShareButton;
private LinearLayout mShareButton;
private Fragment mFragment;

public InfoBottomSheetDialog(@NonNull Fragment fragment, @NonNull final InfoSheetListener infoSheetListener) {
super(fragment.getActivity());
super(fragment.requireActivity());

mFragment = fragment;

Expand Down Expand Up @@ -95,7 +96,6 @@ public void onDismiss(DialogInterface dialog) {
}

public void show(Note note) {

if (mFragment.isAdded()) {
String date = DateTimeUtils.getDateText(mFragment.getActivity(), note.getModificationDate());
mInfoModifiedDate.setText(String.format(mFragment.getString(R.string.modified_time), date));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
Expand Down Expand Up @@ -114,7 +115,7 @@ public void run() {
public void run() {
if (!isAdded()) return;

getActivity().runOnUiThread(new Runnable() {
requireActivity().runOnUiThread(new Runnable() {
@Override
public void run() {

Expand Down Expand Up @@ -175,7 +176,7 @@ public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
case R.id.menu_copy:
if (mLinkText != null && getActivity() != null) {
copyToClipboard(mLinkText);
Toast.makeText(getActivity(), getString(R.string.link_copied), Toast.LENGTH_SHORT).show();
Toast.makeText(requireActivity(), getString(R.string.link_copied), Toast.LENGTH_SHORT).show();
mode.finish();
}
return true;
Expand Down Expand Up @@ -204,7 +205,7 @@ public void onDestroyActionMode(ActionMode mode) {
public void run() {
if (!isAdded()) return;

getActivity().runOnUiThread(new Runnable() {
requireActivity().runOnUiThread(new Runnable() {
@Override
public void run() {

Expand All @@ -230,11 +231,12 @@ public NoteEditorFragment() {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mInfoBottomSheet = new InfoBottomSheetDialog(this, this);
mShareBottomSheet = new ShareBottomSheetDialog(this, this);
mHistoryBottomSheet = new HistoryBottomSheetDialog(this, this);

if (getActivity() != null) {
Simplenote currentApp = (Simplenote) getActivity().getApplication();
mNotesBucket = currentApp.getNotesBucket();
}
Simplenote currentApp = (Simplenote) requireActivity().getApplication();
mNotesBucket = currentApp.getNotesBucket();

mCallIcon = DrawableUtils.tintDrawableWithAttribute(getActivity(), R.drawable.ic_call_white_24dp, R.attr.actionModeTextColor);
mEmailIcon = DrawableUtils.tintDrawableWithAttribute(getActivity(), R.drawable.ic_email_white_24dp, R.attr.actionModeTextColor);
Expand All @@ -245,7 +247,7 @@ public void onCreate(Bundle savedInstanceState) {
mPublishTimeoutHandler = new Handler();
mHistoryTimeoutHandler = new Handler();

mMatchHighlighter = new TextHighlighter(getActivity(),
mMatchHighlighter = new TextHighlighter(requireActivity(),
R.attr.editorSearchHighlightForegroundColor, R.attr.editorSearchHighlightBackgroundColor);
mAutocompleteAdapter = new CursorAdapter(getActivity(), null, 0x0) {
@Override
Expand Down Expand Up @@ -285,15 +287,13 @@ public Cursor runQueryOnBackgroundThread(CharSequence filter) {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
setHasOptionsMenu(true);
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mRootView = inflater.inflate(R.layout.fragment_note_editor, container, false);
mContentEditText = mRootView.findViewById(R.id.note_content);
mContentEditText.addOnSelectionChangedListener(this);
mTagView = mRootView.findViewById(R.id.tag_view);
mTagView.setTokenizer(new SpaceTokenizer());
mTagView.setOnFocusChangeListener(this);

mHighlighter = new MatchOffsetHighlighter(mMatchHighlighter, mContentEditText);

mPlaceholderView = mRootView.findViewById(R.id.placeholder);
Expand Down Expand Up @@ -359,7 +359,7 @@ public void onGlobalLayout() {
}
}
});

setHasOptionsMenu(true);
return mRootView;
}

Expand Down Expand Up @@ -410,7 +410,7 @@ public void onPause() {
}

@Override
public void onSaveInstanceState(Bundle outState) {
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);

if (DisplayUtils.isLargeScreenLandscape(getActivity()) && mNote != null) {
Expand All @@ -420,6 +420,7 @@ public void onSaveInstanceState(Bundle outState) {

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
if (!isAdded() || DisplayUtils.isLargeScreenLandscape(getActivity()) && mNoteMarkdownFragment == null) {
return;
}
Expand All @@ -440,10 +441,7 @@ public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
trashItem.setIcon(R.drawable.ic_trash_24dp);
}
}

DrawableUtils.tintMenuWithAttribute(getActivity(), menu, R.attr.actionBarTextColor);

super.onCreateOptionsMenu(menu, inflater);
}

@Override
Expand All @@ -464,7 +462,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
return true;
case android.R.id.home:
if (!isAdded()) return false;
getActivity().finish();
requireActivity().finish();
return true;
default:
return super.onOptionsItemSelected(item);
Expand All @@ -473,7 +471,7 @@ public boolean onOptionsItemSelected(MenuItem item) {

private void deleteNote() {
NoteUtils.deleteNote(mNote, getActivity());
getActivity().finish();
requireActivity().finish();
}

protected void clearMarkdown() {
Expand All @@ -489,20 +487,6 @@ protected void showMarkdown() {
mMarkdown.setVisibility(View.VISIBLE);
}

private void permanentlyDeleteNote() {
if (mNote == null) {
return;
}

// A note has to be 'trashed' first before it can be deleted forever
// setDeleted() sets a 'deleted' property to signify a note is in the trash
mNote.setDeleted(true);
mNote.save();

// delete() actually permanently deletes the note from Simperium
mNote.delete();
}

private void shareNote() {
if (mNote != null) {
mContentEditText.clearFocus();
Expand Down Expand Up @@ -1031,11 +1015,11 @@ private void updatePublishedState(boolean isSuccess) {
if (mNote.isPublished()) {

if (mIsUndoingPublishing) {
SnackbarUtils.showSnackbar(getActivity(), R.string.publish_successful,
SnackbarUtils.showSnackbar(requireActivity(), R.string.publish_successful,
R.color.simplenote_positive_green,
Snackbar.LENGTH_LONG);
} else {
SnackbarUtils.showSnackbar(getActivity(), R.string.publish_successful,
SnackbarUtils.showSnackbar(requireActivity(), R.string.publish_successful,
R.color.simplenote_positive_green,
Snackbar.LENGTH_LONG, R.string.undo, new View.OnClickListener() {
@Override
Expand All @@ -1048,11 +1032,11 @@ public void onClick(View v) {
copyToClipboard(mNote.getPublishedUrl());
} else {
if (mIsUndoingPublishing) {
SnackbarUtils.showSnackbar(getActivity(), R.string.unpublish_successful,
SnackbarUtils.showSnackbar(requireActivity(), R.string.unpublish_successful,
R.color.simplenote_negative_red,
Snackbar.LENGTH_LONG);
} else {
SnackbarUtils.showSnackbar(getActivity(), R.string.unpublish_successful,
SnackbarUtils.showSnackbar(requireActivity(), R.string.unpublish_successful,
R.color.simplenote_negative_red,
Snackbar.LENGTH_LONG, R.string.undo, new View.OnClickListener() {
@Override
Expand All @@ -1065,10 +1049,10 @@ public void onClick(View v) {
}
} else {
if (mNote.isPublished()) {
SnackbarUtils.showSnackbar(getActivity(), R.string.unpublish_error,
SnackbarUtils.showSnackbar(requireActivity(), R.string.unpublish_error,
R.color.simplenote_negative_red, Snackbar.LENGTH_LONG);
} else {
SnackbarUtils.showSnackbar(getActivity(), R.string.publish_error,
SnackbarUtils.showSnackbar(requireActivity(), R.string.publish_error,
R.color.simplenote_negative_red, Snackbar.LENGTH_LONG);
}
}
Expand All @@ -1079,7 +1063,7 @@ public void onClick(View v) {
private void publishNote() {

if (isAdded()) {
mPublishingSnackbar = SnackbarUtils.showSnackbar(getActivity(), R.string.publishing,
mPublishingSnackbar = SnackbarUtils.showSnackbar(requireActivity(), R.string.publishing,
R.color.simplenote_blue, Snackbar.LENGTH_INDEFINITE);
}
setPublishedNote(true);
Expand All @@ -1088,37 +1072,36 @@ private void publishNote() {
private void unpublishNote() {

if (isAdded()) {
mPublishingSnackbar = SnackbarUtils.showSnackbar(getActivity(), R.string.unpublishing,
mPublishingSnackbar = SnackbarUtils.showSnackbar(requireActivity(), R.string.unpublishing,
R.color.simplenote_blue, Snackbar.LENGTH_INDEFINITE);
}
setPublishedNote(false);
}

private void copyToClipboard(String text) {
ClipboardManager clipboard = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
ClipboardManager clipboard = (ClipboardManager) requireActivity()
.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText(getString(R.string.app_name), text);
clipboard.setPrimaryClip(clip);
if (clipboard != null) {
clipboard.setPrimaryClip(clip);
}
}

private void showShareSheet() {
if (isAdded()) {
mShareBottomSheet = new ShareBottomSheetDialog(this, this);
mShareBottomSheet.show(mNote);
}
}

private void showInfoSheet() {
if (isAdded()) {
mInfoBottomSheet = new InfoBottomSheetDialog(this, this);
mInfoBottomSheet.show(mNote);
}

}

private void showHistorySheet() {
if (isAdded()) {
mHistoryBottomSheet = new HistoryBottomSheetDialog(this, this);

// Request revisions for the current note
mNotesBucket.getRevisions(mNote, MAX_REVISIONS, mHistoryBottomSheet.getRevisionsRequestCallbacks());
saveNote();
Expand Down Expand Up @@ -1275,7 +1258,7 @@ public void run() {

fragment.updateMarkdownView();

fragment.getActivity().invalidateOptionsMenu();
fragment.requireActivity().invalidateOptionsMenu();

fragment.linkifyEditorContent();
fragment.mIsLoadingNote = false;
Expand Down Expand Up @@ -1332,8 +1315,9 @@ private void updateMarkdownView() {
} else {
// This fragment lives in the NoteEditorActivity's ViewPager.
if (mNoteMarkdownFragment == null) {
mNoteMarkdownFragment = ((NoteEditorActivity) getActivity()).getNoteMarkdownFragment();
((NoteEditorActivity) getActivity()).showTabs();
mNoteMarkdownFragment = ((NoteEditorActivity) requireActivity())
.getNoteMarkdownFragment();
((NoteEditorActivity) requireActivity()).showTabs();
}
// Load markdown in the sibling NoteMarkdownFragment's WebView.
mNoteMarkdownFragment.updateMarkdown(getNoteContentString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public boolean onItemLongClick(AdapterView<?> adapterView, View view, int positi
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
getListView().setItemChecked(position, true);
if (mActionMode == null)
getActivity().startActionMode(this);
requireActivity().startActionMode(this);
return true;
}

Expand Down Expand Up @@ -187,7 +187,7 @@ public void onItemCheckedStateChanged(ActionMode actionMode, int position, long
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mNotesAdapter = new NotesCursorAdapter(getActivity().getBaseContext(), null, 0);
mNotesAdapter = new NotesCursorAdapter(requireActivity().getBaseContext(), null, 0);
setListAdapter(mNotesAdapter);
}

Expand All @@ -204,10 +204,10 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

NotesActivity notesActivity = (NotesActivity) getActivity();
NotesActivity notesActivity = (NotesActivity) requireActivity();

if (ACTION_NEW_NOTE.equals(notesActivity.getIntent().getAction()) &&
!notesActivity.userIsUnauthorized()){
Expand Down Expand Up @@ -315,7 +315,7 @@ public void selectFirstNote() {
}

@Override
public void onSaveInstanceState(Bundle outState) {
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
if (mActivatedPosition != ListView.INVALID_POSITION) {
// Serialize and persist the activated item position.
Expand Down Expand Up @@ -385,7 +385,7 @@ public void refreshListFromNavSelect() {
public ObjectCursor<Note> queryNotes() {
if (!isAdded()) return null;

NotesActivity notesActivity = (NotesActivity) getActivity();
NotesActivity notesActivity = (NotesActivity) requireActivity();
Query<Note> query = notesActivity.getSelectedTag().query();

String searchString = mSearchString;
Expand Down Expand Up @@ -421,12 +421,12 @@ private String queryTags(Query<Note> query, String searchString) {
public void addNote() {

// Prevents jarring 'New note...' from showing in the list view when creating a new note
NotesActivity notesActivity = (NotesActivity) getActivity();
NotesActivity notesActivity = (NotesActivity) requireActivity();
if (!DisplayUtils.isLargeScreenLandscape(notesActivity))
notesActivity.stopListeningToNotesBucket();

// Create & save new note
Simplenote simplenote = (Simplenote) getActivity().getApplication();
Simplenote simplenote = (Simplenote) requireActivity().getApplication();
Bucket<Note> notesBucket = simplenote.getNotesBucket();
final Note note = notesBucket.newObject();
note.setCreationDate(Calendar.getInstance());
Expand Down Expand Up @@ -458,7 +458,7 @@ public void run() {
Intent editNoteIntent = new Intent(getActivity(), NoteEditorActivity.class);
editNoteIntent.putExtras(arguments);

getActivity().startActivityForResult(editNoteIntent, Simplenote.INTENT_EDIT_NOTE);
requireActivity().startActivityForResult(editNoteIntent, Simplenote.INTENT_EDIT_NOTE);
}
}

Expand Down Expand Up @@ -559,7 +559,7 @@ public void setNoteId(String noteId) {
public class NotesCursorAdapter extends CursorAdapter {
private ObjectCursor<Note> mCursor;

private SearchSnippetFormatter.SpanFactory mSnippetHighlighter = new TextHighlighter(getActivity(),
private SearchSnippetFormatter.SpanFactory mSnippetHighlighter = new TextHighlighter(requireActivity(),
R.attr.listSearchHighlightForegroundColor, R.attr.listSearchHighlightBackgroundColor);

public NotesCursorAdapter(Context context, ObjectCursor<Note> c, int flags) {
Expand All @@ -586,7 +586,7 @@ public View getView(final int position, View view, ViewGroup parent) {

final NoteViewHolder holder;
if (view == null) {
view = View.inflate(getActivity().getBaseContext(), R.layout.note_list_row, null);
view = View.inflate(requireActivity().getBaseContext(), R.layout.note_list_row, null);
holder = new NoteViewHolder();
holder.titleTextView = view.findViewById(R.id.note_title);
holder.contentTextView = view.findViewById(R.id.note_content);
Expand Down
Loading

0 comments on commit 517a0c0

Please sign in to comment.