Skip to content

Commit

Permalink
(viewer) Fix #3
Browse files Browse the repository at this point in the history
  • Loading branch information
violet-dev committed Jul 16, 2020
1 parent 1d4161c commit 0e3ef3c
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 49 deletions.
65 changes: 65 additions & 0 deletions lib/pages/viewer/semaphore.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// https://github.com/mezoni/semaphore/blob/master/lib/src/semaphore/semaphore.dart
// This source code is a part of Project Violet.
// Copyright (C) 2020. violet-team. Licensed under the MIT License.

import 'dart:async';
import 'dart:collection';

import 'package:synchronized/synchronized.dart';
import 'package:tuple/tuple.dart';

class Semaphore {
final int maxCount = 8;
HashSet<int> has = HashSet<int>();
HashSet<int> completed = HashSet<int>();

int _currentCount = 0;

final Queue<Tuple2<int, Completer>> _waitQueue =
Queue<Tuple2<int, Completer>>();

Future<int> acquire(int index) {
var completer = Completer<int>();

if (_currentCount + 1 <= maxCount || completed.contains(index)) {
_currentCount++;
completed.add(index);
completer.complete(2);
} else {
has.add(index);

if (has.add(index)) {
var ww = _waitQueue.where((element) => element.item1 == index);
return ww.first.item2.future;
}

_waitQueue.add(Tuple2<int, Completer>(index, completer));
}

return completer.future;
}

void adjust(int index) {
if (_waitQueue.length == 0) return;
var ww = _waitQueue.where((element) => element.item1 == index);
if (ww == null || ww.length == 0) return;
var aa = ww.first;

_waitQueue.removeWhere((element) => element.item1 == index);
_waitQueue.addFirst(aa);
}

void drop() {
_waitQueue.clear();
}

void release() {
_currentCount--;
if (_waitQueue.isNotEmpty) {
_currentCount++;
final completer = _waitQueue.removeFirst();
completed.add(completer.item1);
completer.item2.complete();
}
}
}
115 changes: 67 additions & 48 deletions lib/pages/viewer/viewer_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import 'package:photo_view/photo_view_gallery.dart';
import 'package:scrollable_positioned_list/scrollable_positioned_list.dart';
import 'package:violet/database/user/record.dart';
import 'package:violet/dialogs.dart';
import 'package:violet/pages/viewer/semaphore.dart';
import 'package:violet/settings.dart';
import 'package:violet/database/user/user.dart';
import 'package:visibility_detector/visibility_detector.dart';
Expand Down Expand Up @@ -221,12 +222,13 @@ class ViewerWidget extends StatelessWidget {
}

class GalleryExampleItem {
GalleryExampleItem(
{this.id,
this.url,
this.headers,
this.isSvg = false,
this.loaded = false});
GalleryExampleItem({
this.id,
this.url,
this.headers,
this.isSvg = false,
this.loaded = false,
});

final String id;
final String url;
Expand All @@ -237,8 +239,11 @@ class GalleryExampleItem {
}

class GalleryExampleItemThumbnail extends StatelessWidget {
GalleryExampleItemThumbnail({Key key, this.galleryExampleItem, this.onTap})
: super(key: key);
GalleryExampleItemThumbnail({
Key key,
this.galleryExampleItem,
this.onTap,
}) : super(key: key);

final GalleryExampleItem galleryExampleItem;

Expand All @@ -249,52 +254,70 @@ class GalleryExampleItemThumbnail extends StatelessWidget {
final width = MediaQuery.of(context).size.width - 4;
return Container(
child: FutureBuilder(
future: _calculateImageDimension(),
builder: (context, AsyncSnapshot<Size> snapshot) {
if (snapshot.hasData) {
galleryExampleItem.loaded = true;
galleryExampleItem.height = width / snapshot.data.aspectRatio;
future: Future.delayed(Duration(milliseconds: 100)).then((value) => 1),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return SizedBox(
height: 300.0,
child: Center(
child: SizedBox(
child: CircularProgressIndicator(),
width: 30,
height: 30,
),
),
);
}
return SizedBox(
height:
galleryExampleItem.loaded ? galleryExampleItem.height : 300.0,
child: GestureDetector(
onTap: onTap,
child: Hero(
tag: galleryExampleItem.id.toString(),
child: CachedNetworkImage(
imageUrl: galleryExampleItem.url,
httpHeaders: galleryExampleItem.headers,
placeholder: (context, url) => Center(
child: SizedBox(
child: CircularProgressIndicator(),
width: 30,
height: 30,
return FutureBuilder(
future: _calculateImageDimension(),
builder: (context, AsyncSnapshot<Size> snapshot) {
if (snapshot.hasData) {
galleryExampleItem.loaded = true;
galleryExampleItem.height = width / snapshot.data.aspectRatio;
}
return SizedBox(
height: galleryExampleItem.loaded
? galleryExampleItem.height
: 300.0,
child: GestureDetector(
onTap: onTap,
child: Hero(
tag: galleryExampleItem.id.toString(),
child: CachedNetworkImage(
imageUrl: galleryExampleItem.url,
httpHeaders: galleryExampleItem.headers,
placeholder: (context, url) => Center(
child: SizedBox(
child: CircularProgressIndicator(),
width: 30,
height: 30,
),
),
placeholderFadeInDuration: Duration(microseconds: 500),
fadeInDuration: Duration(microseconds: 500),
fadeInCurve: Curves.easeIn,
progressIndicatorBuilder: (context, string, progress) {
return Center(
child: SizedBox(
child: CircularProgressIndicator(
value: progress.progress),
width: 30,
height: 30,
),
);
},
),
),
placeholderFadeInDuration: Duration(microseconds: 500),
fadeInDuration: Duration(microseconds: 500),
fadeInCurve: Curves.easeIn,
progressIndicatorBuilder: (context, string, progress) {
return Center(
child: SizedBox(
child:
CircularProgressIndicator(value: progress.progress),
width: 30,
height: 30,
),
);
},
),
),
),
);
},
);
},
),
);
}

Future<Size> _calculateImageDimension() {
Future<Size> _calculateImageDimension() async {
Completer<Size> completer = Completer();
Image image = new Image(
image: CachedNetworkImageProvider(
Expand All @@ -310,10 +333,6 @@ class GalleryExampleItemThumbnail extends StatelessWidget {
);
return completer.future;
}

// Widget get(BuildContext context, String string, DownloadProgress progress) {

// }
}

class GalleryPhotoViewWrapper extends StatefulWidget {
Expand Down
2 changes: 1 addition & 1 deletion lib/widgets/article_item/article_list_item_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class _ArticleListItemVerySimpleWidgetState
thumbnail = images.item2[0];
imageCount = images.item2.length;
ThumbnailManager.insert(widget.queryResult.id(), images);
setState(() {});
if (!disposed) setState(() {});
});
} else {
var thumbnails = ThumbnailManager.get(widget.queryResult.id()).item2;
Expand Down

0 comments on commit 0e3ef3c

Please sign in to comment.