81 lines
2.9 KiB
Java
81 lines
2.9 KiB
Java
package app.fedilab.fedilabtube.drawable;
|
|
/* Copyright 2021 Thomas Schneider
|
|
*
|
|
* This file is a part of TubeLab
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
|
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
|
* License, or (at your option) any later version.
|
|
*
|
|
* TubeLab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
* Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License along with TubeLab; if not,
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
import android.content.Context;
|
|
import android.view.LayoutInflater;
|
|
import android.view.ViewGroup;
|
|
|
|
import androidx.annotation.NonNull;
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
import com.anjlab.android.iab.v3.BillingProcessor;
|
|
import com.anjlab.android.iab.v3.SkuDetails;
|
|
|
|
import java.util.List;
|
|
import java.util.Locale;
|
|
|
|
import app.fedilab.fedilabtube.DonationActivity;
|
|
import app.fedilab.fedilabtube.databinding.DrawerDonationBinding;
|
|
import app.fedilab.fedilabtube.helper.Helper;
|
|
|
|
|
|
public class DonationButtonAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
|
|
|
|
private final List<String> items;
|
|
private final BillingProcessor billingProcessor;
|
|
private Context context;
|
|
|
|
public DonationButtonAdapter(List<String> items, BillingProcessor billingProcessor) {
|
|
this.items = items;
|
|
this.billingProcessor = billingProcessor;
|
|
}
|
|
|
|
@NonNull
|
|
@Override
|
|
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
|
context = parent.getContext();
|
|
DrawerDonationBinding itemBinding = DrawerDonationBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
|
|
return new ViewHolder(itemBinding);
|
|
}
|
|
|
|
@Override
|
|
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
|
|
final ViewHolder holder = (ViewHolder) viewHolder;
|
|
String item = items.get(position);
|
|
String product_item = "tubelab_donation_" + item;
|
|
SkuDetails skuDetails = billingProcessor.getPurchaseListingDetails(product_item);
|
|
String currency = skuDetails.currency;
|
|
Double price = skuDetails.priceValue;
|
|
holder.binding.buttonDonation.setText(String.format(Locale.getDefault(), "%.2f %s", price, currency));
|
|
holder.binding.buttonDonation.setOnClickListener(v -> billingProcessor.purchase((DonationActivity) context, product_item));
|
|
}
|
|
|
|
|
|
@Override
|
|
public int getItemCount() {
|
|
return items.size();
|
|
}
|
|
|
|
static class ViewHolder extends RecyclerView.ViewHolder {
|
|
DrawerDonationBinding binding;
|
|
|
|
ViewHolder(DrawerDonationBinding itemView) {
|
|
super(itemView.getRoot());
|
|
binding = itemView;
|
|
}
|
|
}
|
|
}
|