aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/com/camilstaps/taize/DailyReadingFragment.java
blob: 4daefbd4bbfa0cb857c14e4aec21994e098b533d (plain) (blame)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package com.camilstaps.taize;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.TextView;

import com.camilstaps.common.Date;
import com.camilstaps.common.Listener;
import com.camilstaps.common.Sharable;

/**
 * Fragment for displaying one daily reading
 * @author Camil Staps
 */
public class DailyReadingFragment extends Fragment implements Sharable {

    private SharedPreferences.OnSharedPreferenceChangeListener sharedPreferenceChangeListener;
    private ViewTreeObserver.OnScrollChangedListener onScrollChangedListener;
    private View rootView;
    private Context context;

    private Date date;
    private String reading;
    private String bibleText;
    private String reference;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.pager_content_dailyreading, container, false);

        context = container.getContext();

        Bundle args = getArguments();
        int dateOffset = args.getInt("dateOffset", 0);

        date = new Date();
        date.addOrSubtractDays(dateOffset);

        setupContent();

        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        sharedPreferenceChangeListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
            @Override
            public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
                switch (key) {
                    case "dailyreadings":
                    case "dailyreadingbible":
                        setupContent();
                        break;
                }
            }
        };
        PreferenceManager.getDefaultSharedPreferences(context).registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
    }

    /**
     * Setup all content of the Fragment
     */
    protected void setupContent() {
        setDailyReading();
        setDailyReadingBibleText();
    }

    /**
     * Set the daily reading
     */
    public void setDailyReading() {
        Taize.getDailyReading(context, new Listener<DailyReading>() {
            @Override
            public void success(DailyReading data) {
                ((TextView) rootView.findViewById(R.id.textDailyReading)).setText(data.getTextWithoutReference());
                reading = data.getTextWithoutReference();
                BibleText bt = data.getBibleReference();
                if (bt != null) {
                    ((TextView) rootView.findViewById(R.id.refDailyReadingBibleText)).setText(bt.toString());
                    reference = bt.toString();
                }
            }

            @Override
            public void failure() {
                ((TextView) rootView.findViewById(R.id.textDailyReading)).setText("No reading found.");
            }
        }, date);

        String niceDate = date.toNiceString(context);
        niceDate = niceDate.substring(0,1).toUpperCase() + niceDate.substring(1);
        ((TextView) rootView.findViewById(R.id.dateDailyReading)).setText(niceDate);
    }

    /**
     * Set the Bible text
     */
    public void setDailyReadingBibleText() {
        Taize.getDailyReadingBibleText(context, new Listener<String>() {
            @Override
            public void success(String data) {
                ((TextView) rootView.findViewById(R.id.textDailyReadingBibleText)).setText(data);
                bibleText = data;
            }

            @Override
            public void failure() {
                ((TextView) rootView.findViewById(R.id.textDailyReadingBibleText)).setText("No bible text found.");
            }
        }, date);
    }

    /**
     * The user can choose to share the reading, the Bible text or both
     * @param id the XML id the resource is linked to
     * @return
     */
    @Override
    public Object getItem(int id) {
        switch (id) {
            case R.id.action_share_bibletext:
                return reference + ": " + bibleText;
            case R.id.action_share_reading_with_bibletext:
                return reading + "\n\n" + reference + ": " + bibleText;
            case R.id.action_share_reading:
            default:
                return reading + " (" + reference + ")";
        }
    }

    /**
     * The user can choose to share the reading, the Bible text or both
     * @return
     */
    @Override
    public int getMenuId() {
        return R.menu.share_popup_reading;
    }
}