Use binding keyword

This commit is contained in:
M M Arif 2022-10-01 08:59:14 +05:00
parent b0a939102a
commit a4c5e86b13
2 changed files with 40 additions and 46 deletions

View file

@ -26,7 +26,7 @@ import org.mian.gitnex.helpers.Markdown;
*/
public class CreateNoteActivity extends BaseActivity {
private ActivityCreateNoteBinding activityCreateNoteBinding;
private ActivityCreateNoteBinding binding;
private boolean renderMd = false;
private String action;
private Notes notes;
@ -38,19 +38,19 @@ public class CreateNoteActivity extends BaseActivity {
super.onCreate(savedInstanceState);
activityCreateNoteBinding = ActivityCreateNoteBinding.inflate(getLayoutInflater());
binding = ActivityCreateNoteBinding.inflate(getLayoutInflater());
notesApi = BaseApi.getInstance(ctx, NotesApi.class);
setContentView(activityCreateNoteBinding.getRoot());
setSupportActionBar(activityCreateNoteBinding.toolbar);
setContentView(binding.getRoot());
setSupportActionBar(binding.toolbar);
InputMethodManager imm =
(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
assert imm != null;
imm.showSoftInput(activityCreateNoteBinding.noteContent, InputMethodManager.SHOW_IMPLICIT);
activityCreateNoteBinding.noteContent.requestFocus();
imm.showSoftInput(binding.noteContent, InputMethodManager.SHOW_IMPLICIT);
binding.noteContent.requestFocus();
activityCreateNoteBinding.close.setOnClickListener(view -> finish());
binding.close.setOnClickListener(view -> finish());
if (getIntent().getStringExtra("action") != null) {
action = getIntent().getStringExtra("action");
@ -58,26 +58,25 @@ public class CreateNoteActivity extends BaseActivity {
action = "";
}
activityCreateNoteBinding.close.setOnClickListener(close -> finish());
activityCreateNoteBinding.toolbarTitle.setMovementMethod(new ScrollingMovementMethod());
binding.close.setOnClickListener(close -> finish());
binding.toolbarTitle.setMovementMethod(new ScrollingMovementMethod());
if (action.equalsIgnoreCase("edit")) {
noteId = getIntent().getIntExtra("noteId", 0);
notes = notesApi.fetchNoteById(noteId);
activityCreateNoteBinding.noteContent.setText(notes.getContent());
binding.noteContent.setText(notes.getContent());
activityCreateNoteBinding.markdownPreview.setVisibility(View.GONE);
activityCreateNoteBinding.toolbarTitle.setText(R.string.editNote);
binding.markdownPreview.setVisibility(View.GONE);
binding.toolbarTitle.setText(R.string.editNote);
activityCreateNoteBinding.noteContent.addTextChangedListener(
binding.noteContent.addTextChangedListener(
new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
String text =
activityCreateNoteBinding.noteContent.getText().toString();
String text = binding.noteContent.getText().toString();
if (!text.isEmpty()) {
@ -95,16 +94,15 @@ public class CreateNoteActivity extends BaseActivity {
});
} else if (action.equalsIgnoreCase("add")) {
activityCreateNoteBinding.markdownPreview.setVisibility(View.GONE);
binding.markdownPreview.setVisibility(View.GONE);
activityCreateNoteBinding.noteContent.addTextChangedListener(
binding.noteContent.addTextChangedListener(
new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
String text =
activityCreateNoteBinding.noteContent.getText().toString();
String text = binding.noteContent.getText().toString();
if (!text.isEmpty() && text.length() > 4) {
@ -129,7 +127,7 @@ public class CreateNoteActivity extends BaseActivity {
CharSequence s, int start, int before, int count) {}
});
} else {
activityCreateNoteBinding.markdownPreview.setVisibility(View.VISIBLE);
binding.markdownPreview.setVisibility(View.VISIBLE);
}
}
@ -163,18 +161,15 @@ public class CreateNoteActivity extends BaseActivity {
ctx,
EmojiParser.parseToUnicode(
Objects.requireNonNull(
activityCreateNoteBinding
.noteContent
.getText()
.toString())),
activityCreateNoteBinding.markdownPreview);
binding.noteContent.getText().toString())),
binding.markdownPreview);
activityCreateNoteBinding.markdownPreview.setVisibility(View.VISIBLE);
activityCreateNoteBinding.noteContent.setVisibility(View.GONE);
binding.markdownPreview.setVisibility(View.VISIBLE);
binding.noteContent.setVisibility(View.GONE);
renderMd = true;
} else {
activityCreateNoteBinding.markdownPreview.setVisibility(View.GONE);
activityCreateNoteBinding.noteContent.setVisibility(View.VISIBLE);
binding.markdownPreview.setVisibility(View.GONE);
binding.noteContent.setVisibility(View.VISIBLE);
renderMd = false;
}
}

View file

@ -34,7 +34,7 @@ import org.mian.gitnex.helpers.Toasty;
*/
public class NotesFragment extends Fragment {
private FragmentNotesBinding fragmentNotesBinding;
private FragmentNotesBinding binding;
private Context ctx;
private NotesAdapter adapter;
private NotesApi notesApi;
@ -45,7 +45,7 @@ public class NotesFragment extends Fragment {
public View onCreateView(
@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
fragmentNotesBinding = FragmentNotesBinding.inflate(inflater, container, false);
binding = FragmentNotesBinding.inflate(inflater, container, false);
ctx = getContext();
setHasOptionsMenu(true);
@ -55,7 +55,7 @@ public class NotesFragment extends Fragment {
noteIntent = new Intent(ctx, CreateNoteActivity.class);
fragmentNotesBinding.newNote.setOnClickListener(
binding.newNote.setOnClickListener(
view -> {
noteIntent.putExtra("action", "add");
ctx.startActivity(noteIntent);
@ -64,30 +64,29 @@ public class NotesFragment extends Fragment {
notesList = new ArrayList<>();
notesApi = BaseApi.getInstance(ctx, NotesApi.class);
fragmentNotesBinding.recyclerView.setHasFixedSize(true);
fragmentNotesBinding.recyclerView.setLayoutManager(new LinearLayoutManager(ctx));
binding.recyclerView.setHasFixedSize(true);
binding.recyclerView.setLayoutManager(new LinearLayoutManager(ctx));
fragmentNotesBinding.recyclerView.setPadding(0, 0, 0, 220);
fragmentNotesBinding.recyclerView.setClipToPadding(false);
binding.recyclerView.setPadding(0, 0, 0, 220);
binding.recyclerView.setClipToPadding(false);
adapter = new NotesAdapter(ctx, notesList);
fragmentNotesBinding.pullToRefresh.setOnRefreshListener(
binding.pullToRefresh.setOnRefreshListener(
() ->
new Handler(Looper.getMainLooper())
.postDelayed(
() -> {
notesList.clear();
fragmentNotesBinding.pullToRefresh.setRefreshing(false);
fragmentNotesBinding.progressBar.setVisibility(
View.VISIBLE);
binding.pullToRefresh.setRefreshing(false);
binding.progressBar.setVisibility(View.VISIBLE);
fetchDataAsync();
},
250));
fetchDataAsync();
return fragmentNotesBinding.getRoot();
return binding.getRoot();
}
@Override
@ -102,20 +101,20 @@ public class NotesFragment extends Fragment {
.observe(
getViewLifecycleOwner(),
allNotes -> {
fragmentNotesBinding.pullToRefresh.setRefreshing(false);
binding.pullToRefresh.setRefreshing(false);
assert allNotes != null;
if (allNotes.size() > 0) {
notesList.clear();
fragmentNotesBinding.noData.setVisibility(View.GONE);
binding.noData.setVisibility(View.GONE);
notesList.addAll(allNotes);
adapter.notifyDataChanged();
fragmentNotesBinding.recyclerView.setAdapter(adapter);
binding.recyclerView.setAdapter(adapter);
} else {
fragmentNotesBinding.noData.setVisibility(View.VISIBLE);
binding.noData.setVisibility(View.VISIBLE);
}
fragmentNotesBinding.progressBar.setVisibility(View.GONE);
binding.progressBar.setVisibility(View.GONE);
});
}