How to implement Rate It feature in Android App

前端 未结 14 1380
囚心锁ツ
囚心锁ツ 2020-12-02 04:19

I am developing an Android App. In which everything is working right. My app is ready to launch. But there I need to implement one more feature. I need to display a popup wh

14条回答
  •  半阙折子戏
    2020-12-02 05:02

    Kotlin version of Raghav Sood's answer

    Rater.kt

        class Rater {
          companion object {
            private const val APP_TITLE = "App Name"
            private const val APP_NAME = "com.example.name"
    
            private const val RATER_KEY = "rater_key"
            private const val LAUNCH_COUNTER_KEY = "launch_counter_key"
            private const val DO_NOT_SHOW_AGAIN_KEY = "do_not_show_again_key"
            private const val FIRST_LAUNCH_KEY = "first_launch_key"
    
            private const val DAYS_UNTIL_PROMPT: Int = 3
            private const val LAUNCHES_UNTIL_PROMPT: Int = 3
    
            fun start(mContext: Context) {
                val prefs: SharedPreferences = mContext.getSharedPreferences(RATER_KEY, 0)
                if (prefs.getBoolean(DO_NOT_SHOW_AGAIN_KEY, false)) {
                    return
                }
    
                val editor: Editor = prefs.edit()
    
                val launchesCounter: Long = prefs.getLong(LAUNCH_COUNTER_KEY, 0) + 1;
                editor.putLong(LAUNCH_COUNTER_KEY, launchesCounter)
    
                var firstLaunch: Long = prefs.getLong(FIRST_LAUNCH_KEY, 0)
                if (firstLaunch == 0L) {
                    firstLaunch = System.currentTimeMillis()
                    editor.putLong(FIRST_LAUNCH_KEY, firstLaunch)
                }
    
                if (launchesCounter >= LAUNCHES_UNTIL_PROMPT) {
                    if (System.currentTimeMillis() >= firstLaunch +
                        (DAYS_UNTIL_PROMPT * 24 * 60 * 60 * 1000)
                    ) {
                        showRateDialog(mContext, editor)
                    }
                }
    
                editor.apply()
            }
    
            fun showRateDialog(mContext: Context, editor: Editor) {
                Dialog(mContext).apply {
                    setTitle("Rate $APP_TITLE")
    
                    val ll = LinearLayout(mContext)
                    ll.orientation = LinearLayout.VERTICAL
    
                    TextView(mContext).apply {
                        text =
                            "If you enjoy using $APP_TITLE, please take a moment to rate it. Thanks for your support!"
    
                        width = 240
                        setPadding(4, 0, 4, 10)
                        ll.addView(this)
                    }
    
                    Button(mContext).apply {
                        text = "Rate $APP_TITLE"
                        setOnClickListener {
                            mContext.startActivity(
                                Intent(
                                    Intent.ACTION_VIEW,
                                    Uri.parse("market://details?id=$APP_NAME")
                                )
                            );
                            dismiss()
                        }
                        ll.addView(this)
                    }
    
                    Button(mContext).apply {
                        text = "Remind me later"
                        setOnClickListener {
                            dismiss()
                        };
                        ll.addView(this)
                    }
    
                    Button(mContext).apply {
                        text = "No, thanks"
                        setOnClickListener {
                            editor.putBoolean(DO_NOT_SHOW_AGAIN_KEY, true);
                            editor.commit()
                            dismiss()
                        };
                        ll.addView(this)
                    }
    
                    setContentView(ll)
                    show()
                }
            }
        }
    }
    

    Optimized answer

    Rater.kt

    class Rater {
        companion object {
            fun start(context: Context) {
                val prefs: SharedPreferences = context.getSharedPreferences(RATER_KEY, 0)
                if (prefs.getBoolean(DO_NOT_SHOW_AGAIN_KEY, false)) {
                    return
                }
    
                val editor: Editor = prefs.edit()
    
                val launchesCounter: Long = prefs.getLong(LAUNCH_COUNTER_KEY, 0) + 1;
                editor.putLong(LAUNCH_COUNTER_KEY, launchesCounter)
    
                var firstLaunch: Long = prefs.getLong(FIRST_LAUNCH_KEY, 0)
                if (firstLaunch == 0L) {
                    firstLaunch = System.currentTimeMillis()
                    editor.putLong(FIRST_LAUNCH_KEY, firstLaunch)
                }
    
                if (launchesCounter >= LAUNCHES_UNTIL_PROMPT) {
                    if (System.currentTimeMillis() >= firstLaunch +
                        (DAYS_UNTIL_PROMPT * 24 * 60 * 60 * 1000)
                    ) {
                        showRateDialog(context, editor)
                    }
                }
    
                editor.apply()
            }
    
            fun showRateDialog(context: Context, editor: Editor) {
                Dialog(context).apply {
                    setTitle("Rate $APP_TITLE")
                    LinearLayout(context).let { layout ->
                        layout.orientation = LinearLayout.VERTICAL
                        setDescription(context, layout)
                        setPositiveAnswer(context, layout)
                        setNeutralAnswer(context, layout)
                        setNegativeAnswer(context, editor, layout)
                        setContentView(layout)
                        show()       
                    }
                }
            }
    
            private fun setDescription(context: Context, layout: LinearLayout) {
                TextView(context).apply {
                    text = context.getString(R.string.rate_description, APP_TITLE)
                    width = 240
                    setPadding(4, 0, 4, 10)
                    layout.addView(this)
                }
            }
    
            private fun Dialog.setPositiveAnswer(
                context: Context,
                layout: LinearLayout
            ) {
                Button(context).apply {
                    text = context.getString(R.string.rate_now)
                    setOnClickListener {
                        context.startActivity(
                            Intent(
                                Intent.ACTION_VIEW,
                                Uri.parse(context.getString(R.string.market_uri, APP_NAME))
                            )
                        );
                        dismiss()
                    }
                    layout.addView(this)
                }
            }
    
            private fun Dialog.setNeutralAnswer(
                context: Context,
                layout: LinearLayout
            ) {
                Button(context).apply {
                    text = context.getString(R.string.remind_later)
                    setOnClickListener {
                        dismiss()
                    };
                    layout.addView(this)
                }
            }
    
            private fun Dialog.setNegativeAnswer(
                context: Context,
                editor: Editor,
                layout: LinearLayout
            ) {
                Button(context).apply {
                    text = context.getString(R.string.no_thanks)
                    setOnClickListener {
                        editor.putBoolean(DO_NOT_SHOW_AGAIN_KEY, true);
                        editor.commit()
                        dismiss()
                    };
                    layout.addView(this)
                }
            }
        }
    }
    

    Constants.kt

    object Constants {
    
        const val APP_TITLE = "App Name"
        const val APP_NAME = "com.example.name"
    
        const val RATER_KEY = "rater_key"
        const val LAUNCH_COUNTER_KEY = "launch_counter_key"
        const val DO_NOT_SHOW_AGAIN_KEY = "do_not_show_again_key"
        const val FIRST_LAUNCH_KEY = "first_launch_key"
    
        const val DAYS_UNTIL_PROMPT: Int = 3
        const val LAUNCHES_UNTIL_PROMPT: Int = 3
    
    }
    

    strings.xml

    
        If you enjoy using %1$s, please take a moment to rate it. Thanks for your support!
        Rate now
        No, thanks
        Remind me later
        market://details?id=%1$s
    
    

提交回复
热议问题