简体   繁体   中英

How can I DRY my code in the context below(android)

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
                }
            });



}
}

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);  
                }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM