Skip to content

Commit

Permalink
addons: viewbinding in myPersonals module (fixes #2564) (#2566)
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 4, 2023
1 parent df49423 commit e73ae22
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 81 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 1075
versionName "0.10.75"
versionCode 1076
versionName "0.10.76"
ndkVersion '21.3.6528147'
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@

import org.ole.planet.myplanet.R;
import org.ole.planet.myplanet.callback.OnSelectedMyPersonal;
import org.ole.planet.myplanet.databinding.AlertMyPersonalBinding;
import org.ole.planet.myplanet.databinding.RowFeedbackBinding;
import org.ole.planet.myplanet.databinding.RowMyPersonalBinding;
import org.ole.planet.myplanet.model.RealmMyPersonal;
import org.ole.planet.myplanet.ui.feedback.AdapterFeedback;
import org.ole.planet.myplanet.ui.viewer.ImageViewerActivity;
import org.ole.planet.myplanet.ui.viewer.PDFReaderActivity;
import org.ole.planet.myplanet.ui.viewer.VideoPlayerActivity;
Expand All @@ -31,7 +35,8 @@

import io.realm.Realm;

public class AdapterMyPersonal extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public class AdapterMyPersonal extends RecyclerView.Adapter<AdapterMyPersonal.ViewHolderMyPersonal> {
private RowMyPersonalBinding rowMyPersonalBinding;
private Context context;
private Realm realm;
private List<RealmMyPersonal> list;
Expand All @@ -52,36 +57,34 @@ public void setRealm(Realm realm) {

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.row_my_personal, parent, false);
return new ViewHolderMyPersonal(v);
public ViewHolderMyPersonal onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
rowMyPersonalBinding = RowMyPersonalBinding.inflate(LayoutInflater.from(context), parent, false);
return new ViewHolderMyPersonal(rowMyPersonalBinding);
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if (holder instanceof ViewHolderMyPersonal) {
((ViewHolderMyPersonal) holder).title.setText(list.get(position).getTitle());
((ViewHolderMyPersonal) holder).description.setText(list.get(position).getDescription());
((ViewHolderMyPersonal) holder).date.setText(TimeUtils.getFormatedDate(list.get(position).getDate()));
((ViewHolderMyPersonal) holder).ivDelete.setOnClickListener(view -> new AlertDialog.Builder(context).setMessage(R.string.delete_record).setPositiveButton(R.string.ok, (dialogInterface, i) -> {
if (!realm.isInTransaction()) realm.beginTransaction();
RealmMyPersonal personal = realm.where(RealmMyPersonal.class).equalTo("_id", list.get(position).get_id()).findFirst();
personal.deleteFromRealm();
realm.commitTransaction();
notifyDataSetChanged();
listener.onAddedResource();
}).setNegativeButton(R.string.cancel, null).show());

((ViewHolderMyPersonal) holder).ivEdit.setOnClickListener(view -> {
editPersonal(list.get(position));
});
holder.itemView.setOnClickListener(view -> {
openResource(list.get(position).getPath());
});
((ViewHolderMyPersonal) holder).ivUpload.setOnClickListener(v -> {
if (listener != null) listener.onUpload(list.get(position));
});
}
public void onBindViewHolder(@NonNull ViewHolderMyPersonal holder, int position) {
rowMyPersonalBinding.title.setText(list.get(position).getTitle());
rowMyPersonalBinding.description.setText(list.get(position).getDescription());
rowMyPersonalBinding.date.setText(TimeUtils.getFormatedDate(list.get(position).getDate()));
rowMyPersonalBinding.imgDelete.setOnClickListener(view -> new AlertDialog.Builder(context).setMessage(R.string.delete_record).setPositiveButton(R.string.ok, (dialogInterface, i) -> {
if (!realm.isInTransaction()) realm.beginTransaction();
RealmMyPersonal personal = realm.where(RealmMyPersonal.class).equalTo("_id", list.get(position).get_id()).findFirst();
personal.deleteFromRealm();
realm.commitTransaction();
notifyDataSetChanged();
listener.onAddedResource();
}).setNegativeButton(R.string.cancel, null).show());

rowMyPersonalBinding.imgEdit.setOnClickListener(view -> {
editPersonal(list.get(position));
});
holder.itemView.setOnClickListener(view -> {
openResource(list.get(position).getPath());
});
rowMyPersonalBinding.imgUpload.setOnClickListener(v -> {
if (listener != null) listener.onUpload(list.get(position));
});
}

private void openResource(String path) {
Expand Down Expand Up @@ -121,44 +124,47 @@ private void openVideo(String path) {
}

private void editPersonal(RealmMyPersonal personal) {
View v = LayoutInflater.from(context).inflate(R.layout.alert_my_personal, null);
EditText etTitle = v.findViewById(R.id.et_title);
EditText etDesc = v.findViewById(R.id.et_description);
etDesc.setText(personal.getDescription());
etTitle.setText(personal.getTitle());
new AlertDialog.Builder(context).setTitle(R.string.edit_personal).setIcon(R.drawable.ic_edit).setView(v).setPositiveButton(R.string.button_submit, (dialogInterface, i) -> {
String title = etTitle.getText().toString().trim();
String desc = etDesc.getText().toString().trim();
if (title.isEmpty()) {
Utilities.toast(context, String.valueOf(R.string.please_enter_title));
return;
}
if (!realm.isInTransaction()) realm.beginTransaction();
personal.setDescription(desc);
personal.setTitle(title);
realm.commitTransaction();
notifyDataSetChanged();
listener.onAddedResource();
}).setNegativeButton(R.string.cancel, null).show();
AlertMyPersonalBinding alertMyPersonalBinding = AlertMyPersonalBinding.inflate(LayoutInflater.from(context));
alertMyPersonalBinding.etDescription.setText(personal.getDescription());
alertMyPersonalBinding.etTitle.setText(personal.getTitle());

new AlertDialog.Builder(context)
.setTitle(R.string.edit_personal)
.setIcon(R.drawable.ic_edit)
.setView(alertMyPersonalBinding.getRoot())
.setPositiveButton(R.string.button_submit, (dialogInterface, i) -> {
String title = alertMyPersonalBinding.etDescription.getText().toString().trim();
String desc = alertMyPersonalBinding.etTitle.getText().toString().trim();

if (title.isEmpty()) {
Utilities.toast(context, String.valueOf(R.string.please_enter_title));
return;
}

if (!realm.isInTransaction()) realm.beginTransaction();
personal.setDescription(desc);
personal.setTitle(title);
realm.commitTransaction();

notifyDataSetChanged();
listener.onAddedResource();
})
.setNegativeButton(R.string.cancel, null)
.show();
}


@Override
public int getItemCount() {
return list.size();
}

class ViewHolderMyPersonal extends RecyclerView.ViewHolder {
TextView title, description, date;
ImageView ivEdit, ivDelete, ivUpload;

public ViewHolderMyPersonal(View itemView) {
super(itemView);
title = itemView.findViewById(R.id.title);
description = itemView.findViewById(R.id.description);
date = itemView.findViewById(R.id.date);
ivDelete = itemView.findViewById(R.id.img_delete);
ivEdit = itemView.findViewById(R.id.img_edit);
ivUpload = itemView.findViewById(R.id.img_upload);
static class ViewHolderMyPersonal extends RecyclerView.ViewHolder {
RowMyPersonalBinding rowMyPersonalBinding;

public ViewHolderMyPersonal(RowMyPersonalBinding rowMyPersonalBinding) {
super(rowMyPersonalBinding.getRoot());
this.rowMyPersonalBinding = rowMyPersonalBinding;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
package org.ole.planet.myplanet.ui.myPersonals;


import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import org.ole.planet.myplanet.R;
import org.ole.planet.myplanet.callback.OnSelectedMyPersonal;
import org.ole.planet.myplanet.databinding.FragmentMyPersonalsBinding;
import org.ole.planet.myplanet.datamanager.DatabaseService;
import org.ole.planet.myplanet.model.RealmMyPersonal;
import org.ole.planet.myplanet.model.RealmUserModel;
Expand All @@ -29,8 +26,7 @@
import io.realm.Realm;

public class MyPersonalsFragment extends Fragment implements OnSelectedMyPersonal {
RecyclerView rvMyPersonal;
TextView tvNodata;
private FragmentMyPersonalsBinding fragmentMyPersonalsBinding;
Realm mRealm;
ProgressDialog pg;

Expand All @@ -40,20 +36,20 @@ public MyPersonalsFragment() {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_my_personals, container, false);
fragmentMyPersonalsBinding = FragmentMyPersonalsBinding.inflate(inflater, container, false);

pg = new ProgressDialog(getActivity());
mRealm = new DatabaseService(getActivity()).getRealmInstance();
rvMyPersonal = v.findViewById(R.id.rv_mypersonal);
tvNodata = v.findViewById(R.id.tv_nodata);
rvMyPersonal.setLayoutManager(new LinearLayoutManager(getActivity()));
v.findViewById(R.id.add_my_personal).setOnClickListener(vi -> {
fragmentMyPersonalsBinding.rvMypersonal.setLayoutManager(new LinearLayoutManager(getActivity()));
fragmentMyPersonalsBinding.addMyPersonal.setOnClickListener(vi -> {
AddResourceFragment f = new AddResourceFragment();
Bundle b = new Bundle();
b.putInt("type", 1);
f.setArguments(b);
f.show(getChildFragmentManager(), getString(R.string.add_resource));
});
return v;
return fragmentMyPersonalsBinding.getRoot();

}

@Override
Expand All @@ -68,18 +64,18 @@ private void setAdapter() {
AdapterMyPersonal personalAdapter = new AdapterMyPersonal(getActivity(), realmMyPersonals);
personalAdapter.setListener(this);
personalAdapter.setRealm(mRealm);
rvMyPersonal.setAdapter(personalAdapter);
fragmentMyPersonalsBinding.rvMypersonal.setAdapter(personalAdapter);
showNodata();
mRealm.addChangeListener(realm -> showNodata());
}

private void showNodata() {
Utilities.log("Show nodata");
if (rvMyPersonal.getAdapter().getItemCount() == 0) {
tvNodata.setVisibility(View.VISIBLE);
tvNodata.setText(R.string.no_data_available_please_click_button_to_add_new_resource_in_mypersonal);
if (fragmentMyPersonalsBinding.rvMypersonal.getAdapter().getItemCount() == 0) {
fragmentMyPersonalsBinding.tvNodata.setVisibility(View.VISIBLE);
fragmentMyPersonalsBinding.tvNodata.setText(R.string.no_data_available_please_click_button_to_add_new_resource_in_mypersonal);
} else {
tvNodata.setVisibility(View.GONE);
fragmentMyPersonalsBinding.tvNodata.setVisibility(View.GONE);
}
}

Expand Down

0 comments on commit e73ae22

Please sign in to comment.