~cytrogen/fluent-reader-mobile

ref: 2f8d3710d368f61ea1ffd02396e04ac291c3f4cc fluent-reader-mobile/lib/pages/subscription_list_page.dart -rw-r--r-- 10.1 KiB
2f8d3710 — Bruce Liu update packages and use uri for http 4 years ago
                                                                                
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import 'package:fluent_reader_lite/components/badge.dart';
import 'package:fluent_reader_lite/components/mark_all_action_sheet.dart';
import 'package:fluent_reader_lite/components/my_list_tile.dart';
import 'package:fluent_reader_lite/components/subscription_item.dart';
import 'package:fluent_reader_lite/components/sync_control.dart';
import 'package:fluent_reader_lite/generated/l10n.dart';
import 'package:fluent_reader_lite/models/source.dart';
import 'package:fluent_reader_lite/models/sources_model.dart';
import 'package:fluent_reader_lite/models/sync_model.dart';
import 'package:fluent_reader_lite/pages/group_list_page.dart';
import 'package:fluent_reader_lite/pages/home_page.dart';
import 'package:fluent_reader_lite/utils/colors.dart';
import 'package:fluent_reader_lite/utils/global.dart';
import 'package:fluent_reader_lite/utils/store.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:intl/intl.dart';
import 'package:modal_bottom_sheet/modal_bottom_sheet.dart';
import 'package:provider/provider.dart';

class SubscriptionListPage extends StatefulWidget {
  final ScrollTopNotifier scrollTopNotifier;

  SubscriptionListPage(this.scrollTopNotifier, {Key key}) : super(key: key);

  @override
  _SubscriptionListPageState createState() {
    return _SubscriptionListPageState();
  }
}

class _SubscriptionListPageState extends State<SubscriptionListPage> {
  List<String> sids;
  String title;
  bool transitioning = false;
  bool unreadOnly = Store.sp.getBool(StoreKeys.UNREAD_SUBS_ONLY) ?? false;

  void _onScrollTop() {
    if (widget.scrollTopNotifier.index == 1 && !Navigator.of(context).canPop()) {
      PrimaryScrollController.of(context).animateTo(
        0,
        curve: Curves.easeOut,
        duration: const Duration(milliseconds: 300),
      );
    }
  }

  @override
  void initState() {
    super.initState();
    widget.scrollTopNotifier.addListener(_onScrollTop);
  }
  
  @override
  void dispose() {
    widget.scrollTopNotifier.removeListener(_onScrollTop);
    super.dispose();
  }

  void _openGroups() async {
    List<String> result;
    if (Global.isTablet) {
      result = await Navigator.of(context).push(CupertinoPageRoute(
        builder: (context) => GroupListPage(),
      ));
    } else {
      setState(() { transitioning = true; });
      result = await CupertinoScaffold.showCupertinoModalBottomSheet(
        context: context,
        useRootNavigator: true,
        builder: (context) => GroupListPage(),
      );
    }
    if (!mounted) return;
    if (result != null) {
      _onScrollTop();
      if (result.length == 0) {
        setState(() {
          title = null;
          sids = null;
        });
      } else if (result.length > 1) {
        setState(() {
          title = S.of(context).uncategorized;
          sids = Global.groupsModel.uncategorized;
        });
      } else {
        setState(() {
          title = result[0];
          sids = Global.groupsModel.groups[title];
        });
      }
    }
    await Future.delayed(Duration(milliseconds: 300));
    setState(() { transitioning = false; });
  }

  void _openMarkAllModal() {
    showCupertinoModalPopup(
      context: context,
      builder: (context) => MarkAllActionSheet(sids == null ? {} : Set.from(sids)),
    );
  }

  void _openSettings() {
    Navigator.of(context, rootNavigator: true).pushNamed("/settings");
  }

  void _openErrorLog() {
    if (!Global.syncModel.lastSyncSuccess) {
      HapticFeedback.mediumImpact();
      Navigator.of(context, rootNavigator: true).pushNamed("/error-log");
    }
  }

  void _toggleUnreadOnly() {
    HapticFeedback.mediumImpact();
    setState(() { unreadOnly = !unreadOnly; });
    _onScrollTop();
    Store.sp.setBool(StoreKeys.UNREAD_SUBS_ONLY, unreadOnly);
  }

  void _dismissTip() {
    if (Global.sourcesModel.showUnreadTip) {
      Global.sourcesModel.showUnreadTip = false;
      setState(() {});
    }
  }

