Skip to content

Commit

Permalink
hotfix/CP-8895-story-view-track-opened-not-calling-correctly (#249)
Browse files Browse the repository at this point in the history
* hotfix/CP-8895-story-view-track-opened-not-calling-correctly

When we click on widget except 0th position `track-opened` is not calling

* hotfix/CP-8895-story-view-track-opened-not-calling-correctly

only on click of widget call trackStoryOpened
  • Loading branch information
unnaticleverpush authored Nov 27, 2024
1 parent 6b11f38 commit b34411b
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public class StoryDetailActivity extends Activity implements StoryChangeListener
int measuredWidth = 0;
int measuredHeight = 0;
boolean isHideStoryShareButton = false;
private boolean hasTrackOpenedCalled = false;

public static void launch(Activity activity, ArrayList<Story> stories, int selectedPosition, StoryViewListAdapter storyViewListAdapter,
int closeButtonPosition, int subStoryPosition, String widgetId, int sortToLastIndex, StoryView storyView) {
Expand Down Expand Up @@ -199,9 +200,6 @@ private void handleBundleData(Bundle extras) {
storyView = StoryView.getStoryView();
storyViewOpenedListener = storyView.storyViewOpenedListener;
widget = storyView.getWidget();
if (selectedPosition == 0) {
trackStoryOpened();
}
if (extras.containsKey("stories")) {
stories = (ArrayList<Story>) extras.getSerializable("stories");
loadStoryDetails();
Expand Down Expand Up @@ -339,6 +337,20 @@ public void onStoryReady() {

parentLayout.animate().alpha(1.0f).setDuration(500).start();
webView.animate().alpha(1.0f).setDuration(500).start();

if (!hasTrackOpenedCalled) {
trackStoryOpened();
}
hasTrackOpenedCalled = true;

runOnUiThread(() -> {
if (storyView != null) {
storyView.updateStories(stories);
}
if (storyViewListAdapter != null) {
storyViewListAdapter.updateStories(stories);
}
});
});
} catch (Exception ignored) {
}
Expand Down Expand Up @@ -402,7 +414,6 @@ public void onNavigation(int position) {
selectedPosition = position;
String storyId = stories.get(position).getId();
setStoryOpened(storyId);
trackStoryOpened();
}

private void updateStoryPreferences(String unreadCountMap, String subStoryPositionMap, int unreadCount, SharedPreferences sharedPreferences) {
Expand Down Expand Up @@ -517,19 +528,19 @@ private void setStoryOpened(String storyId) {
try {
SharedPreferences sharedPreferences = SharedPreferencesManager.getSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = sharedPreferences.edit();
String preferencesString = sharedPreferences.getString(CleverPushPreferences.APP_OPENED_STORIES, "");

if (preferencesString.isEmpty()) {
editor.putString(CleverPushPreferences.APP_OPENED_STORIES, storyId).apply();
} else {
if (!preferencesString.contains(storyId)) {
editor.putString(CleverPushPreferences.APP_OPENED_STORIES, preferencesString + "," + storyId).apply();
}
}
editor.apply();
if (widget != null && widget.isGroupStoryCategories()) {
setOpenedForGroupStories();
} else {
String preferencesString = sharedPreferences.getString(CleverPushPreferences.APP_OPENED_STORIES, "");

if (preferencesString.isEmpty()) {
editor.putString(CleverPushPreferences.APP_OPENED_STORIES, storyId).apply();
} else {
if (!preferencesString.contains(storyId)) {
editor.putString(CleverPushPreferences.APP_OPENED_STORIES, preferencesString + "," + storyId).apply();
}
}
editor.apply();
String storyIds = sharedPreferences.getString(CleverPushPreferences.APP_OPENED_STORIES, "");
for (int i = 0; i < stories.size(); i++) {
stories.get(i).setOpened(storyIds.contains(stories.get(i).getId()));
Expand Down Expand Up @@ -599,12 +610,11 @@ public void trackStoryOpened() {

String storyPath = "/story-widget/" + widgetId + "/track-opened";

SharedPreferences sharedPreferences = SharedPreferencesManager.getSharedPreferences(getApplicationContext());
String preferencesString = sharedPreferences.getString(CleverPushPreferences.APP_OPENED_STORIES, "");
String storyId = stories.get(selectedPosition).getId();

ArrayList<String> storyIds = new ArrayList<>();
if (!preferencesString.isEmpty()) {
storyIds = new ArrayList<>(Arrays.asList(preferencesString.split(",")));
if (!storyId.isEmpty()) {
storyIds = new ArrayList<>(Arrays.asList(storyId.split(",")));
}

JSONObject jsonBody = new JSONObject();
Expand Down
60 changes: 32 additions & 28 deletions cleverpush/src/main/java/com/cleverpush/stories/StoryView.java
Original file line number Diff line number Diff line change
Expand Up @@ -300,40 +300,44 @@ public void onGlobalLayout() {
private OnItemClickListener getOnItemClickListener(ArrayList<Story> stories, RecyclerView recyclerView) {
return position -> {
try {
ActivityLifecycleListener.currentActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
SharedPreferences sharedPreferences = SharedPreferencesManager.getSharedPreferences(context);
SharedPreferences.Editor editor = sharedPreferences.edit();
String preferencesString = sharedPreferences.getString(CleverPushPreferences.APP_OPENED_STORIES, "");

if (sortToLastIndex == 1) {
ArrayList<Story> categorizeStories = categorizeStories(stories);
stories.clear();
stories.addAll(categorizeStories);
}
if (ActivityLifecycleListener.currentActivity != null) {
ActivityLifecycleListener.currentActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
SharedPreferences sharedPreferences = SharedPreferencesManager.getSharedPreferences(context);
SharedPreferences.Editor editor = sharedPreferences.edit();
String preferencesString = sharedPreferences.getString(CleverPushPreferences.APP_OPENED_STORIES, "");

if (sortToLastIndex == 1) {
ArrayList<Story> categorizeStories = categorizeStories(stories);
stories.clear();
stories.addAll(categorizeStories);
}

String storyId = stories.get(position).getId();
if (preferencesString.isEmpty()) {
editor.putString(CleverPushPreferences.APP_OPENED_STORIES, storyId).apply();
} else {
if (!preferencesString.contains(storyId)) {
editor.putString(CleverPushPreferences.APP_OPENED_STORIES, preferencesString + "," + storyId).apply();
String storyId = stories.get(position).getId();
if (preferencesString.isEmpty()) {
editor.putString(CleverPushPreferences.APP_OPENED_STORIES, storyId).apply();
} else {
if (!preferencesString.contains(storyId)) {
editor.putString(CleverPushPreferences.APP_OPENED_STORIES, preferencesString + "," + storyId).apply();
}
}
}

int subStoryIndex = calculateSubStoryIndex(sharedPreferences, stories.get(position));
int subStoryIndex = calculateSubStoryIndex(sharedPreferences, stories.get(position));

int closeButtonPosition = attrArray.getInt(R.styleable.StoryView_close_button_position, 0);
int closeButtonPosition = attrArray.getInt(R.styleable.StoryView_close_button_position, 0);

if (widget != null && !widget.isGroupStoryCategories()) {
stories.get(position).setOpened(true);
if (widget != null && !widget.isGroupStoryCategories()) {
stories.get(position).setOpened(true);
}
StoryDetailActivity.launch(ActivityLifecycleListener.currentActivity, stories, position, storyViewListAdapter,
closeButtonPosition, subStoryIndex, widgetId, sortToLastIndex, StoryView.this);
recyclerView.smoothScrollToPosition(position);
}
StoryDetailActivity.launch(ActivityLifecycleListener.currentActivity, stories, position, storyViewListAdapter,
closeButtonPosition, subStoryIndex, widgetId, sortToLastIndex, StoryView.this);
recyclerView.smoothScrollToPosition(position);
}
});
});
} else {
Logger.i(TAG, "getOnItemClickListener: ActivityLifecycleListener.currentActivity is null");
}
} catch (Exception e) {
Logger.e(TAG, "Error getOnItemClickListener of StoryView.", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -606,42 +606,46 @@ public int getItemCount() {

private void loadImage(int position, ImageView image, boolean isDarkModeEnabled) {
try {
ActivityLifecycleListener.currentActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
String widgetUrl = "";
if (isDarkModeEnabled) {
widgetUrl = stories.get(position).getContent().getPreview().getWidgetDarkSrc();
} else {
widgetUrl = stories.get(position).getContent().getPreview().getWidgetSrc();
}
String posterPortraitUrl = stories.get(position).getContent().getPreview().getPosterPortraitSrc();

RequestOptions options = new RequestOptions()
.fitCenter()
.placeholder(R.drawable.ic_story_placeholder)
.error(R.drawable.ic_story_placeholder)
.priority(Priority.HIGH);

if (widgetUrl != null && !widgetUrl.isEmpty()) {
Glide.with(context)
.load(widgetUrl)
.apply(options)
.error(
Glide.with(context)
.load(posterPortraitUrl) // Fallback to posterPortraitUrl if widgetUrl fails
.apply(options)
.error(R.drawable.ic_story_placeholder) // Final fallback placeholder
)
.into(image);
} else {
Glide.with(context)
.load(posterPortraitUrl)
.apply(options)
.into(image);
if (ActivityLifecycleListener.currentActivity != null) {
ActivityLifecycleListener.currentActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
String widgetUrl = "";
if (isDarkModeEnabled) {
widgetUrl = stories.get(position).getContent().getPreview().getWidgetDarkSrc();
} else {
widgetUrl = stories.get(position).getContent().getPreview().getWidgetSrc();
}
String posterPortraitUrl = stories.get(position).getContent().getPreview().getPosterPortraitSrc();

RequestOptions options = new RequestOptions()
.fitCenter()
.placeholder(R.drawable.ic_story_placeholder)
.error(R.drawable.ic_story_placeholder)
.priority(Priority.HIGH);

if (widgetUrl != null && !widgetUrl.isEmpty()) {
Glide.with(context)
.load(widgetUrl)
.apply(options)
.error(
Glide.with(context)
.load(posterPortraitUrl) // Fallback to posterPortraitUrl if widgetUrl fails
.apply(options)
.error(R.drawable.ic_story_placeholder) // Final fallback placeholder
)
.into(image);
} else {
Glide.with(context)
.load(posterPortraitUrl)
.apply(options)
.into(image);
}
}
}
});
});
} else {
Logger.i(TAG, "loadImage: ActivityLifecycleListener.currentActivity is null");
}
} catch (Exception exception) {
Logger.e(TAG, "Error while loading image in StoryViewListAdapter", exception);
}
Expand Down

0 comments on commit b34411b

Please sign in to comment.