mirror of
https://github.com/gitnex-org/gitnex.git
synced 2026-07-20 20:43:31 -05:00
Add bottom sheet and delete wiki page
This commit is contained in:
@@ -105,8 +105,7 @@ public class ReleasesAdapter extends RecyclerView.Adapter<ReleasesAdapter.Releas
|
||||
optionsMenu.setOnClickListener(v -> {
|
||||
final Context context = v.getContext();
|
||||
|
||||
@SuppressLint("InflateParams")
|
||||
View view = LayoutInflater.from(context).inflate(R.layout.bottom_sheet_release_in_list, null);
|
||||
View view = LayoutInflater.from(context).inflate(R.layout.bottom_sheet_release_in_list, itemView.findViewById(android.R.id.content), false);
|
||||
|
||||
TextView deleteRelease = view.findViewById(R.id.deleteRelease);
|
||||
|
||||
|
||||
@@ -10,18 +10,26 @@ import android.view.ViewGroup;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.core.text.HtmlCompat;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
import com.amulyakhare.textdrawable.TextDrawable;
|
||||
import com.amulyakhare.textdrawable.util.ColorGenerator;
|
||||
import com.google.android.material.bottomsheet.BottomSheetDialog;
|
||||
import org.gitnex.tea4j.v2.models.WikiPageMetaData;
|
||||
import org.mian.gitnex.R;
|
||||
import org.mian.gitnex.activities.RepoDetailActivity;
|
||||
import org.mian.gitnex.activities.WikiActivity;
|
||||
import org.mian.gitnex.clients.RetrofitClient;
|
||||
import org.mian.gitnex.databinding.FragmentWikiBinding;
|
||||
import org.mian.gitnex.helpers.ClickListener;
|
||||
import org.mian.gitnex.helpers.TimeHelper;
|
||||
import org.mian.gitnex.helpers.Toasty;
|
||||
import org.mian.gitnex.helpers.contexts.RepositoryContext;
|
||||
import java.util.List;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Response;
|
||||
|
||||
/**
|
||||
* @author M M Arif
|
||||
@@ -33,10 +41,16 @@ public class WikiListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
|
||||
private List<WikiPageMetaData> wikiList;
|
||||
private OnLoadMoreListener loadMoreListener;
|
||||
private boolean isLoading = false, isMoreDataAvailable = true;
|
||||
private final FragmentWikiBinding fragmentWikiBinding;
|
||||
private final String repoOwner;
|
||||
private final String repoName;
|
||||
|
||||
public WikiListAdapter(List<WikiPageMetaData> wikiListMain, Context ctx) {
|
||||
public WikiListAdapter(List<WikiPageMetaData> wikiListMain, Context ctx, String repoOwner, String repoName, FragmentWikiBinding fragmentWikiBinding) {
|
||||
this.ctx = ctx;
|
||||
this.wikiList = wikiListMain;
|
||||
this.repoOwner = repoOwner;
|
||||
this.repoName = repoName;
|
||||
this.fragmentWikiBinding = fragmentWikiBinding;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@@ -73,6 +87,7 @@ public class WikiListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
|
||||
private final ImageView avatar;
|
||||
private final TextView pageName;
|
||||
private final TextView wikiLastUpdatedBy;
|
||||
private final ImageView wikiMenu;
|
||||
|
||||
WikisHolder(View itemView) {
|
||||
|
||||
@@ -80,13 +95,40 @@ public class WikiListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
|
||||
pageName = itemView.findViewById(R.id.page_name);
|
||||
avatar = itemView.findViewById(R.id.image_avatar);
|
||||
wikiLastUpdatedBy = itemView.findViewById(R.id.wiki_last_updated_by);
|
||||
wikiMenu = itemView.findViewById(R.id.wiki_menu);
|
||||
|
||||
itemView.setOnClickListener(v -> {
|
||||
|
||||
Intent intent = new Intent(ctx, WikiActivity.class);
|
||||
intent.putExtra("pageName", wikiPageMeta.getTitle());
|
||||
intent.putExtra(RepositoryContext.INTENT_EXTRA, ((RepoDetailActivity) itemView.getContext()).repository);
|
||||
ctx.startActivity(intent);
|
||||
});
|
||||
|
||||
wikiMenu.setOnClickListener(v -> {
|
||||
|
||||
Context ctx = v.getContext();
|
||||
|
||||
View view = LayoutInflater.from(ctx).inflate(R.layout.bottom_sheet_wiki_in_list, itemView.findViewById(android.R.id.content), false);
|
||||
|
||||
TextView editWiki = view.findViewById(R.id.edit_wiki);
|
||||
TextView deleteWiki = view.findViewById(R.id.delete_wiki);
|
||||
|
||||
BottomSheetDialog dialog = new BottomSheetDialog(ctx);
|
||||
dialog.setContentView(view);
|
||||
dialog.show();
|
||||
|
||||
editWiki.setOnClickListener(v12 -> {
|
||||
|
||||
dialog.dismiss();
|
||||
});
|
||||
|
||||
deleteWiki.setOnClickListener(v12 -> {
|
||||
|
||||
deleteWiki(repoOwner, repoName, wikiPageMeta.getTitle(), getAbsoluteAdapterPosition(), ctx);
|
||||
dialog.dismiss();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@SuppressLint("SetTextI18n")
|
||||
@@ -110,6 +152,12 @@ public class WikiListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
|
||||
}
|
||||
}
|
||||
|
||||
private void updateAdapter(int position) {
|
||||
wikiList.remove(position);
|
||||
notifyItemRemoved(position);
|
||||
notifyItemRangeChanged(position, wikiList.size());
|
||||
}
|
||||
|
||||
public void setMoreDataAvailable(boolean moreDataAvailable) {
|
||||
isMoreDataAvailable = moreDataAvailable;
|
||||
if(!isMoreDataAvailable) {
|
||||
@@ -137,4 +185,40 @@ public class WikiListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
|
||||
wikiList = list;
|
||||
notifyDataChanged();
|
||||
}
|
||||
|
||||
private void deleteWiki(final String owner, final String repo, final String pageName, int position, final Context context) {
|
||||
|
||||
new AlertDialog.Builder(context)
|
||||
.setTitle(String.format(context.getString(R.string.deleteGenericTitle), pageName))
|
||||
.setMessage(context.getString(R.string.deleteWikiPageMessage, pageName))
|
||||
.setIcon(R.drawable.ic_delete)
|
||||
.setPositiveButton(R.string.menuDeleteText, (dialog, whichButton) -> RetrofitClient
|
||||
.getApiInterface(context).repoDeleteWikiPage(owner, repo, pageName).enqueue(new Callback<>() {
|
||||
|
||||
@Override
|
||||
public void onResponse(@NonNull Call<Void> call, @NonNull Response<Void> response) {
|
||||
|
||||
if(response.isSuccessful()) {
|
||||
updateAdapter(position);
|
||||
Toasty.success(context, context.getString(R.string.wikiPageDeleted));
|
||||
if(getItemCount() == 0) {
|
||||
fragmentWikiBinding.noData.setVisibility(View.VISIBLE);
|
||||
}
|
||||
}
|
||||
else if(response.code() == 403) {
|
||||
Toasty.error(context, context.getString(R.string.authorizeError));
|
||||
}
|
||||
else {
|
||||
Toasty.error(context, context.getString(R.string.genericError));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(@NonNull Call<Void> call, @NonNull Throwable t) {
|
||||
|
||||
Toasty.error(context, context.getString(R.string.genericError));
|
||||
}
|
||||
}))
|
||||
.setNeutralButton(R.string.cancelButton, null).show();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ public class WikiFragment extends Fragment {
|
||||
|
||||
wikiViewModel.getWiki(owner, repo, page, resultLimit, getContext(), fragmentWikiBinding).observe(getViewLifecycleOwner(), wikiListMain -> {
|
||||
|
||||
adapter = new WikiListAdapter(wikiListMain, getContext());
|
||||
adapter = new WikiListAdapter(wikiListMain, getContext(), owner, repo, fragmentWikiBinding);
|
||||
adapter.setLoadMoreListener(new WikiListAdapter.OnLoadMoreListener() {
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:paddingTop="6dp"
|
||||
android:paddingBottom="12dp"
|
||||
android:background="?attr/primaryBackgroundColor">
|
||||
|
||||
<androidx.core.widget.NestedScrollView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:orientation="vertical"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/wiki_list_head_frame"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:padding="8dp"
|
||||
android:orientation="vertical">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/bottom_sheet_header"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:gravity="center"
|
||||
android:text="@string/wiki"
|
||||
android:textColor="?attr/primaryTextColor"
|
||||
android:textSize="16sp" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<com.google.android.flexbox.FlexboxLayout
|
||||
android:id="@+id/wiki_list_section"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
app:flexWrap="wrap"
|
||||
app:alignItems="stretch"
|
||||
android:padding="8dp"
|
||||
app:alignContent="stretch" >
|
||||
|
||||
<TextView
|
||||
android:id="@+id/edit_wiki"
|
||||
android:layout_width="98dp"
|
||||
android:layout_height="100dp"
|
||||
android:padding="4dp"
|
||||
android:background="?android:attr/selectableItemBackgroundBorderless"
|
||||
android:gravity="center"
|
||||
app:layout_alignSelf="flex_start"
|
||||
app:drawableTopCompat="@drawable/ic_edit"
|
||||
android:text="@string/menuEditText"
|
||||
android:textColor="?attr/primaryTextColor"
|
||||
android:textSize="16sp" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/delete_wiki"
|
||||
android:layout_width="98dp"
|
||||
android:layout_height="100dp"
|
||||
android:padding="4dp"
|
||||
android:background="?android:attr/selectableItemBackgroundBorderless"
|
||||
android:gravity="center"
|
||||
app:layout_alignSelf="flex_start"
|
||||
app:drawableTopCompat="@drawable/ic_delete"
|
||||
android:text="@string/menuDeleteText"
|
||||
android:textColor="?attr/primaryTextColor"
|
||||
android:textSize="16sp" />
|
||||
|
||||
</com.google.android.flexbox.FlexboxLayout>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</androidx.core.widget.NestedScrollView>
|
||||
|
||||
</LinearLayout>
|
||||
@@ -770,4 +770,6 @@
|
||||
<!-- wiki -->
|
||||
<string name="wiki">Wiki</string>
|
||||
<string name="wikiAuthor"><![CDATA[<b>%1$s</b> updated %2$s]]></string>
|
||||
<string name="deleteWikiPageMessage">Do you really want to delete %1$s?</string>
|
||||
<string name="wikiPageDeleted">Wiki page deleted successfully</string>
|
||||
</resources>
|
||||
|
||||
Reference in New Issue
Block a user