Conversation
Initialize the OpenWeb SDK and configure Conversation in your Android app.
The OpenWeb Android SDK provides two approaches to create a fluid conversation experience for your app. Each is listed in the following table.
Approach | Description |
---|---|
Pre-Conversation | A preview of comments at the end of an article to encourage engagement with a Conversation. |
Full Conversation | A full-page Conversation experience that initiates when the user triggers a CTA button. |
Pre-Conversation
Using interactive comment features, Pre-Conversation enables users to easily scan and digest comments in their feed:
- Preview of comments from the Conversation in a Pre-Conversation fragment
- Textbox to enter comments
- Button to show all comments
If a user clicks on the text box, the comment creation screen appears which enables the user to type a comment. If a user clicks the button to see more comments, the SDK opens the Activity
displaying all comments from the Conversation.
The OpenWeb Android SDK offers two approaches to add a Pre-Conversation:
Pre-Conversation as a Fragment
This method allows you to implement a Pre-Conversation fragment.
Use the following steps to initialize the SDK and configure the Conversation:
- In
onCreate()
of an article activity, add the Pre-Conversation fragment to theActivity
.
SpotIm.getPreConversationFragment(CONVERSATION_ID, object :
SpotCallback<Fragment> {
override fun onSuccess(fragment: Fragment) {
//do Something ...
}
override fun onFailure(exception: SpotException) {
//do Something ...
}
})
Pre-Conversation as a RecyclerView item
This approach permits you to implement a Conversation as the last item in a RecyclerView.Adapter
.
Use the following steps to initialize the SDK and configure the Conversation:
- Create and add item_spotim_fragment_container.xml to your project.
<?xml version="1.0" encoding="utf-8"?>
<spotIm.common.helpers.SPFragmentConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</spotIm.common.helpers.SPFragmentConstraintLayout>
- In the article
RecyclerView.Adapter
, define an instance variable to hold the fragment.
private var spotImFragment: Fragment? = null
private var fragmentManger: FragmentManager? = null
- Create a method to set the
spotImFragment
and update the data set.
fun setSpotImFragment(spotImFragment: Fragment, fragmentManger: FragmentManager = null) {
this.spotImFragment = spotImFragment
this.fragmentManger = fragmentManger
notifyDataSetChanged()
}
- Add
getItemCount()
.
override fun getItemCount(): Int {
return if (this.spotImFragment != null) {
originalRecyclerViewSize + 1
} else {
originalRecyclerViewSize
}
}
- Add
getItemViewType()
. This method returns a unique item type for the lastRecyclerView
item.
override fun getItemViewType(position: Int): Int {
return if (position < originalRecyclerViewSize) {
YOUR_ITEM_TYPE
} else { // Last item
SPOT_IM_ITEM_TYPE
}
}
- Add
onCreateViewHolder()
method.
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val inflater = LayoutInflater.from(parent.context)
// your implementation ...
if (viewType == SPOT_IM_ITEM_TYPE) {
val fragmentContainerView = inflater.inflate(R.layout.item_spotim_fragment_container, parent, false) as SPFragmentConstraintLayout
val fragmentManager = this.fragmentManger ?: (parent.context as AppCompatActivity).supportFragmentManager
fragmentContainerView.fragmentManager = fragmentManager
fragmentContainerView.spotImFragment = this.spotImFragment!!
return object : RecyclerView.ViewHolder(fragmentContainerView) {}
}
}
- Add
onViewAttachedToWindow()
.
override fun onViewAttachedToWindow(holder: RecyclerView.ViewHolder) {
if (holder.itemViewType == SPOT_IM_ITEM_TYPE){
val fragmentContainerView = holder.itemView as SPFragmentConstraintLayout
fragmentContainerView.onViewAttachedToWindow()
}
super.onViewAttachedToWindow(holder)
}
- In
onCreate()
of an article activity, get the Pre-Conversation fragment and callsetSpotImFragment()
.If
RecycleView
is embedded within a fragment, setchildFragmentManager
to avoid using the ActivityFragmentManager
.
SpotIm.getPreConversationFragment(CONVERSATION_ID, object :
SpotCallback<Fragment> {
override fun onSuccess(fragment: Fragment) {
yourAdapter.setSpotImFragment(fragment)
//within a fragment
//yourAdapter.setSpotImFragment(fragment, childFragmentManager)
}
override fun onFailure(exception: SpotException) {
//do Something ...
}
})
Full RecyclerView.Adapter code
The following code sample shows steps 2-7 together.
private var spotImFragment: Fragment? = null
private var fragmentManger: FragmentManager? = null
fun setSpotImFragment(spotImFragment: Fragment, fragmentManger: FragmentManager = null) {
this.spotImFragment = spotImFragment
this.fragmentManger = fragmentManger
notifyDataSetChanged()
}
override fun getItemCount(): Int {
return if (this.spotImFragment != null) {
originalRecyclerViewSize + 1
} else {
originalRecyclerViewSize
}
}
override fun getItemViewType(position: Int): Int {
return if (position < originalRecyclerViewSize) {
YOUR_ITEM_TYPE
} else { // Last item
SPOT_IM_ITEM_TYPE
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val inflater = LayoutInflater.from(parent.context)
// your implementation ...
if (viewType == SPOT_IM_ITEM_TYPE) {
val fragmentContainerView = inflater.inflate(R.layout.item_spotim_fragment_container, parent, false) as SPFragmentConstraintLayout
val fragmentManager = this.fragmentManger ?: (parent.context as AppCompatActivity).supportFragmentManager
fragmentContainerView.fragmentManager = fragmentManager
fragmentContainerView.spotImFragment = this.spotImFragment!!
return object : RecyclerView.ViewHolder(fragmentContainerView) {}
}
}
override fun onViewAttachedToWindow(holder: RecyclerView.ViewHolder) {
if (holder.itemViewType == SPOT_IM_ITEM_TYPE){
val fragmentContainerView = holder.itemView as SPFragmentConstraintLayout
fragmentContainerView.onViewAttachedToWindow()
}
super.onViewAttachedToWindow(holder)
}
Full Conversation
This approach allows you to open a full Conversation screen directly without displaying a Pre-Conversation either as an Intent
or a Fragment
.
Full Conversation as a Fragment
- In
onCreate()
of an article activity, add the full Conversation to theFragment
.
SpotIm.getConversationFragment(CONVERSATION_ID, object :
SpotCallback<Fragment> {
override fun onSuccess(fragment: Fragment) {
// Show the full conversation fragment
}
override fun onFailure(exception: SpotException) {
// Handle error here
}
})
Full Conversation as an Intent
- In
onCreate()
of an article activity, add the full Conversation to theIntent
.
SpotIm.getConversationIntent(context, CONVERSATION_ID, object :
SpotCallback<Intent> {
override fun onSuccess(intent: Intent) {
startActivity(intent)
}
override fun onFailure(exception: SpotException) {
//Handle error here
}
})
- Monetize the Conversation.
Updated about 1 year ago