Skip to content

Instantly share code, notes, and snippets.

View lslv1243's full-sized avatar

Leonardo da Silva lslv1243

View GitHub Profile
@lslv1243
lslv1243 / main.swift
Last active October 10, 2023 12:14
A test for the resolution of the `@MainActor` wrapper in Swift
import Foundation
protocol AProtocol {
func printIsMainThread()
}
struct AStruct: AProtocol {
@MainActor
func printIsMainThread() {
print("(struct) isMainThread=\(Thread.isMainThread)")
@lslv1243
lslv1243 / Republished.swift
Last active May 29, 2023 21:03
`@Republished` property wrapper to forward changes from nested `ObservableObject`.
import Combine
import SwiftUI
struct ContentView: View {
@StateObject var observable = OuterObservable()
var body: some View {
VStack {
Display(title: "inner", value: observable.inner.binding(\.value))
Display(title: "outer", value: $observable.value)
@lslv1243
lslv1243 / infinite_list_controller.dart
Last active August 31, 2022 11:32
Flutter Infinite List Controller
import 'package:flutter/foundation.dart';
class InfiniteListPage<T> {
final List<T> items;
final int totalCount;
InfiniteListPage(this.items, this.totalCount);
}
class InfiniteListController<T> extends ChangeNotifier {
@lslv1243
lslv1243 / break_modal_route.dart
Created February 11, 2022 01:40
ModalRoute didPop breaks in flutter 2.10.1 and used to work in version 2.5.3
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
@lslv1243
lslv1243 / image_editor.dart
Created August 12, 2021 14:28
Flutter Image Editor
import 'dart:async';
import 'dart:typed_data';
import 'dart:ui';
const _BYTES_PER_PIXEL = 4;
class ImageEditor {
final int imageWidth;
final int imageHeight;
final ByteData _byteData;
@lslv1243
lslv1243 / positioned_overlay_entry.dart
Created July 28, 2021 19:45
Can be used to enable touch events to happen in a Positioned widget which is outside of the stack boundaries.
class PositionedOverlayEntry extends StatefulWidget {
final OverlayState overlay;
final Widget child;
final double? left;
final double? top;
final double? right;
final double? bottom;
final double? width;
final double? height;
@lslv1243
lslv1243 / main.dart
Last active July 10, 2022 15:52
simple dart file upload server
import 'dart:io';
import 'package:mime/mime.dart';
void main(List<String> arguments) async {
const port = 8080;
var lastId = (await _loadLastId()) ?? -1;
final server = await HttpServer.bind(InternetAddress.anyIPv4, port);
server.listen((request) async {
void close(int statusCode) {
request.response.statusCode = 404;
@lslv1243
lslv1243 / replace_localized_text.js
Created April 26, 2021 19:39
Apple Itunes Connect replace localized text
/**
* Script to replace localized text on the page (any element with the data-loc-key attribute)
*/
function replaceLocalizedText(locObject) {
document.querySelectorAll('[data-loc-key]').forEach(function(textElement){
const locKey = textElement.getAttribute('data-loc-key');
textElement.innerHTML = locObject[locKey];
});