Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewriting feed list to slivers #14

Merged
merged 5 commits into from
Jan 17, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
clean up after moving to slivers
  • Loading branch information
IgorKhramtsov committed Jan 16, 2022
commit 9e37e1523e3bf6dd9638ec7271f545d093525f20
86 changes: 9 additions & 77 deletions lib/presentation/screens/feed/feed_widget.dart
Original file line number Diff line number Diff line change
@@ -29,7 +29,6 @@ class FeedWidget extends StatefulWidget {
class _FeedWidgetState extends State<FeedWidget> {
late CacheManager cacheManager = serviceLocator<CacheManager>();
final ScrollController controller = ScrollController();
final ItemPositionsListener listener = ItemPositionsListener.create();
final CurrentElementListener currentElementListener =
CurrentElementListener();
final renderedMemes = <int, Size>{};
@@ -40,33 +39,24 @@ class _FeedWidgetState extends State<FeedWidget> {
super.initState();

currentElementListener.currentIndex.addListener(updateSelectedMeme);
listener.itemPositions.addListener(updateSelectedMeme);
// cacheManager.emptyCache();
}

@override
void dispose() {
listener.itemPositions.removeListener(updateSelectedMeme);
currentElementListener.currentIndex.removeListener(updateSelectedMeme);
super.dispose();
}

void updateSelectedMeme() {
final bloc = BlocProvider.of<FeedBloc>(context);

// final selectedMeme = listener.itemPositions.value
// .where((e) => e.itemLeadingEdge <= 0.5 && e.itemTrailingEdge >= 0.5)
// .firstOrNull;
// if (selectedMeme == null) {
// assert(false);
// return;
// }

bloc.add(
FeedEvent.select(
currentElementListener.currentIndex.value,
Option.fromNullable(
renderedMemesKeys[currentElementListener.currentIndex.value]),
renderedMemesKeys[currentElementListener.currentIndex.value],
),
),
);
}
@@ -85,58 +75,6 @@ class _FeedWidgetState extends State<FeedWidget> {
);
}

Widget buildList(double maxHeight, BoxConstraints cnstr) {
return ScrollablePositionedList.builder(
physics: FeedScrollPhysics(
renderedMemes,
// sbustract bottom tab navbar size, because we cant see anything
// behind it (unlike top unsafe area)
maxHeight,
topPadding: MediaQuery.of(context).padding.top,
),
itemCount: state.memes.length,
itemPositionsListener: listener,
itemBuilder: (BuildContext context, int index) {
final imageProvider = CachedNetworkImageProvider(
state.memes[index].imageLink,
cacheManager: cacheManager,
);
final key = renderedMemesKeys.putIfAbsent(
index,
() => GlobalKey(),
);
if (renderedMemes[index] == null) {
propogateSizeToMap(
imageProvider,
cnstr,
maxHeight,
index,
);
renderedMemes[index] = Size(cnstr.maxWidth, 200);
}

// Using [AnimatedVisibilityWithReversedDuration] to
// hide selected post with animation and show immediately
// if it was already hidden. PostWidget will re-render
// any error images, so they will be reloaded, and 2 same widgets
// will have different state, and overlaping each other
return AnimatedVisibilityWithReversedDuration(
duration: const Duration(milliseconds: 300),
reversedDuration: Duration.zero,
visibility: index != state.iterator,
child: FeedImage(
key: key,
imageProvider: imageProvider,
constraints: cnstr,
onRefreshPressed: () => setState(() {
renderedMemesKeys[index] = GlobalKey();
}),
),
);
},
);
}

Widget buildSliverList(double maxHeight, BoxConstraints cnstr) {
return CustomScrollView(
physics: FeedScrollPhysics(
@@ -194,20 +132,14 @@ class _FeedWidgetState extends State<FeedWidget> {
buildSliverList(maxHeight, cnstr),
buildVignette(),
// Build overlay with actions, description and listenable of current
ValueListenableBuilder(
valueListenable: listener.itemPositions,
builder: (_, __, ___) {
return AnimatedSwitcher(
duration: const Duration(milliseconds: 150),
child: PostWidget(
state,
constraints: cnstr,
key: ValueKey(state.currentMemeModel.sourceLink),
),
);
},
AnimatedSwitcher(
duration: const Duration(milliseconds: 150),
child: PostWidget(
state,
constraints: cnstr,
key: ValueKey(state.currentMemeModel.sourceLink),
),
)
// scroll position
],
);
},