Skip to content

Instantly share code, notes, and snippets.

View mobibob's full-sized avatar
🏠
Working from home

Bob James mobibob

🏠
Working from home
View GitHub Profile
@mobibob
mobibob / ColorExtension.swift
Last active September 13, 2024 22:02
SwiftUI Color Extension - contrasting for text on background
extension Color {
static var forExampleMyDarkRed: Color {
return Color(hue: 1.0, saturation: 1.0, brightness: 0.6, opacity: 0.8)
}
init(hex: String) {
let scanner = Scanner(string: hex)
_ = scanner.scanString("#") // Skip the '#' prefix if present
var rgb: UInt64 = 0
@mobibob
mobibob / ScrollViewUI.swift
Created August 27, 2024 14:02
SwiftUI pattern for scroll-view conditional on macOs and iOs
// Conditional compile for macOs and iOS code.
ScrollViewReader { scrollView in
ScrollView {
List {
ForEach(items, id: \.self) { item in
Text(item)
}
}
.onChange(of: selectedItem) { newValue in
@mobibob
mobibob / Orientation.swift
Created May 15, 2024 23:13
SwiftUI - Detect orientation changes
import Foundation
import SwiftUI
class OrientationObserver: NSObject {
let action: (UIDeviceOrientation) -> Void
init(action: @escaping (UIDeviceOrientation) -> Void) {
self.action = action
super.init()
registerForOrientationChanges()
@mobibob
mobibob / Calculator.swift
Created March 30, 2023 19:10
SwiftUI decimal (Double) formatter as-if in a calculator.
import Foundation
import SwiftUI
struct Calculator: View {
func fmtDouble(dbl: Double) -> String? {
let formatter = NumberFormatter()
formatter.numberStyle = .currencyAccounting
formatter.minimumFractionDigits = 3
formatter.formatWidth = 70
@mobibob
mobibob / StudyLocalDateTime.kt
Created March 18, 2023 19:38
Format and math using LocalDate, LocalTime and LocalDate time in Kotlin / Compose. Preview enabled.
package com.mobidawg.android.beta.studylocaldatetime
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.*
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
@mobibob
mobibob / flutter_lots_of_widgets.dart
Created August 9, 2021 18:56
From a tutorial, an accumulation of widgets from buttons, date-picker, sliders, alert dialog, etc. Some stuff is commented out to reduce the clutter for new widgets. Have fun as sample Dart / Flutter code.
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/material.dart';
void main() {
runApp(new MaterialApp(
home: new MyApp()));
}
@mobibob
mobibob / flutter_http_json_listview
Created August 9, 2021 18:50
From a tutorial, this simple app covers getting a JSON response from an HTTP get request and displaying the content in a scrolling ListView.
/// NOTE: Need some yamspec.yml to complete from a default configuration.
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:async';
import 'dart:io';
@mobibob
mobibob / kt
Created January 14, 2021 19:21
kotlin deprecated ViewModelProviders (alternative)
class DashboardFragment : Fragment() {
private lateinit var dashboardViewModel: DashboardViewModel
private var btnPodcast: Button? = null
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
@mobibob
mobibob / splash.kt
Created August 17, 2020 22:39
Delay startActivity
private val msec = 1000L
private val hsec = 100L
private val delaySplash: Long = 2 * msec + 8 * hsec
Timer("splash_delay").schedule( delaySplash) {
intent = Intent(applicationContext, SignonActivity::class.java)
startActivity(intent)
}
@mobibob
mobibob / base64_tiff-decoder.html
Created March 17, 2020 12:58
HTML template to decode and display base64 TIFF
<html>
<body>
<img src='data:image/tiff;base64, <manually paste tiff encoded as base64 here>' />
</body>
</html>