1
0
mirror of https://framagit.org/tom79/fedilab-tube synced 2025-04-04 22:01:04 +02:00

Some changes

This commit is contained in:
Thomas 2021-01-07 17:39:47 +01:00
parent 6d70bd758a
commit b22b21c47a
5 changed files with 109 additions and 36 deletions

View File

@ -27,6 +27,7 @@ import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import com.android.billingclient.api.AcknowledgePurchaseParams;
import com.android.billingclient.api.BillingClient;
import com.android.billingclient.api.BillingClientStateListener;
import com.android.billingclient.api.BillingResult;
@ -43,12 +44,16 @@ import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import app.fedilab.fedilabtube.databinding.ActivityDonationBinding;
import app.fedilab.fedilabtube.fragment.MySubscriptionDonationsFragment;
import app.fedilab.fedilabtube.fragment.DonationsFragment;
import app.fedilab.fedilabtube.helper.Helper;
import es.dmoral.toasty.Toasty;
public class DonationActivity extends AppCompatActivity implements PurchasesUpdatedListener {
@ -79,30 +84,7 @@ public class DonationActivity extends AppCompatActivity implements PurchasesUpda
donationsFragment.initialized(billingClient);
subscriptionDonationsFragment.initialized(billingClient);
Purchase.PurchasesResult purchasesResult = billingClient.queryPurchases(BillingClient.SkuType.SUBS);
List<Purchase> purchases = purchasesResult.getPurchasesList();
List<String> isSubscriptions = new ArrayList<>();
if (purchases != null) {
for (Purchase purchase : purchases) {
try {
JSONObject purchaseJson = new JSONObject(purchase.getOriginalJson());
String productId = purchaseJson.getString("productId");
isSubscriptions.add(productId);
} catch (JSONException e) {
e.printStackTrace();
}
}
SkuDetailsParams.Builder paramsSub = SkuDetailsParams.newBuilder();
paramsSub.setSkusList(isSubscriptions).setType(BillingClient.SkuType.SUBS);
billingClient.querySkuDetailsAsync(paramsSub.build(),
(billingResult2, skuDetailsList) -> mySubscriptionDonationsFragment.initialized(skuDetailsList, billingClient));
} else {
mySubscriptionDonationsFragment.initialized(new ArrayList<>(), billingClient);
}
purchasesResult = billingClient.queryPurchases(BillingClient.SkuType.INAPP);
purchases = purchasesResult.getPurchasesList();
List<Purchase> purchases = queryPurchases();
if (purchases != null) {
for (Purchase purchase : purchases) {
if (!purchase.isAutoRenewing()) {
@ -188,6 +170,34 @@ public class DonationActivity extends AppCompatActivity implements PurchasesUpda
}
private List<Purchase> queryPurchases() {
Purchase.PurchasesResult purchasesResult = billingClient.queryPurchases(BillingClient.SkuType.SUBS);
List<Purchase> purchases = purchasesResult.getPurchasesList();
List<String> isSubscriptions = new ArrayList<>();
HashMap<String, Purchase> map = new HashMap<>();
if (purchases != null) {
for (Purchase purchase : purchases) {
try {
if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) {
JSONObject purchaseJson = new JSONObject(purchase.getOriginalJson());
String productId = purchaseJson.getString("productId");
isSubscriptions.add(productId);
map.put(productId, purchase);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
SkuDetailsParams.Builder paramsSub = SkuDetailsParams.newBuilder();
paramsSub.setSkusList(isSubscriptions).setType(BillingClient.SkuType.SUBS);
billingClient.querySkuDetailsAsync(paramsSub.build(),
(billingResult2, skuDetailsList) -> mySubscriptionDonationsFragment.initialized(skuDetailsList, map, billingClient));
} else {
mySubscriptionDonationsFragment.initialized(new ArrayList<>(), map, billingClient);
}
return purchases;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
@ -210,11 +220,21 @@ public class DonationActivity extends AppCompatActivity implements PurchasesUpda
.build();
ConsumeResponseListener listener = (billingResult1, purchaseToken) -> {
if (billingResult1.getResponseCode() == BillingClient.BillingResponseCode.OK) {
// Handle the success of the consume operation.
}
};
billingClient.consumeAsync(consumeParams, listener);
} else {
if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) {
if (!purchase.isAcknowledged()) {
AcknowledgePurchaseParams acknowledgePurchaseParams =
AcknowledgePurchaseParams.newBuilder()
.setPurchaseToken(purchase.getPurchaseToken())
.build();
billingClient.acknowledgePurchase(acknowledgePurchaseParams, b -> {
});
}
}
queryPurchases();
}
}
message = getString(R.string.donation_succeeded_null);

View File

@ -19,15 +19,26 @@ import android.view.LayoutInflater;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;
import com.android.billingclient.api.BillingClient;
import com.android.billingclient.api.ConsumeParams;
import com.android.billingclient.api.ConsumeResponseListener;
import com.android.billingclient.api.Purchase;
import com.android.billingclient.api.SkuDetails;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.List;
import app.fedilab.fedilabtube.R;
import app.fedilab.fedilabtube.databinding.DrawerMyDonationBinding;
import app.fedilab.fedilabtube.helper.Helper;
import es.dmoral.toasty.Toasty;
public class DonationHistoryAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
@ -35,10 +46,12 @@ public class DonationHistoryAdapter extends RecyclerView.Adapter<RecyclerView.Vi
private final List<SkuDetails> skuDetailsList;
private final BillingClient billingClient;
private Context context;
private final HashMap<String, Purchase> map;
public DonationHistoryAdapter(List<SkuDetails> SkuDetailsList, BillingClient billingClient) {
public DonationHistoryAdapter(List<SkuDetails> SkuDetailsList, HashMap<String, Purchase> map, BillingClient billingClient) {
this.skuDetailsList = SkuDetailsList;
this.billingClient = billingClient;
this.map = map;
}
@ -51,6 +64,7 @@ public class DonationHistoryAdapter extends RecyclerView.Adapter<RecyclerView.Vi
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
final ViewHolder holder = (ViewHolder) viewHolder;
@ -58,8 +72,45 @@ public class DonationHistoryAdapter extends RecyclerView.Adapter<RecyclerView.Vi
holder.binding.productTitle.setText(skuDetails.getTitle());
holder.binding.productName.setText(skuDetails.getDescription());
holder.binding.productInfo.setText(skuDetails.getOriginalPrice());
holder.binding.cancelDonation.setOnClickListener(v -> {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
dialogBuilder.setMessage(R.string.cancel_subscription_confirm);
dialogBuilder.setPositiveButton(R.string.cancel_subscription, (dialog, id) -> {
JSONObject skudetailsJson;
try {
skudetailsJson = new JSONObject(skuDetails.getOriginalJson());
String productId = skudetailsJson.getString("productId");
if (map.containsKey(productId)) {
Purchase purchase = map.get(productId);
if (purchase != null) {
ConsumeParams consumeParams =
ConsumeParams.newBuilder()
.setPurchaseToken(purchase.getPurchaseToken())
.build();
if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) {
ConsumeResponseListener listener = (billingResult1, purchaseToken) -> {
if (billingResult1.getResponseCode() == BillingClient.BillingResponseCode.OK) {
Toasty.success(context, context.getString(R.string.subscription_cancelled), Toasty.LENGTH_LONG).show();
}
skuDetailsList.remove(skuDetails);
notifyDataSetChanged();
};
billingClient.consumeAsync(consumeParams, listener);
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
dialog.dismiss();
});
dialogBuilder.setNegativeButton(R.string.cancel, (dialog, id) -> dialog.dismiss());
AlertDialog alertDialogLogoutAccount = dialogBuilder.create();
alertDialogLogoutAccount.show();
});
}
@ -69,6 +120,7 @@ public class DonationHistoryAdapter extends RecyclerView.Adapter<RecyclerView.Vi
return skuDetailsList.size();
}
static class ViewHolder extends RecyclerView.ViewHolder {
DrawerMyDonationBinding binding;

View File

@ -34,7 +34,6 @@ import java.util.List;
import app.fedilab.fedilabtube.R;
import app.fedilab.fedilabtube.databinding.FragmentDonationsBinding;
import app.fedilab.fedilabtube.drawable.DonationButtonAdapter;
import app.fedilab.fedilabtube.helper.Helper;
public class DonationsFragment extends Fragment {
@ -83,6 +82,9 @@ public class DonationsFragment extends Fragment {
(billingResult, skuDetailsList) -> {
binding.loader.setVisibility(View.GONE);
binding.lvProducts.setVisibility(View.VISIBLE);
if (skuDetailsList != null) {
Collections.sort(skuDetailsList, (obj1, obj2) -> obj1.getPrice().compareTo(obj2.getPrice()));
}
DonationButtonAdapter donationButtonAdapter = new DonationButtonAdapter(skuDetailsList, bc, isSubscriptions);
binding.lvProducts.setAdapter(donationButtonAdapter);
binding.lvProducts.setLayoutManager(new LinearLayoutManager(context));

View File

@ -28,15 +28,11 @@ import com.android.billingclient.api.BillingClient;
import com.android.billingclient.api.Purchase;
import com.android.billingclient.api.SkuDetails;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import app.fedilab.fedilabtube.databinding.FragmentMyDonationsBinding;
import app.fedilab.fedilabtube.drawable.DonationHistoryAdapter;
import app.fedilab.fedilabtube.helper.Helper;
import static app.fedilab.fedilabtube.fragment.DonationsFragment.donations;
public class MySubscriptionDonationsFragment extends Fragment {
@ -55,11 +51,11 @@ public class MySubscriptionDonationsFragment extends Fragment {
return rootView;
}
public void initialized(List<SkuDetails> skuDetailsList, BillingClient bc) {
public void initialized(List<SkuDetails> skuDetailsList, HashMap<String, Purchase> map, BillingClient bc) {
binding.loader.setVisibility(View.GONE);
binding.lvPurchases.setVisibility(View.VISIBLE);
DonationHistoryAdapter donationHistoryAdapter = new DonationHistoryAdapter(skuDetailsList, bc);
DonationHistoryAdapter donationHistoryAdapter = new DonationHistoryAdapter(skuDetailsList, map, bc);
binding.lvPurchases.setAdapter(donationHistoryAdapter);
binding.lvPurchases.setLayoutManager(new LinearLayoutManager(context));

View File

@ -468,5 +468,8 @@
<string name="one_time">One time</string>
<string name="my_subscriptions">My subscription</string>
<string name="month">Month</string>
<string name="subscription_cancelled">Subscription cancelled!</string>
<string name="cancel_subscription">Cancel subscription</string>
<string name="cancel_subscription_confirm">Are you sure, you want to cancel that subscription?</string>
</resources>