  Widget _buildUnreadTip() {
    return SliverToBoxAdapter(child: Container(
      padding: EdgeInsets.all(16),
      child: ClipRRect(
        borderRadius: BorderRadius.circular(16),
        child: Container(
          padding: EdgeInsets.all(12),
          color: CupertinoColors.secondarySystemBackground.resolveFrom(context),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Padding(
                padding: EdgeInsets.only(right: 12),
                child: Icon(Icons.radio_button_checked),
              ),
              Flexible(child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    S.of(context).unreadSourceTip,
                    style: TextStyle(
                      color: CupertinoColors.label.resolveFrom(context),
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  Padding(padding: EdgeInsets.only(bottom: 6)),
                  CupertinoButton(
                    minSize: 28,
                    padding: EdgeInsets.zero,
                    child: Text(S.of(context).confirm),
                    onPressed: _dismissTip,
                  ),
                ],
              )),
            ],
          ),
        ),
      ),
    ));
  }

  @override
  Widget build(BuildContext context) {
    final titleWidget = GestureDetector(
      onLongPress: _toggleUnreadOnly,
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          Container(
            constraints: BoxConstraints(
              maxWidth: Global.isTablet
                ? 260
                : MediaQuery.of(context).size.width - 60,
            ),
            child: Text(
              title ?? S.of(context).subscriptions, 
              overflow: TextOverflow.ellipsis,
            ),
          ),
          if (unreadOnly) Padding(
            padding: EdgeInsets.only(left: 4),
            child: Icon(Icons.radio_button_checked, size: 18),
          ),
        ],
      ),
    );
    final navigationBar = CupertinoSliverNavigationBar(
      stretch: false,
      largeTitle: titleWidget,
      heroTag: "subscriptions",
      transitionBetweenRoutes: true,
      backgroundColor: transitioning ? MyColors.tileBackground : CupertinoColors.systemBackground,
      leading: CupertinoButton(
        minSize: 36,
        padding: EdgeInsets.zero,
        child: Text(S.of(context).groups),
        onPressed: _openGroups,
      ),
      trailing: Container(
        transform: Matrix4.translationValues(12, 0, 0),
        child: Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            CupertinoButton(
              padding: EdgeInsets.zero,
              child: Icon(
                CupertinoIcons.checkmark_circle,
                semanticLabel: S.of(context).markAll,
              ),
              onPressed: _openMarkAllModal,
            ),
            CupertinoButton(
              padding: EdgeInsets.zero,
              child: Icon(
                CupertinoIcons.settings,
                semanticLabel: S.of(context).settings,
              ),
              onPressed: _openSettings,
            ),
          ],
        ), 
      )
    );
    final sourcesList = Consumer<SourcesModel>(
      builder: (context, sourcesModel, child) {
        List<RSSSource> sources;
        if (sids == null) {
          sources = Global.sourcesModel.getSources().toList();
          if (unreadOnly) {
            sources = sources.where((s) => s.unreadCount > 0).toList();
          }
        } else {
          sources = [];
          for (var sid in sids) {
            final source = Global.sourcesModel.getSource(sid);
            if (!unreadOnly || source.unreadCount > 0) {
              sources.add(source);
            }
          }
        }
        // Latest sources first
        sources.sort((a, b) {
          return b.latest.compareTo(a.latest);
        });
        return SliverList(
          delegate: SliverChildBuilderDelegate((content, index) {
            var source = sources[index];
            return SubscriptionItem(source, key: Key(source.id));
          }, childCount: sources.length),
        );
      },
    );
    final syncStyle = TextStyle(
      fontSize: 14,
      color: CupertinoColors.tertiaryLabel.resolveFrom(context),
    );
    final syncInfo = Consumer<SyncModel>(
      builder: (context, syncModel, child) {
        return SliverToBoxAdapter(
          child: GestureDetector(
            onLongPress: _openErrorLog,
            child: Container(
              padding: EdgeInsets.all(12),
              child: Column(
                children: [
                  Text(
                    syncModel.lastSyncSuccess
                      ? S.of(context).lastSyncSuccess
                      : S.of(context).lastSyncFailure,
                    style: syncStyle,
                  ),
                  Text(
                    DateFormat
                      .Md(Localizations.localeOf(context).toString())
                      .add_Hm().format(syncModel.lastSynced),
                    style: syncStyle,
                  ),
                ],
              ),
            ),
          ),
        );
      },
    );
    return CupertinoScrollbar(child: CustomScrollView(
      slivers: [
        navigationBar,
        SyncControl(),
        if (Global.sourcesModel.showUnreadTip) _buildUnreadTip(),
        if (sids != null && sids.length > 0) Consumer<SourcesModel>(
          builder: (context, sourcesModel, child) {
            var count = sids
              .map((sid) => sourcesModel.getSource(sid))
              .fold(0, (c, s) => c + s.unreadCount);
            return SliverToBoxAdapter(child: MyListTile(
              title: Text(S.of(context).allArticles),
              trailing: count > 0 ? Badge(count) : null,
              trailingChevron: false,
              onTap: () async { 
                await Global.feedsModel.initSourcesFeed(sids.toList());
                Navigator.of(context).pushNamed("/feed", arguments: title);
              },
              background: CupertinoColors.systemBackground,
            ));
          },
        ),
        sourcesList,
        syncInfo,
      ]
    ));
  }
  
}