Skip to content

Commit

Permalink
Consent form activity now styleable
Browse files Browse the repository at this point in the history
  • Loading branch information
SenNeonoveNoci committed Feb 19, 2019
1 parent cb88386 commit e7a1a69
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 23 deletions.
3 changes: 3 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,13 @@ class MainActivity : AppCompatActivity(), ConsentResultsListener {

private fun handleStartConsentActivity(consentFormData: ConsentFormData) {
start_activity.setOnClickListener {
App.consentSDK.startConsentFormActivity(this, consentFormData,
CONSENT_REQUEST_CODE
)
App.consentSDK.startConsentFormActivity(this, consentFormData, CONSENT_REQUEST_CODE, R.style.ActivityStyle)
}
}

private fun handleShowFragmentDialog(consentFormData: ConsentFormData) {
show_dialog_fragment.setOnClickListener {
App.consentSDK.showConsentFormDialogFragment(this, consentFormData)
App.consentSDK.showConsentFormDialogFragment(this, consentFormData, R.style.DialogStyle)
}
}

Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@
<item name="colorAccent">#85318F</item>
</style>

<style name="ActivityStyle" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorAccent">#85318F</item>
</style>

</resources>
47 changes: 44 additions & 3 deletions consentsdk/src/main/java/com/smartlook/consentsdk/ConsentSDK.kt
Original file line number Diff line number Diff line change
Expand Up @@ -92,30 +92,71 @@ class ConsentSDK(context: Context) : ContextWrapper(context) {
* @param activity Calling Activity reference. This Activity needs to implement ConsentResultsListener.
* @param consentFormData Data object containing all needed info display the form (@see ConsentFormData).
*/
fun showConsentFormDialogFragment(activity: FragmentActivity, consentFormData: ConsentFormData) {
fun showConsentFormDialogFragment(activity: FragmentActivity,
consentFormData: ConsentFormData) {
ConsentFormDialogFragment.show(activity, consentFormData)
}

/**
* Display consent form on DialogFragment.
*
* @param activity Calling Activity reference. This Activity needs to implement ConsentResultsListener.
* @param consentFormData Data object containing all needed info display the form (@see ConsentFormData).
*/
fun showConsentFormDialogFragment(activity: FragmentActivity,
consentFormData: ConsentFormData,
styleId: Int) {
ConsentFormDialogFragment.show(activity, consentFormData, styleId)
}

/**
* Display consent form on DialogFragment.
*
* @param fragment Calling Fragment reference. This Fragment needs to implement ConsentResultsListener.
* @param consentFormData Data object containing all needed info display the form (@see ConsentFormData).
*/
fun showConsentFormDialogFragment(fragment: Fragment, consentFormData: ConsentFormData) {
fun showConsentFormDialogFragment(fragment: Fragment,
consentFormData: ConsentFormData) {
ConsentFormDialogFragment.show(fragment, consentFormData)
}

/**
* Display consent form on DialogFragment.
*
* @param fragment Calling Fragment reference. This Fragment needs to implement ConsentResultsListener.
* @param consentFormData Data object containing all needed info display the form (@see ConsentFormData).
*/
fun showConsentFormDialogFragment(fragment: Fragment,
consentFormData: ConsentFormData,
styleId: Int) {
ConsentFormDialogFragment.show(fragment, consentFormData, styleId)
}

/**
* Display consent form Activity.
*
* @param activity Calling Activity reference. This Activity needs to implement onActivityResult to get result.
* @param consentFormData Data object containing all needed info display the form (@see ConsentFormData).
* @param requestCode Unique request code used in onActivityResult to determine corresponding result.
*/
fun startConsentFormActivity(activity: Activity, consentFormData: ConsentFormData, requestCode: Int) =
fun startConsentFormActivity(activity: Activity,
consentFormData: ConsentFormData,
requestCode: Int) =
ConsentFormActivity.start(activity, consentFormData, requestCode)

/**
* Display consent form Activity.
*
* @param activity Calling Activity reference. This Activity needs to implement onActivityResult to get result.
* @param consentFormData Data object containing all needed info display the form (@see ConsentFormData).
* @param requestCode Unique request code used in onActivityResult to determine corresponding result.
*/
fun startConsentFormActivity(activity: Activity,
consentFormData: ConsentFormData,
requestCode: Int,
styleId: Int) =
ConsentFormActivity.start(activity, consentFormData, requestCode, styleId)

/**
* Parse out consentResults HashMap<consentKey, grantResult> from activity result.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ package com.smartlook.consentsdk.helpers
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.View


object UtilsHelper {

const val STYLE_ID_EXTRA = "style_id_extra"

fun hideViewIfNull(nullableObject: Any?, view: View) {
view.visibility = if (nullableObject == null) {
View.GONE
Expand All @@ -29,4 +32,12 @@ object UtilsHelper {
return px / (context.resources.displayMetrics.densityDpi.toFloat() / DisplayMetrics.DENSITY_DEFAULT)
}

fun getStyleId(arguments: Bundle?): Int? {
val styleId = arguments?.getInt(STYLE_ID_EXTRA)
return if (styleId == View.NO_ID) {
null
} else {
styleId
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,34 @@ package com.smartlook.consentsdk.ui.consent.activity
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.support.v4.app.DialogFragment
import android.support.v7.app.AppCompatActivity
import android.view.View
import com.smartlook.consentsdk.R
import com.smartlook.consentsdk.data.ConsentFormData
import com.smartlook.consentsdk.helpers.ConsentHelper
import com.smartlook.consentsdk.helpers.UtilsHelper
import com.smartlook.consentsdk.ui.consent.ConsentBase
import com.smartlook.consentsdk.ui.consent.dialog.ConsentFormDialogFragment
import kotlinx.android.synthetic.main.consent_dialog.*
import java.security.InvalidParameterException

class ConsentFormActivity : AppCompatActivity() {

companion object {
fun start(activity: Activity, consentFormData: ConsentFormData, requestCode: Int) {
activity.startActivityForResult(
Intent(activity, ConsentFormActivity::class.java).apply { putExtras(consentFormData.createBundle()) },
requestCode)

fun start(activity: Activity,
consentFormData: ConsentFormData,
requestCode: Int,
styleId: Int? = null) {

val intent = Intent(activity, ConsentFormActivity::class.java).apply {
putExtras(consentFormData.createBundle().apply {
putInt(UtilsHelper.STYLE_ID_EXTRA, styleId ?: View.NO_ID)
})
}

activity.startActivityForResult(intent, requestCode)
}
}

Expand All @@ -26,16 +39,23 @@ class ConsentFormActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val styleId = UtilsHelper.getStyleId(intent.extras)
if (styleId != null) {
setTheme(styleId)
}

setContentView(R.layout.consent_activity)
hideToolbar()

consentFormData = ConsentFormData.constructFromBundle(intent.extras) ?: throw InvalidParameterException()
consentFormData = ConsentFormData.constructFromBundle(intent.extras)
?: throw InvalidParameterException()

consentBase = ConsentBase(
consentFormData,
root,
createResultListener(),
ConsentHelper.restoreConsentResults(savedInstanceState))
consentFormData,
root,
createResultListener(),
ConsentHelper.restoreConsentResults(savedInstanceState))

consentBase.displayConsent()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import android.view.ViewGroup
import com.smartlook.consentsdk.R
import com.smartlook.consentsdk.data.ConsentFormData
import com.smartlook.consentsdk.helpers.ConsentHelper
import com.smartlook.consentsdk.helpers.UtilsHelper
import com.smartlook.consentsdk.listeners.ConsentResultsListener
import com.smartlook.consentsdk.ui.consent.ConsentBase
import kotlinx.android.synthetic.main.consent_dialog.*
Expand All @@ -25,20 +26,22 @@ class ConsentFormDialogFragment : DialogFragment() {
private const val CONSENT_DIALOG_FRAGMENT_TAG = "consent_dialog_fragment"
private const val CONSENT_DIALOG_FRAGMENT_CALLER_TYPE = "consent_dialog_fragment_caller_type"

fun show(activity: FragmentActivity, consentFormData: ConsentFormData) {
fun show(activity: FragmentActivity, consentFormData: ConsentFormData, styleId: Int? = null) {
val consentDialogFragment = ConsentFormDialogFragment().apply {
arguments = consentFormData.createBundle().apply {
putInt(CONSENT_DIALOG_FRAGMENT_CALLER_TYPE, CALLED_FROM_ACTIVITY)
putInt(UtilsHelper.STYLE_ID_EXTRA, styleId ?: View.NO_ID)
}
}

consentDialogFragment.show(activity.supportFragmentManager, CONSENT_DIALOG_FRAGMENT_TAG)
}

fun show(fragment: Fragment, consentFormData: ConsentFormData) {
fun show(fragment: Fragment, consentFormData: ConsentFormData, styleId: Int? = null) {
val consentDialogFragment = ConsentFormDialogFragment().apply {
arguments = consentFormData.createBundle().apply {
putInt(CONSENT_DIALOG_FRAGMENT_CALLER_TYPE, CALLED_FROM_FRAGMENT)
putInt(UtilsHelper.STYLE_ID_EXTRA, styleId ?: View.NO_ID)
}
}

Expand All @@ -52,6 +55,11 @@ class ConsentFormDialogFragment : DialogFragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val styleId = UtilsHelper.getStyleId(arguments)
if (styleId != null) {
setStyle(DialogFragment.STYLE_NORMAL, styleId)
}

isCancelable = false
registerListener(arguments?.getInt(CONSENT_DIALOG_FRAGMENT_CALLER_TYPE))
}
Expand All @@ -66,10 +74,10 @@ class ConsentFormDialogFragment : DialogFragment() {
val consentFormData = ConsentFormData.constructFromBundle(arguments) ?: throw InvalidParameterException()

consentBase = ConsentBase(
consentFormData,
root,
createResultListener(),
ConsentHelper.restoreConsentResults(savedInstanceState)
consentFormData,
root,
createResultListener(),
ConsentHelper.restoreConsentResults(savedInstanceState)
)

consentBase.displayConsent()
Expand Down

0 comments on commit e7a1a69

Please sign in to comment.