Skip to content

Commit

Permalink
resources: viewbinding refactor (fixes #2509) (#2587)
Browse files Browse the repository at this point in the history
Co-authored-by: dogi <[email protected]>
  • Loading branch information
Okuro3499 and dogi authored Oct 6, 2023
1 parent 7c64e37 commit 421f1de
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 241 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {
applicationId "org.ole.planet.myplanet"
minSdkVersion 21
targetSdkVersion 34
versionCode 1085
versionName "0.10.85"
versionCode 1086
versionName "0.10.86"
ndkVersion '21.3.6528147'
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
package org.ole.planet.myplanet.ui.library;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatRatingBar;
import androidx.recyclerview.widget.RecyclerView;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.flexbox.FlexboxLayout;
import com.google.gson.JsonObject;
Expand All @@ -23,6 +17,7 @@
import org.ole.planet.myplanet.callback.OnHomeItemClickListener;
import org.ole.planet.myplanet.callback.OnLibraryItemSelected;
import org.ole.planet.myplanet.callback.OnRatingChangeListener;
import org.ole.planet.myplanet.databinding.RowLibraryBinding;
import org.ole.planet.myplanet.model.RealmMyLibrary;
import org.ole.planet.myplanet.model.RealmTag;
import org.ole.planet.myplanet.ui.course.AdapterCourses;
Expand All @@ -39,7 +34,8 @@
import io.noties.markwon.movement.MovementMethodPlugin;
import io.realm.Realm;

public class AdapterLibrary extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public class AdapterLibrary extends RecyclerView.Adapter<AdapterLibrary.ViewHolderLibrary> {
private RowLibraryBinding rowLibraryBinding;
private Context context;
private List<RealmMyLibrary> libraryList;
private List<RealmMyLibrary> selectedItems;
Expand Down Expand Up @@ -86,38 +82,36 @@ public void setListener(OnLibraryItemSelected listener) {

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.row_library, parent, false);
return new ViewHolderLibrary(v);
public ViewHolderLibrary onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
rowLibraryBinding = RowLibraryBinding.inflate(LayoutInflater.from(context), parent, false);
return new ViewHolderLibrary(rowLibraryBinding);
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
if (holder instanceof ViewHolderLibrary) {
Utilities.log("On bind " + position);
((ViewHolderLibrary) holder).title.setText(libraryList.get(position).getTitle());
Utilities.log(libraryList.get(position).getDescription());
markwon.setMarkdown(((ViewHolderLibrary) holder).desc, libraryList.get(position).getDescription());
((ViewHolderLibrary) holder).timesRated.setText(libraryList.get(position).getTimesRated() + context.getString(R.string.total));
((ViewHolderLibrary) holder).checkBox.setChecked(selectedItems.contains(libraryList.get(position)));
((ViewHolderLibrary) holder).rating.setText(TextUtils.isEmpty(libraryList.get(position).getAverageRating()) ? "0.0" : String.format("%.1f", Double.parseDouble(libraryList.get(position).getAverageRating())));
((ViewHolderLibrary) holder).tvDate.setText(TimeUtils.formatDate(libraryList.get(position).getCreatedDate(), "MMM dd, yyyy"));
displayTagCloud(((ViewHolderLibrary) holder).flexboxDrawable, position);
holder.itemView.setOnClickListener(view -> openLibrary(libraryList.get(position)));
((ViewHolderLibrary) holder).ivDownloaded.setImageResource(libraryList.get(position).isResourceOffline() ? R.drawable.ic_eye : R.drawable.ic_download);
if (ratingMap.containsKey(libraryList.get(position).getResource_id())) {
JsonObject object = ratingMap.get(libraryList.get(position).getResource_id());
AdapterCourses.showRating(object, ((ViewHolderLibrary) holder).rating, ((ViewHolderLibrary) holder).timesRated, ((ViewHolderLibrary) holder).ratingBar);
} else {
((ViewHolderLibrary) holder).ratingBar.setRating(0);
}

((ViewHolderLibrary) holder).checkBox.setOnClickListener((view) -> {
Utilities.handleCheck(((CheckBox) view).isChecked(), position, (ArrayList) selectedItems, libraryList);
if (listener != null) listener.onSelectedListChange(selectedItems);
notifyDataSetChanged();
});
public void onBindViewHolder(@NonNull ViewHolderLibrary holder, final int position) {
Utilities.log("On bind " + position);
rowLibraryBinding.title.setText(libraryList.get(position).getTitle());
Utilities.log(libraryList.get(position).getDescription());
markwon.setMarkdown(rowLibraryBinding.description, libraryList.get(position).getDescription());
rowLibraryBinding.timesRated.setText(libraryList.get(position).getTimesRated() + context.getString(R.string.total));
rowLibraryBinding.checkbox.setChecked(selectedItems.contains(libraryList.get(position)));
rowLibraryBinding.rating.setText(TextUtils.isEmpty(libraryList.get(position).getAverageRating()) ? "0.0" : String.format("%.1f", Double.parseDouble(libraryList.get(position).getAverageRating())));
rowLibraryBinding.tvDate.setText(TimeUtils.formatDate(libraryList.get(position).getCreatedDate(), "MMM dd, yyyy"));
displayTagCloud(rowLibraryBinding.flexboxDrawable, position);
holder.itemView.setOnClickListener(view -> openLibrary(libraryList.get(position)));
rowLibraryBinding.ivDownloaded.setImageResource(libraryList.get(position).isResourceOffline() ? R.drawable.ic_eye : R.drawable.ic_download);
if (ratingMap.containsKey(libraryList.get(position).getResource_id())) {
JsonObject object = ratingMap.get(libraryList.get(position).getResource_id());
AdapterCourses.showRating(object, rowLibraryBinding.rating, rowLibraryBinding.timesRated, rowLibraryBinding.ratingBar);
} else {
rowLibraryBinding.ratingBar.setRating(0);
}

rowLibraryBinding.checkbox.setOnClickListener((view) -> {
Utilities.handleCheck(((CheckBox) view).isChecked(), position, (ArrayList) selectedItems, libraryList);
if (listener != null) listener.onSelectedListChange(selectedItems);
notifyDataSetChanged();
});
}

public boolean areAllSelected(){
Expand Down Expand Up @@ -172,31 +166,15 @@ public int getItemCount() {
}

class ViewHolderLibrary extends RecyclerView.ViewHolder {
TextView title, desc, rating, tvDate, timesRated, average;
CheckBox checkBox;
AppCompatRatingBar ratingBar;
FlexboxLayout flexboxDrawable;
LinearLayout llRating;
ImageView ivDownloaded;

public ViewHolderLibrary(View itemView) {
super(itemView);
title = itemView.findViewById(R.id.title);
desc = itemView.findViewById(R.id.description);
rating = itemView.findViewById(R.id.rating);
timesRated = itemView.findViewById(R.id.times_rated);
ratingBar = itemView.findViewById(R.id.rating_bar);
tvDate = itemView.findViewById(R.id.tv_date);
checkBox = itemView.findViewById(R.id.checkbox);
llRating = itemView.findViewById(R.id.ll_rating);
ivDownloaded = itemView.findViewById(R.id.iv_downloaded);
average = itemView.findViewById(R.id.average);
ratingBar.setOnTouchListener((v1, event) -> {
RowLibraryBinding rowLibraryBinding;
public ViewHolderLibrary(RowLibraryBinding rowLibraryBinding) {
super(rowLibraryBinding.getRoot());
this.rowLibraryBinding = rowLibraryBinding;
rowLibraryBinding.ratingBar.setOnTouchListener((v1, event) -> {
if (event.getAction() == MotionEvent.ACTION_UP)
homeItemClickListener.showRatingDialog("resource", libraryList.get(getAdapterPosition()).getResource_id(), libraryList.get(getAdapterPosition()).getTitle(), ratingChangeListener);
return true;
});
flexboxDrawable = itemView.findViewById(R.id.flexbox_drawable);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
package org.ole.planet.myplanet.ui.library;

import android.os.Bundle;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import com.google.android.material.textfield.TextInputLayout;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import org.ole.planet.myplanet.R;
import org.ole.planet.myplanet.databinding.ActivityAddResourceBinding;
import org.ole.planet.myplanet.datamanager.DatabaseService;
import org.ole.planet.myplanet.model.RealmMyLibrary;
import org.ole.planet.myplanet.model.RealmUserModel;
Expand All @@ -32,9 +28,7 @@
import io.realm.RealmList;

public class AddResourceActivity extends AppCompatActivity {
EditText etTitle, etAuthor, etYear, etDescription, etPublisher, etLinkToLicense, etOpenWhich;
Spinner spnLang, spnMedia, spnResourceType, spnOpenWith;
TextView tvSubjects, tvLevels, tvResourceFor, tvAddedBy, fileUrl;
private ActivityAddResourceBinding activityAddResourceBinding;
Realm mRealm;
RealmUserModel userModel;
RealmList<String> subjects;
Expand All @@ -45,7 +39,8 @@ public class AddResourceActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_resource);
activityAddResourceBinding = ActivityAddResourceBinding.inflate(getLayoutInflater());
setContentView(activityAddResourceBinding.getRoot());
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
userModel = new UserProfileDbHandler(this).getUserModel();
Expand All @@ -64,33 +59,17 @@ protected void onDestroy() {
}

private void initializeViews() {
etTitle = findViewById(R.id.et_title);
etAuthor = findViewById(R.id.et_author);
fileUrl = findViewById(R.id.file_url);
etYear = findViewById(R.id.et_year);
etDescription = findViewById(R.id.et_description);
etPublisher = findViewById(R.id.et_publisher);
etLinkToLicense = findViewById(R.id.et_link_to_license);
etOpenWhich = findViewById(R.id.et_open_which);
spnLang = findViewById(R.id.spn_lang);
spnMedia = findViewById(R.id.spn_media);
spnResourceType = findViewById(R.id.spn_resource_type);
spnOpenWith = findViewById(R.id.spn_open_with);
tvSubjects = findViewById(R.id.tv_subject);
tvAddedBy = findViewById(R.id.tv_added_by);
tvLevels = findViewById(R.id.tv_levels);
tvResourceFor = findViewById(R.id.tv_resource_for);
fileUrl.setText(getString(R.string.file) + resourceUrl);
tvAddedBy.setText(userModel.getName());
tvLevels.setOnClickListener(view -> showMultiSelectList(getResources().getStringArray(R.array.array_levels), levels, view));
tvSubjects.setOnClickListener(view -> showMultiSelectList(getResources().getStringArray(R.array.array_subjects), subjects, view));
tvResourceFor.setOnClickListener(view -> showMultiSelectList(getResources().getStringArray(R.array.array_resource_for), subjects, view));
findViewById(R.id.btn_submit).setOnClickListener(view -> saveResource());
findViewById(R.id.btn_cancel).setOnClickListener(view -> finish());
activityAddResourceBinding.fileUrl.setText(getString(R.string.file) + resourceUrl);
activityAddResourceBinding.tvAddedBy.setText(userModel.getName());
activityAddResourceBinding.tvLevels.setOnClickListener(view -> showMultiSelectList(getResources().getStringArray(R.array.array_levels), levels, view));
activityAddResourceBinding.tvSubject.setOnClickListener(view -> showMultiSelectList(getResources().getStringArray(R.array.array_subjects), subjects, view));
activityAddResourceBinding.tvResourceFor.setOnClickListener(view -> showMultiSelectList(getResources().getStringArray(R.array.array_resource_for), subjects, view));
activityAddResourceBinding.btnSubmit.setOnClickListener(view -> saveResource());
activityAddResourceBinding.btnCancel.setOnClickListener(view -> finish());
}

private void saveResource() {
String title = etTitle.getText().toString().trim();
String title = activityAddResourceBinding.etTitle.getText().toString().trim();
if (!validate(title)) return;
mRealm.executeTransactionAsync(realm -> {
String id = UUID.randomUUID().toString();
Expand All @@ -104,17 +83,17 @@ private void saveResource() {
}

private void createResource(RealmMyLibrary resource, String id) {
resource.setAddedBy(tvAddedBy.getText().toString().trim());
resource.setAuthor(etAuthor.getText().toString().trim());
resource.setAddedBy(activityAddResourceBinding.tvAddedBy.getText().toString().trim());
resource.setAuthor(activityAddResourceBinding.etAuthor.getText().toString().trim());
resource.setResource_id(id);
resource.setYear(etYear.getText().toString().trim());
resource.setDescription(etDescription.getText().toString().trim());
resource.setPublisher(etPublisher.getText().toString().trim());
resource.setLinkToLicense(etLinkToLicense.getText().toString().trim());
resource.setOpenWith(spnOpenWith.getSelectedItem().toString());
resource.setLanguage(spnLang.getSelectedItem().toString());
resource.setMediaType(spnMedia.getSelectedItem().toString());
resource.setResourceType(spnResourceType.getSelectedItem().toString());
resource.setYear(activityAddResourceBinding.etYear.getText().toString().trim());
resource.setDescription(activityAddResourceBinding.etDescription.getText().toString().trim());
resource.setPublisher(activityAddResourceBinding.etPublisher.getText().toString().trim());
resource.setLinkToLicense(activityAddResourceBinding.etLinkToLicense.getText().toString().trim());
resource.setOpenWith(activityAddResourceBinding.spnOpenWith.getSelectedItem().toString());
resource.setLanguage(activityAddResourceBinding.spnLang.getSelectedItem().toString());
resource.setMediaType(activityAddResourceBinding.spnMedia.getSelectedItem().toString());
resource.setResourceType(activityAddResourceBinding.spnResourceType.getSelectedItem().toString());
resource.setSubject(subjects);
resource.setUserId(new RealmList<>());
resource.setLevel(levels);
Expand All @@ -127,7 +106,7 @@ private void createResource(RealmMyLibrary resource, String id) {

private boolean validate(String title) {
if (title.isEmpty()) {
((TextInputLayout) findViewById(R.id.tl_title)).setError(getString(R.string.title_is_required));
activityAddResourceBinding.tlTitle.setError(getString(R.string.title_is_required));
return false;
}
if (levels.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static android.app.Activity.RESULT_OK;

import android.app.Activity;
import android.app.Dialog;
import android.content.ContentValues;
import android.content.Context;
Expand All @@ -23,7 +22,6 @@

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.core.content.FileProvider;

import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialog;
Expand All @@ -33,6 +31,8 @@
import org.jetbrains.annotations.NotNull;
import org.ole.planet.myplanet.MainApplication;
import org.ole.planet.myplanet.R;
import org.ole.planet.myplanet.databinding.AlertSoundRecorderBinding;
import org.ole.planet.myplanet.databinding.FragmentAddResourceBinding;
import org.ole.planet.myplanet.datamanager.DatabaseService;
import org.ole.planet.myplanet.model.RealmMyPersonal;
import org.ole.planet.myplanet.model.RealmUserModel;
Expand All @@ -48,6 +48,7 @@
import io.realm.Realm;

public class AddResourceFragment extends BottomSheetDialogFragment {
private FragmentAddResourceBinding fragmentAddResourceBinding;
static final int REQUEST_VIDEO_CAPTURE = 1;
static final int REQUEST_RECORD_SOUND = 0;
static final int REQUEST_CAPTURE_PICTURE = 2;
Expand Down Expand Up @@ -113,29 +114,36 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_add_resource, container, false);
v.findViewById(R.id.ll_record_video).setOnClickListener(view -> dispatchTakeVideoIntent());
v.findViewById(R.id.ll_record_audio).setOnClickListener(view -> {
fragmentAddResourceBinding = FragmentAddResourceBinding.inflate(inflater, container, false);
fragmentAddResourceBinding.llRecordVideo.setOnClickListener(view -> dispatchTakeVideoIntent());
fragmentAddResourceBinding.llRecordAudio.setOnClickListener(view -> {
showAudioRecordAlert();
});
v.findViewById(R.id.ll_capture_image).setOnClickListener(view -> takePhoto());
v.findViewById(R.id.ll_draft).setOnClickListener(view -> FileUtils.openOleFolder(this, 100));
return v;
fragmentAddResourceBinding.llCaptureImage.setOnClickListener(view -> takePhoto());
fragmentAddResourceBinding.llDraft.setOnClickListener(view -> FileUtils.openOleFolder(this, 100));
return fragmentAddResourceBinding.getRoot();
}

private void showAudioRecordAlert() {
View v = LayoutInflater.from(getActivity()).inflate(R.layout.alert_sound_recorder, null);
tvTime = v.findViewById(R.id.tv_time);
floatingActionButton = v.findViewById(R.id.fab_record);
AlertDialog dialog = new AlertDialog.Builder(getActivity()).setTitle("Record Audio").setView(v).setCancelable(false).create();
AlertSoundRecorderBinding alertSoundRecorderBinding = AlertSoundRecorderBinding.inflate(LayoutInflater.from(getActivity()));
tvTime = alertSoundRecorderBinding.tvTime;
floatingActionButton = alertSoundRecorderBinding.fabRecord;
AlertDialog dialog = new AlertDialog.Builder(getActivity())
.setTitle("Record Audio")
.setView(alertSoundRecorderBinding.getRoot())
.setCancelable(false)
.create();

createAudioRecorderService(dialog);
floatingActionButton.setOnClickListener(view -> {

alertSoundRecorderBinding.fabRecord.setOnClickListener(view -> {
if (!audioRecorderService.isRecording()) {
audioRecorderService.startRecording();
} else {
audioRecorderService.stopRecording();
}
});

dialog.setButton(AlertDialog.BUTTON_POSITIVE, getString(R.string.dismiss), (dialogInterface, i) -> {
if (audioRecorderService != null && audioRecorderService.isRecording()) {
audioRecorderService.forceStop();
Expand Down Expand Up @@ -253,9 +261,7 @@ private String getRealPathFromUri(Uri uri) {
return cursor.getString(columnIndex);
}
}

return "";

}

private void addResource(String path) {
Expand Down
Loading

0 comments on commit 421f1de

Please sign in to comment.