aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/miz_hi/smileessence/talkchase/TalkChaser.java
blob: c2328013c09fd0bfef474801ac85763877734012 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package net.miz_hi.smileessence.talkchase;

import net.miz_hi.smileessence.cache.TweetCache;
import net.miz_hi.smileessence.model.status.tweet.TweetModel;
import net.miz_hi.smileessence.model.statuslist.StatusList;
import net.miz_hi.smileessence.model.statuslist.impl.TalkList;
import net.miz_hi.smileessence.notification.Notificator;
import net.miz_hi.smileessence.status.TweetUtils;
import net.miz_hi.smileessence.statuslist.StatusListManager;
import net.miz_hi.smileessence.view.fragment.impl.TalkFragment;

public class TalkChaser
{

    public TalkFragment fragment;
    public long chasingId;
    public StatusList talkList;

    public TalkChaser(TalkFragment fragment)
    {
        this.talkList = new TalkList();
        this.fragment = fragment;
    }

    public void hitNewTweet(TweetModel tweet)
    {
        this.talkList.addToTop(tweet.getOriginal());
        updateChasingId(tweet.getOriginal().statusId);
    }

    private void updateChasingId(long id)
    {
        this.chasingId = id;
        fragment.setChasingId(chasingId);
    }

    /**
     * Call on Network Thread
     */
    public void startRelation(long statusId)
    {
        TweetModel start = TweetUtils.getOrCreateStatusModel(statusId);
        if (start == null)
        {
            Notificator.alert("ツイートの取得に失敗しました");
            return;
        }
        talkList.addToTop(start);

        //Load older
        long inReplyTo = start.getInReplyToStatusId();
        while (inReplyTo > 0)
        {
            TweetModel older = TweetUtils.getOrCreateStatusModel(inReplyTo);
            talkList.addToBottom(older);
            inReplyTo = older.getInReplyToStatusId();
        }

        //Load newer
        inReplyTo = start.statusId;
        while (true)
        {
            long before = inReplyTo;
            for (TweetModel newer : TweetCache.getList())
            {
                if (newer.getInReplyToStatusId() == inReplyTo)
                {
                    talkList.addToTop(newer);
                    inReplyTo = newer.statusId;
                    break;
                }
            }
            if (before == inReplyTo)
            {
                break;
            }
        }

        updateChasingId(((TweetModel) talkList.getStatus(0)).statusId);
        StatusListManager.getAdapter(talkList).forceNotifyAdapter();
    }

    public void stopRelation()
    {
        TalkManager.removeTalkChaser(this);
    }
}