summaryrefslogtreecommitdiff
path: root/org.fox.ttrss/src/main/java/org/fox/ttrss/BaseFeedlistFragment.java
blob: ec4e4a7a2b57a3aa694891c660361ebe7d899dcd (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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package org.fox.ttrss;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v7.widget.SwitchCompat;
import android.util.TypedValue;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import org.fox.ttrss.offline.OfflineActivity;

import java.net.MalformedURLException;
import java.net.URL;

public abstract class BaseFeedlistFragment extends StateSavedFragment {
    abstract public void refresh();

    public void initDrawerHeader(LayoutInflater inflater, View view, ListView list, final CommonActivity activity, final SharedPreferences prefs, boolean isRoot) {

        boolean isOffline = activity instanceof OfflineActivity;

        if (true /*m_activity.findViewById(R.id.headlines_drawer) != null*/) {
            try {

                boolean needSettingsFooter = false;

                if (activity.isSmallScreen()) {
                    View layout = inflater.inflate(R.layout.drawer_header, list, false);
                    list.addHeaderView(layout, null, false);

                    TextView login = view.findViewById(R.id.drawer_header_login);
                    TextView server = view.findViewById(R.id.drawer_header_server);

                    login.setText(prefs.getString("login", ""));
                    try {
                        server.setText(new URL(prefs.getString("ttrss_url", "")).getHost());
                    } catch (MalformedURLException e) {
                        server.setText("");
                    }

                    View settings = view.findViewById(R.id.drawer_settings_btn);

                    settings.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            try {
                                Intent intent = new Intent(getActivity(),
                                        PreferencesActivity.class);

                                ActivityOptionsCompat options = ActivityOptionsCompat
                                        .makeSceneTransitionAnimation(getActivity(), v, "SETTINGS_REVEAL");

                                ActivityCompat.startActivityForResult(getActivity(), intent, 0, options.toBundle());

                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });
                } else {
                    needSettingsFooter = true;
                }

				/* deal with ~material~ footers */

                // divider
                View footer = inflater.inflate(R.layout.drawer_divider, list, false);
                footer.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //
                    }
                });
                list.addFooterView(footer);

                // unread only checkbox
                footer = inflater.inflate(R.layout.feeds_row_toggle, list, false);
                list.addFooterView(footer);
                TextView text = footer.findViewById(R.id.title);
                text.setText(R.string.unread_only);

                ImageView icon = footer.findViewById(R.id.icon);
                TypedValue tv = new TypedValue();
                getActivity().getTheme().resolveAttribute(R.attr.ic_filter_variant, tv, true);
                icon.setImageResource(tv.resourceId);

                final SwitchCompat rowSwitch = footer.findViewById(R.id.row_switch);
                rowSwitch.setChecked(activity.getUnreadOnly());

                rowSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton button, boolean isChecked) {
                        activity.setUnreadOnly(isChecked);
                        refresh();
                    }
                });

                footer.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        rowSwitch.setChecked(!rowSwitch.isChecked());
                    }
                });

                if (isRoot) {
                    if (needSettingsFooter) {
                        // settings (as a list footer row)

                        footer = inflater.inflate(R.layout.feeds_row, list, false);
                        footer.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                try {
                                    Intent intent = new Intent(getActivity(),
                                            PreferencesActivity.class);

                                    ActivityOptionsCompat options = ActivityOptionsCompat
                                            .makeSceneTransitionAnimation(getActivity(), v, "SETTINGS_REVEAL");

                                    ActivityCompat.startActivityForResult(getActivity(), intent, 0, options.toBundle());

                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        });

                        list.addFooterView(footer);
                        text = footer.findViewById(R.id.title);
                        text.setText(R.string.action_settings);

                        icon = footer.findViewById(R.id.icon);
                        tv = new TypedValue();
                        getActivity().getTheme().resolveAttribute(R.attr.ic_settings, tv, true);
                        icon.setImageResource(tv.resourceId);

                        TextView counter = footer.findViewById(R.id.unread_counter);
                        counter.setText(R.string.blank);
                    }

                    // offline
                    footer = inflater.inflate(R.layout.feeds_row, list, false);
                    footer.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (activity instanceof OnlineActivity) {
                                ((OnlineActivity)activity).switchOffline();

                            } else if (activity instanceof OfflineActivity) {
                                ((OfflineActivity)activity).switchOnline();
                            }
                        }
                    });

                    list.addFooterView(footer);
                    text = footer.findViewById(R.id.title);
                    text.setText(isOffline ? R.string.go_online : R.string.go_offline);

                    icon = footer.findViewById(R.id.icon);
                    tv = new TypedValue();
                    getActivity().getTheme().resolveAttribute(isOffline ? R.attr.ic_cloud_upload : R.attr.ic_cloud_download, tv, true);
                    icon.setImageResource(tv.resourceId);

                    TextView counter = footer.findViewById(R.id.unread_counter);
                    counter.setText(R.string.blank);
                }

            } catch (InflateException e) {
                // welp couldn't inflate header i guess
                e.printStackTrace();
            } catch (java.lang.UnsupportedOperationException e) {
                e.printStackTrace();
            }
        }

    }

}