drawRect / Instagram_Stories

Inspired by Instagram Stories functionality. This source is similar to Instagram Stories, which is having both image and video support.
MIT License
414 stars 76 forks source link

[question/request] How to open story at a specific snap index? #109

Closed Ludotrico closed 3 years ago

Ludotrico commented 3 years ago

Not sure why this is not implemented as it is vital in story implementation:

example logic:

    func shouldOpenStoryAtIndex() -> Int {
        if let lastPlayedSnapIndex = model.lastPlayedSnapIndex {
            if lastPlayedSnapIndex == (model.snapCount - 1) {
                //Watched entire story, start at beginning
                return 0
            } else {
                //Watched part of story, resume where user left off
                return lastPlayedSnapIndex + 1
            }
        }
        else {
            //User has never watched story
            return 0
        }

    }

When a user views any snap, the lastPlayedSnapIndex is saved on the server. This means that the lastPlayedSnapIndex is saved in a database for each story, for each user. When a user taps on a story, the model has the lastPlayedSnapIndex from the server and thus can easily calculate which snap should be opened using shouldOpenStoryAtIndex().

In addition, since each story has the lastPlayedSnapIndex, when the next story is to be displayed (either because the user swiped or the timer ran out), the same logic should be applied from shouldOpenStoryAtIndex().

This is exactly how Instagram, Snapchat, Facebook, Whatsapp, etc operates. The user should never watch the same snap twice unless he explicitly taps backwards. The logic I just explained allows for story content to be always fresh for the user. The current implementation does not support a persisting lastPlayedSnapIndex.

I do not think that there is much trouble implementing this if I understood the codebase well. Any insight?

boominadhaprakash commented 3 years ago

Hi @Ludotrico

We haven't created complete Instagram application. User mostly stuck on viewing snaps with progress bar. We have concentrated and implemented that part only. You can apply your own logic and modify the code on top of our implementation.

We haven't used any server to store and retrieve the lastPlayedSnapIndex value. That's why we haven't concentrated on that part.

If you have the lastplayedsnapindex value, you can start the snap at specific index. Please refer this issue. https://github.com/drawRect/Instagram_Stories/issues/98

Ludotrico commented 3 years ago

@boominadhaprakash incredible, thank you.

Couple questions:

1.) Your branch allows stories to be opened at a specific snap index which is great. However, this only works for the first selected story. The functionality is lost if you swipe to the next story, it always starts at 0 no matter what. Any insight on how to make the library respect the lastPlayedSnapIndex even when tapping through stories? I see, strangely, that the handPickedSnapIndex is set to zero sometimes, maybe that is a clue.

2.) Why is your branch #98 not merged to master?

Thank you

boominadhaprakash commented 3 years ago

Hi @Ludotrico

Answer for your 1st Question: When you are moving to next story by swiping it or when it automatically moves, you can set the lastPlayedSnapIndex value to that particular story's lastPlayedSnapIndex value. So that it will start from the lastPlayedSnapIndexValue. You can tweak the willDisplayCell method to achieve your functionality.

Answer for your 2nd Question: We have waited for the user to confirm whether the functionality is working or not. That's why we didn't merge the code. We will merge it and intimate you. Thanks for notified it to us.

boominadhaprakash commented 3 years ago

Hi @Ludotrico

We have merged the branch. Kindly look into it.

Ludotrico commented 3 years ago

Thanks, for me the branch is working great.

I achieved a solution to number 1 which is making the struct models into classes. This allows for changes to lastPlayedSnapIndex (which is stored in my model) to persist as the objects are passed by reference, exactly how instagram has it. Thank you for your time.

forTJ555 commented 3 years ago

@Ludotrico can pls send code how you handle this