Reputation: 1
I am new to android development and using shared preferences to show ads after a number of buttons clicks. I have several buttons(Like the example below) each with a different intent. What would be the best implementation for my single logic below so that my code is DRY(Don't Repeat Yourself) for all other buttons with different intent. My Adapter class extends RecyclerView
public class ImageAdaptor extends RecyclerView.Adapter<ImageAdaptor.MyViewHolder> {
#####################################
######################################
########################################
public class MyViewHolder extends RecyclerView.ViewHolder {
public MyViewHolder(View v) {
MobileAds.initialize(acontext, "ca-app-pub-3940256099942544~3347511713");
final InterstitialAd mInterstitialAd = new InterstitialAd(acontext);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
mInterstitialAd.loadAd(new AdRequest.Builder().build());
//Here is my working example on first button
//I want to achieve something like this on other clicks with different intents
//I don't want to repeat the logic over and over again
**Button1**.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final ModelStatus modelStatus = arrayList.get(getAdapterPosition());
final Intent intent = new Intent(acontext, Activity1.class);
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
acontext.startActivity(intent);
}
});
SharedPreferences preferences = acontext.getSharedPreferences("PREFRENCE",
Context.MODE_PRIVATE);
int clickCount = preferences.getInt("KEY_CLICK_COUNT", 1);
if (clickCount % 6 == 0) {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
acontext.startActivity(intent);
};
}else {
acontext.startActivity(intent);
}
clickCount++;
preferences.edit().putInt("KEY_CLICK_COUNT", clickCount).apply();
}
});
**Button2**.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Intent intent = new Intent(acontext, Activity2.class);
//Some code with an intent
// I want my logic to go in here for 5 clicks per ad.
// I don't want to repeat myself since i have several such
//buttons with a different intent
}
});
}
}
Upvotes: 0
Views: 70
Reputation: 1018
As easy as pie
public void handleClick(Class destination, int buttonType){
final ModelStatus modelStatus = arrayList.get(getAdapterPosition());
final Intent intent = new Intent(getAppContext(), destination);
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
acontext.startActivity(intent);
}
});
SharedPreferences preferences = acontext.getSharedPreferences("PREFRENCE",
Context.MODE_PRIVATE);
int clickCount = preferences.getInt("KEY_CLICK_COUNT"+buttonType, 1);
if (clickCount % 6 == 0) {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
acontext.startActivity(intent);
};
}else {
acontext.startActivity(intent);
}
clickCount++;
preferences.edit().putInt("KEY_CLICK_COUNT"+buttonType, clickCount).apply();
}
Button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
handleClick(Activity1.class, 1);
}
Button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
handleClick(Whatever.class, 2);
}
Upvotes: 1