Question

I am working with a digital book application. I make use of swf loader to load swf pages created from pdf. I use TextSnapsot to draw inline text highlight on the pages. The highlight is thoroughly retained on the respective pages throughout the session and later it can be updated/deleted without any problem. Everything was working great till I made the following changes in the swf loading approach to enable page caching:

I am now loading swf loader object into application memory and while doing jumping from one page to other page I am just copying the content of the next page to the current swf loader which is on the display to the user. There are two sets of swf loaders - one for displaying the page and other to cache the next/previous page(s). On the caching side, I load the swf into application memory and after getting it loaded I pick all the contents of the loaded swf page (the children of it's movie clip) into an array collection. While changing the page I copy the cached content into the swf loader's movie clip which is displaying the page.

Now when I highlight on the page on display and navigate back/forth from the page and comeback again to the page where I did the highlighting: It shows the highlight I did. But as soon as I try to draw another highlight on that page, the previous highlight is instantly disappears from the page.

I suspect that the Textsnapshot object which draws highlight while navigating (to the target display page) is different from the one which redraws/update the highlight on the same page next time. Although the Textsnapshot object id for both the objects is same.

Here are some code snippet:

For copying the content from the swf loader object cached in application memory:

    private function copyPageContent():void

    {

        var contentCollection:ArrayCollection = new ArrayCollection();

        _pageContentVO = new PageContentVO();

        _pageContentVO.contentHeight = MovieClip(_swfPageLoader.content).height;

        _pageContentVO.contentWidth = MovieClip(_swfPageLoader.content).width;



        var count:int = MovieClip(_swfPageLoader.content).numChildren;                

        for(var i:int=0;i<count;i++)

        {

            var dispObject:DisplayObject = MovieClip(_swfPageLoader.content).removeChildAt(0);                

            contentCollection.addItem(dispObject);

        }



        _pageContentVO.pageContentCollection = contentCollection;

        _swfPageLoader = null;

    }

For copying the content to the swf loader which is displaying the page:

    private function copyContent(pageContentVo:PageContentVO):void

    {

        for(var i:int = 0;i<pageContentVo.pageContentCollection.length;i++)

        {

            var dispObject:DisplayObject = pageContentVo.pageContentCollection.getItemAt(i) as DisplayObject;

            MovieClip(this.content).addChild(dispObject);

        }

        this.content.height = this.height;

        this.content.width = this.width;

    }

after this I dispatch swf loader's complete manually and in the handler of that event I take the text snap shot object.(highlightManager.as)

Code I use to draw highlight manually(using mouse drag on the page).

    public function setHighlight():void

    {

        removeAll();

        if(_textSnapShot!=null && _textSnapShot.getText(0,_textSnapShot.charCount)!="")

        {                

            if(_isCoveredTextSelectedAtAnyInstance)

            {

                _textSnapShot.setSelected(_beginIndex,_endIndex+1,false); //this is the global variable to the class

            }

            else

            {

                _textSnapShot.setSelectColor(0xfff100);

                _textSnapShot.setSelected(_beginIndex,_endIndex+1,true);

            }

            if(saveHighlight)

            {

                countHighlightedSegments();

            }                

        }            

    }

Code I use to redraw previously drawn highlight when I return to the page:

    public function showHighlights(textSnapShot:TextSnapshot,currentPageNum:int):void

    {            

        if(currentPageNum >= 0)

        {

            textSnapShot.setSelected(0,textSnapShot.charCount,false);

            var pageVO:PageVO = _model.eBookVO.eBookPagesVO.getItemAt(currentPageNum) as PageVO;

            var objColl:ArrayCollection = new ArrayCollection();

            objColl.source = pageVO.highLightSelection;

            for(var i:int=0;i<objColl.length;i++)

            {

                var highlightVO:HighlightVO = new HighlightVO();

                highlightVO.beginIndex = objColl.getItemAt(i).beginIndex;

                highlightVO.endIndex = objColl.getItemAt(i).endIndex;

                setHighlightedSegment(textSnapShot,highlightVO.beginIndex,highlightVO.endIndex);

            }

        }

    }



    private function setHighlightedSegment(textSnapShot:TextSnapshot,beginIndex:int,endIndex:int):void

    {

        textSnapShot.setSelectColor(0xfff100);

        textSnapShot.setSelected(beginIndex,endIndex,true);

    }

Looking forward to your support to resolve this issue.

Regards,

JS

Was it helpful?

Solution

What you're doing is not 'caching', it's preloading previous/next pages. Also, what you're doing is really bad practice. I'm not even sure why you're casting these things into MovieClips unless the SWFs are that; if they're Flex SWFs, they'll be UIComponents. I would recommend you rethink your approach. I wouldn't even bother copying the children or anything over. Once the browser loads a SWF, it is now part of the browser cache, meaning the next time it's requested, it won't actually download it.

If you want to 'cache' your SWFs for a quicker next/previous page flipping, I would recommend you use something like SWFLoader to just load the other SWFs without actually adding it to the display, then removing it from memory. That will cache the SWFs for you in the browser. Then when the user click previous/next, just change the url of the main swfloader of the currently displayed page and it will load it up really quickly. No downloading since it's already cached, it will just need to instantiate.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top