aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/org/rssin/android/NavigationDrawerManageFeedsFragment.java
blob: 3e52b1be19f7cd3028da47b16b2c7e0598ce912b (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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/**
 * RSSin - Clever RSS reader for Android
 * Copyright (C) 2015 Randy Wanga, Jos Craaijo, Joep Bernards, Camil Staps
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 2 of the License, or 
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package org.rssin.android;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import org.rssin.listener.FallibleListener;
import org.rssin.rssin.Feed;
import org.rssin.rssin.Filter;
import org.rssin.rssin.R;

import java.io.IOException;
import java.util.List;

/**
 * @author Jos.
 */
public class NavigationDrawerManageFeedsFragment extends Fragment {

    private View rootView;

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static NavigationDrawerManageFeedsFragment newInstance() {
        NavigationDrawerManageFeedsFragment fragment = new NavigationDrawerManageFeedsFragment();
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        container.removeAllViews();

        final Bundle args = getArguments();
        rootView = inflater.inflate(R.layout.fragment_feeds, container, false);
        final Context context = rootView.getContext();


        feedsView = (ListView) rootView.findViewById(R.id.feeds_list);

        try {
            feedsList = FeedsList.getInstance(context);
        } catch (IOException e) {
            Frontend.error(context, R.string.error_load_feeds, e);
        }

        feedAdapter = new FeedAdapter(context, R.layout.item_feed, feedsList.getFeeds());
        feedsView.setAdapter(feedAdapter);

        feedsView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                Feed feed = feedAdapter.getItem(position);
                DefaultStorageProvider
                        .getInstance(context)
                        .removeFeed(feed.getStorageKey());
                feedsList.getFeeds().remove(feed);
                feedAdapter.notifyDataSetChanged();
                return true;
            }
        });

        return rootView;
    }


    private FeedsList feedsList;
    private ListView feedsView;
    private FeedAdapter feedAdapter;

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.feeds_action_add) {
            openAddDialog();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * Open dialog to add new feed
     * For the moment, we temporarily disable rotating because we can't get it working otherwise.
     * @todo make rotating possible
     */
    public void openAddDialog() {
        //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
        final Context context = rootView.getContext();

        AlertDialog.Builder alert = new AlertDialog.Builder(context);

        alert.setTitle(getString(R.string.feeds_activity_add_feed));
        alert.setMessage(getString(R.string.feeds_activity_url));

        final EditText input = new EditText(context);
        input.setFocusable(true);
        input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_URI);
        input.setMaxLines(1);
        input.requestFocus();

        AlertDialog dialog = alert
                .setView(input)
                .setPositiveButton(getResources().getString(R.string.button_apply), new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        String value = input.getText().toString();
                        try {
                            Feed f = new Feed(value, context, DefaultStorageProvider.getInstance(context), VolleyFetcher.getInstance(context), new FallibleListener<String, Object>() {
                                @Override
                                public void onError(Object error) {

                                }

                                @Override
                                public void onReceive(String data) {
                                    feedAdapter.notifyDataSetChanged();
                                }
                            });

                            f.store(DefaultStorageProvider.getInstance(context));
                            feedsList.getFeeds().add(f);
                            feedAdapter.notifyDataSetChanged();
                        } catch (Exception e) {
                            Frontend.error(context, R.string.error_save_feeds, e);
                        }
                        input.setText("");
                        //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
                    }
                })
                .setNegativeButton(getResources().getString(R.string.button_cancel), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        input.setText("");
                        //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
                    }
                })
                .setOnCancelListener(new DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialog) {
                        input.setText("");
                        //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
                    }
                })
                .create();

        dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        dialog.show();
    }

    /**
     * Custom ArrayAdapter to display feeds with our own menu item
     */
    private static class FeedAdapter extends SortedArrayAdapter<Feed> {

        Context context;
        int layoutResourceId;

        public FeedAdapter(Context context, int resource, List<Feed> objects) {
            super(context, resource, objects);
            this.context = context;
            layoutResourceId = resource;
            items = objects;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row = convertView;
            FeedHolder holder = null;

            if (row == null) {
                LayoutInflater inflater = ((Activity) context).getLayoutInflater();
                row = inflater.inflate(layoutResourceId, parent, false);

                holder = new FeedHolder();
                holder.title = (TextView) row.findViewById(R.id.feed_item_title);
                holder.url = (TextView) row.findViewById(R.id.feed_item_url);

                row.setTag(holder);
            } else {
                holder = (FeedHolder) row.getTag();
            }

            Feed feed = items.get(position);
            holder.title.setText(feed.getTitle());
            holder.url.setText(feed.getURL().toString());

            return row;
        }

        /**
         * TextViews holder
         */
        private static class FeedHolder {
            TextView title;
            TextView url;
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }
}