Skip to content

Commit

Permalink
Merge pull request #66 from rees46/bug/initializing-stories-manager
Browse files Browse the repository at this point in the history
feat: Added checking initialization stories manager
  • Loading branch information
TorinAsakura authored Sep 25, 2024
2 parents c20b3de + 685b26a commit 36b7a81
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 141 deletions.
2 changes: 1 addition & 1 deletion personalization-sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
id 'kotlin-kapt'
}

version='2.0.28'
version='2.0.29'

android {
compileSdkVersion 34
Expand Down
70 changes: 42 additions & 28 deletions personalization-sdk/src/main/kotlin/com/personalization/SDK.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.personalization.sdk.domain.usecases.network.SendNetworkMethodUseCase
import com.personalization.sdk.domain.usecases.notification.GetAllNotificationsUseCase
import com.personalization.sdk.domain.usecases.preferences.GetPreferencesValueUseCase
import com.personalization.sdk.domain.usecases.preferences.InitPreferencesUseCase
import com.personalization.sdk.domain.usecases.recommendation.SetRecommendedByUseCase
import com.personalization.sdk.domain.usecases.userSettings.GetUserSettingsValueUseCase
import com.personalization.sdk.domain.usecases.userSettings.InitUserSettingsUseCase
import com.personalization.stories.StoriesManager
Expand Down Expand Up @@ -80,9 +81,13 @@ open class SDK {
@Inject
lateinit var sendNetworkMethodUseCase: SendNetworkMethodUseCase

@Inject
lateinit var setRecommendedByUseCase: SetRecommendedByUseCase

@Inject
lateinit var getAllNotificationsUseCase: GetAllNotificationsUseCase


/**
* @param shopId Shop key
*/
Expand Down Expand Up @@ -132,6 +137,43 @@ open class SDK {
storiesManager.initialize(storiesView)
}

/**
* @param listener
*/
fun stories(code: String, listener: OnApiCallbackListener) {
storiesManager.requestStories(code, listener)
}

/**
* Show stories block by code
*
* @param code Stories block code
*/
fun showStories(code: String) {
storiesManager.showStories(context.mainLooper, code)
}

/**
* Triggers a story event
*
* @param event Event
* @param code Stories block code
* @param storyId Story ID
* @param slideId Slide ID
*/
fun trackStory(event: String, code: String, storyId: Int, slideId: String) {
if (::storiesManager.isInitialized) {
storiesManager.trackStory(
event = event,
code = code,
storyId = storyId,
slideId = slideId
)
} else {
Log.i(TAG, "storiesManager is not initialized")
}
}

/**
* Update profile data
*
Expand Down Expand Up @@ -670,34 +712,6 @@ open class SDK {
}
}

/**
* @param listener
*/
fun stories(code: String, listener: OnApiCallbackListener) {
storiesManager.requestStories(code, listener)
}

/**
* Show stories block by code
*
* @param code Stories block code
*/
fun showStories(code: String) {
storiesManager.showStories(context.mainLooper, code)
}

/**
* Triggers a story event
*
* @param event Event
* @param code Stories block code
* @param storyId Story ID
* @param slideId Slide ID
*/
fun trackStory(event: String, code: String, storyId: Int, slideId: String) {
storiesManager.trackStory(event, code, storyId, slideId)
}

/**
* @param data from data notification
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,47 @@ import javax.inject.Inject

class StoriesManager @Inject constructor(
val setRecommendedByUseCase: SetRecommendedByUseCase,
private val sendNetworkMethodUseCase: SendNetworkMethodUseCase
val sendNetworkMethodUseCase: SendNetworkMethodUseCase
) {

private lateinit var storiesView: StoriesView

internal fun initialize(storiesView: StoriesView) {
Log.d("StoriesManager", "initialize called")
this.storiesView = storiesView

updateStories()
}

internal fun showStories(looper: Looper, code: String) {
requestStories(code, object : OnApiCallbackListener() {
override fun onSuccess(response: JSONObject?) {
response?.let {
Log.d("stories", response.toString())
try {
val stories = getStories(response)

if(stories.isEmpty()) return

resetStoriesStartPositions(stories)

showStories(looper, stories)
} catch (e: JSONException) {
Log.e(SDK.TAG, e.message, e)
requestStories(
code = code,
listener = object : OnApiCallbackListener() {
override fun onSuccess(response: JSONObject?) {
response?.let {
Log.d("stories", response.toString())
try {
val stories = getStories(response)

if (stories.isEmpty()) return

resetStoriesStartPositions(stories)

showStories(looper, stories)
} catch (e: JSONException) {
Log.e(SDK.TAG, e.message, e)
}
}
}
}
})
)
}

internal fun requestStories(code: String, listener: OnApiCallbackListener) {
sendNetworkMethodUseCase.getAsync(String.format(REQUEST_STORIES_METHOD, code), JSONObject(), listener)
sendNetworkMethodUseCase.getAsync(
method = String.format(REQUEST_STORIES_METHOD, code),
params = JSONObject(),
listener = listener
)
}

/**
Expand Down Expand Up @@ -78,19 +85,22 @@ class StoriesManager @Inject constructor(
}

private fun updateStories() {
requestStories(storiesView.code, object : OnApiCallbackListener() {
override fun onSuccess(response: JSONObject?) {
response?.let {
Log.d("stories", response.toString())
try {
val stories = getStories(response)
storiesView.updateStories(stories)
} catch (e: JSONException) {
Log.e(SDK.TAG, e.message, e)
requestStories(
code = storiesView.code,
listener = object : OnApiCallbackListener() {
override fun onSuccess(response: JSONObject?) {
response?.let {
Log.d("stories", response.toString())
try {
val stories = getStories(response)
storiesView.updateStories(stories)
} catch (e: JSONException) {
Log.e(SDK.TAG, e.message, e)
}
}
}
}
})
)
}

private fun getStories(json: JSONObject): List<Story> {
Expand All @@ -114,17 +124,20 @@ class StoriesManager @Inject constructor(
private fun showStories(looper: Looper, stories: List<Story>, startPosition: Int = 0) {
val handler = Handler(looper)
handler.post {
storiesView.showStories(stories, startPosition)
storiesView.showStories(
stories = stories,
startPosition = startPosition
)
}
}

companion object {
private const val TRACK_STORIES_METHOD = "track/stories"
private const val REQUEST_STORIES_METHOD = "stories/%s"
const val TRACK_STORIES_METHOD = "track/stories"
const val REQUEST_STORIES_METHOD = "stories/%s"

private const val EVENT_PARAMS_NAME = "event"
private const val STORY_ID_PARAMS_NAME = "story_id"
private const val SLIDE_ID_PARAMS_NAME = "slide_id"
private const val CODE_PARAMS_NAME = "code"
const val EVENT_PARAMS_NAME = "event"
const val STORY_ID_PARAMS_NAME = "story_id"
const val SLIDE_ID_PARAMS_NAME = "slide_id"
const val CODE_PARAMS_NAME = "code"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,13 @@ class StoriesView : ConstraintLayout, ClickListener {
completeShowStory: () -> Unit = { },
cancelShowStory: () -> Unit = { }
) {
val dialog = StoryDialog(this, stories, startPosition, completeShowStory, cancelShowStory)
val dialog = StoryDialog(
storiesView = this,
stories = stories,
startPosition = startPosition,
completeShowStory = completeShowStory,
cancelShowStory = cancelShowStory
)
dialog.show()
}

Expand Down
Loading

0 comments on commit 36b7a81

Please sign in to comment.