Skip to content

Instantly share code, notes, and snippets.

View alphamikle's full-sized avatar

Mike Alfa alphamikle

View GitHub Profile
@alphamikle
alphamikle / main.dart
Created January 1, 2021 15:29
Example of Navigator 2.0 Pages methods bug
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
final parser = RootRouterParser();
final delegate = RootRouterDelegate();
const indexes = ['First', 'Second', 'Third', 'Fourth', 'Fifth'];
@alphamikle
alphamikle / loading_items_in_main_thread.dart
Created January 27, 2021 18:33
Loading items in main thread
Future<void> loadItemsOnMainThread() async {
_startFpsMeter();
isLoading = true;
notifyListeners();
List<Item> mainThreadItems;
for (int i = 0; i < 10; i++) {
bench.startTimer('Load items in main thread');
mainThreadItems = await makeManyRequests(5);
final double diff = bench.endTimer('Load items in main thread');
requestDurations.add(diff);
@alphamikle
alphamikle / loading_items_with_compute.dart
Created January 31, 2021 13:51
Loading items with compute
Future<void> loadItemsWithComputed() async {
_startFpsMeter();
isLoading = true;
notifyListeners();
List<Item> computedItems;
/// There were two variants of execution
/// Each set of 5 concurrent requests, run sequentially,
/// ran in compute function
if (true) {
@alphamikle
alphamikle / loading_items_with_isolate.dart
Created January 31, 2021 13:53
Loading items with isolate
/// This method is the entry point to the operation
Future<void> loadItemsWithIsolate() async {
/// We start the frame counter before the whole operation
_startFpsMeter();
isLoading = true;
notifyListeners();
/// We start counting the request time
bench.startTimer('Load items in separate isolate');
@alphamikle
alphamikle / backend_method_for_loading_items_with_isolate.dart
Created January 31, 2021 13:54
Backend method for loading items with isolate
/// Event handler [Events.startLoadingItems]
Future<void> _loadingItems() async {
_items.clear();
for (int i = 0; i < 10; i++) {
_items.addAll(await makeManyRequests(5));
if (i < (10 - 1)) {
/// For all requests except the last one - we send only one event
send(Events.loadingItems);
} else {
/// For the last of 10 requests - send a message with data
@alphamikle
alphamikle / cache_items_function.dart
Created January 31, 2021 13:54
Cache items function
/// Function for creating a copy of elements
/// used as source for filtering
void cacheItems() {
_notFilteredItems.clear();
final List<Item> multipliedItems = [];
for (int i = 0; i < 10; i++) {
multipliedItems.addAll(items);
}
_notFilteredItems.addAll(multipliedItems);
}
@alphamikle
alphamikle / search_function.dart
Created January 31, 2021 13:55
Search function
/// Function that launches a test script
/// for entering characters into a text input
Future<void> _testSearch() async {
List<String> words = items.map((Item item) => item.profile.replaceAll('https://opencollective.com/', '')).toSet().toList();
words = words
.map((String word) {
final String newWord = word.substring(0, min(word.length, 3));
return newWord;
})
.toSet()
@alphamikle
alphamikle / set_word_function.dart
Created January 31, 2021 13:56
Set word function
/// We enter characters with a delay of 800ms,
/// but if the data from the asynchronous filter (computed / isolate)
/// has not yet arrived, then we are waiting for them
Future<void> _setWord(String word) async {
if (!canPlaceNextLetter) {
await wait(800);
await _setWord(word);
} else {
searchController.value = TextEditingValue(text: word);
await wait(800);
@alphamikle
alphamikle / filter_items_function.dart
Created January 31, 2021 13:57
Filter items function
/// Depending on the set flag [USE_SIMILARITY]
/// whether or not search with string similarity is used
List<Item> filterItems(Packet2<List<Item>, String> itemsAndInputValue) {
return itemsAndInputValue.value.where((Item item) {
return item.profile.contains(itemsAndInputValue.value2) || (USE_SIMILARITY && isStringsSimilar(item.profile, itemsAndInputValue.value2));
}).toList();
}
bool isStringsSimilar(String first, String second) {
return max(StringSimilarity.compareTwoStrings(first, second), StringSimilarity.compareTwoStrings(second, first)) >= 0.3);
@alphamikle
alphamikle / search_in_the_main_thread.dart
Created January 31, 2021 13:58
Search in the main thread
Future<void> runSearchOnMainThread() async {
cacheItems();
isLoading = true;
notifyListeners();
searchController.addListener(_searchOnMainThread);
await _testSearch();
searchController.removeListener(_searchOnMainThread);
isLoading = false;
notifyListeners();
}