Skip to content

Instantly share code, notes, and snippets.

@winterdl
Last active September 27, 2021 02:19
Show Gist options
  • Save winterdl/de0bac7ee7cd9bd932dc486c54ac06a5 to your computer and use it in GitHub Desktop.
Save winterdl/de0bac7ee7cd9bd932dc486c54ac06a5 to your computer and use it in GitHub Desktop.
Open Book
class BookPage extends StatefulWidget {
final String url;
const BookPage(this.url, {Key? key}) : super(key: key);
@override
_BookPageState createState() => _BookPageState();
}
class _BookPageState extends State<BookPage> {
bool isDone = false;
@override
Widget build(BuildContext context) {
return WebView(
initialUrl: widget.url,
javascriptMode: JavascriptMode.unrestricted,
}
}
import 'package:mno_server/mno_server.dart';
import 'package:mno_shared/mediatype.dart';
import 'package:mno_shared/publication.dart';
import 'package:mno_streamer/parser.dart';
late ServerBloc serverBloc;
late String address;
PubBox? pubBox;
List<Link>? spines;
serverBloc = ServerBloc();
serverBloc.stream.listen((event) async {
if (event is ServerStarted) {
setState(() {
address = event.address;
isStarted = true;
spines = pubBox?.publication.readingOrder;
});
} else if (event is ServerClosed) {
setState(() {
isStarted = false;
});
}
});
// spines, pubBox from createServer
Widget createPageView(
BuildContext context, PageController controller, List<Link>? spines) {
if (spines == null) {
return const Center(
child: Text('Click Open Book'),
);
}
return PageView.builder(
itemCount: spine.length,
physics: const AlwaysScrollableScrollPhysics(),
scrollDirection: Axis.vertical,
pageSnapping: true,
itemBuilder: (context, index) {
return BookPage('$address${spines[index].href}');
},
);
}
var dirPath = await copyBookFromAsset('assets/books/test.epub');
pubBox = await EpubParser().parse(dirPath);
if (pubBox != null) {
serverBloc.add(StartServer([
AssetsRequestHandler("assets",
assetProvider: SimpleAssetProvider()),
FetcherRequestHandler(pubBox.publication)
]));
}
@JideGuru
Copy link

Where are you importing SimpleAssetProvider from please?

@winterdl
Copy link
Author

winterdl commented Sep 26, 2021

Sorry for the missing source:

class SimpleAssetProvider extends AssetProvider {
  @override
  Future<ByteData> load(String path) {
    return rootBundle.load(path);
  }
}
static Future<File> copyBookFromAsset(String asset) async {
    ByteData data = await rootBundle.load(asset);
    String dir = (await getTemporaryDirectory()).path;
    String path = '$dir/${basename(asset)}';
    if (!File(path).existsSync()) {
      final buffer = data.buffer;
      return File(path).writeAsBytes(
          buffer.asUint8List(data.offsetInBytes, data.lengthInBytes));
    } else {
      return File(path);
    }
  }

@winterdl
Copy link
Author

I've tested 2 days last weekend, the official flutter webview lacks of features (the flutter members who coding flutter webview are too lazy to porting the widget, only about 1/4 properties, events compare to native webview, even with basic property like enable disable scrollbar, javascript channel could not return value for javascript side, buggy and bad performance, the flutter_inapwebview have more feature and expose more events, I think we should use inappwebview over flutter_webview. I've got some problem with gesture arena when using webview with pageview.

@JideGuru
Copy link

Yes you’re right about inappwebview having more features. I used it for my text editor package infact https://github.com/JideGuru/rich_editor

@JideGuru
Copy link

I think it’ll be really helpful if you pushed to a repo instead
We can probably collaborate better that way and that should speed up things. Thanks

@winterdl
Copy link
Author

I will create testing repo next week after clean up, for now I could only create server to stream epub by using mno packages, testing flutter webview and inappwebview some gesture usecases. The mno streamer package have issue with unpack epub, I could parsing only 1/5 testing epub, I’ve raised the issue and waiting them to confirm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment