Skip to content

Instantly share code, notes, and snippets.

View naturalwarren's full-sized avatar
🎯
Focusing

Warren Smith naturalwarren

🎯
Focusing
  • Waterloo Canada
View GitHub Profile
@naturalwarren
naturalwarren / KotlinRxJava2CallAdapterFactory.kt
Last active January 28, 2019 22:15
A CallAdapterFactory capable of creating NetworkResponse instances.
/**
* A [CallAdapter.Factory] which allows [NetworkResponse] objects to be
* returned from RxJava streams created by Retrofit.
*
* Adding this class to [Retrofit] allows you to write service methods like:
*
* fun getTokens(): Single<NetworkResponse<AccessToken,Error>>
*/
class KotlinRxJava2CallAdapterFactory : CallAdapter.Factory() {
@naturalwarren
naturalwarren / UIComponent.py
Last active April 8, 2023 19:17
SDUI UI Base Component Type
class UIComponent(BaseModel, ABC):
sdui_component_type: ComponentType
def __init__(self, **data) -> None:
data['sdui_component_type'] = self.component_type()
super().__init__(**data)
@classmethod
@abstractmethod
@naturalwarren
naturalwarren / DataRow.py
Last active April 18, 2023 23:05
SDUI DataRow Type
class DataRow(UIComponent):
title: str
subtitle: str
layout: Layout = Layout.HORIZONTAL
@classmethod
def component_type(cls) -> ComponentType:
return ComponentType.DATA_ROW_STACKED
class Layout(str, Enum):
@naturalwarren
naturalwarren / UIComponent.py
Last active April 8, 2023 19:19
SDUI Base Component Compatibility
class UIComponent(BaseModel, ABC):
# ...
def __init__(self, **data) -> None:
# SDUIContext is set as a context var when handling any request on the backend
current_context = SDUIContext.current()
if current_context is not None:
# Will raise an error if incompatible
current_context.validate_compatibility(
@naturalwarren
naturalwarren / Row.py
Last active April 8, 2023 19:20
SDUI Row With Compatibility
class Row(UIComponent):
title: str
subtitle: Optional[str] = None
@classmethod
def component_type(cls) -> ComponentType:
return ComponentType.BUTTON
@classmethod
def platform_compatibility(cls) -> Dict[Platform, Version]:
@naturalwarren
naturalwarren / SDUIComponent.swift
Created April 8, 2023 19:26
Swift SDUIComponent
enum SDUIComponent: Equatable, Codable {
case button(SDUIButton)
case row(SDUIRow)
case text(SDUIText)
}
@naturalwarren
naturalwarren / SDUIComponent.kt
Created April 8, 2023 19:27
Kotlin SDUIComponent
sealed interface SDUIComponent
data class SDUIButton: SDUIComponent
data class SDUIRow: SDUIComponent
data class SDUIText: SDUIComponent
@naturalwarren
naturalwarren / SDUIComponent.ts
Created April 8, 2023 19:28
TypeScript SDUIComponent
SDUIComponent = SDUIButton | SDUIRow | SDUIText
@naturalwarren
naturalwarren / DataRow+ViewModelConvertible.swift
Last active April 19, 2023 00:19
DataRow+ViewModelConvertible
extension DataRow: ViewModelConvertible {
func viewModel(context: SDUIContext) -> some ViewModel {
return DataRowViewModel(
title: self.title,
titleURL: self.titleHyperlink,
subtitle: self.subtitle,
layout: self.layout.toNativeLayout(),
accessory: accessoryView?.viewModel(context: context)
)
}
@naturalwarren
naturalwarren / SDUIComponent.swift
Created April 8, 2023 19:31
SDUIComponent Helper
extension SDUIComponent {
private var concreteModel: Any {
switch self {
case .button(let button):
return button
case .row(let row):
return row
case .text(let text):
return text
}