Skip to content

Instantly share code, notes, and snippets.

View lordcodes's full-sized avatar
⌨️
Coding

Andrew Lord lordcodes

⌨️
Coding
View GitHub Profile
@lordcodes
lordcodes / Identifier.kt
Last active February 11, 2020 07:29
Code for my article "Enforcing type safety of IDs in Kotlin"
data class Identifier(val rawValue: UUID)
@lordcodes
lordcodes / Models.kt
Last active February 11, 2020 07:29
Code for my article "Enforcing type safety of IDs in Kotlin"
data class Team(val id: UUID, val size: Int)
data class TeamMember(val id: String, val name: String)
@lordcodes
lordcodes / AuthenticationState.kt
Created February 5, 2020 08:25
Code for article "The power of sealed classes in Kotlin"
sealed class AuthenticationState
data class SignedIn(val userGuid: UUID) : AuthenticationState()
data class StoredCredentials(val credentials: Credentials) : AuthenticationState()
object SignedOut : AuthenticationState()
@lordcodes
lordcodes / AnalyticsEvent.kt
Created February 3, 2020 09:44
Code for article: "The power of sealed classes in Kotlin"
sealed class AnalyticsEvent {
object AccountCreated : AnalyticsEvent()
data class MessageSent(val conversation: Conversation) : AnalyticsEvent()
data class ProfileOpened(val participant: Participant) : AnalyticsEvent()
fun parameters(): Map<Parameter, String> = when (this) {
AccountCreated -> mapOf()
is MessageSent -> mapOf(
Parameter.PARTICIPANT_COUNT to conversation.participants.size.toString(),
Parameter.IS_ARCHIVED to conversation.isArchived.toString()
@lordcodes
lordcodes / RenderViewState.kt
Created February 3, 2020 09:43
Code for article: "The power of sealed classes in Kotlin"
fun renderViewState(viewState: ViewState) = when (viewState) {
LoadingState -> showLoadingViews()
is PresentingState -> showPresentingViews(viewState.viewData)
is ErrorState -> showErrorViews(viewState.message)
}
@lordcodes
lordcodes / ViewState.kt
Created February 3, 2020 09:42
Code for article: "The power of sealed classes in Kotlin"
sealed class ViewState
object LoadingState : ViewState()
data class PresentingState(val viewData: ContactsViewData) : ViewState()
data class ErrorState(val message: String) : ViewState()
@lordcodes
lordcodes / OnAuthenticationStateChanged.kt
Created February 3, 2020 09:41
Code for article: "The power of sealed classes in Kotlin"
fun onAuthenticationStateChanged(newState: AuthenticationState) = when (newState) {
is AuthenticationState.SignedIn -> showSignedIn(newState.userGuid)
is AuthenticationState.StoredCredentials -> showSignedIn(newState.credentials)
AuthenticationState.SignedOut -> showSignedOut()
}
@lordcodes
lordcodes / AuthenticationState.kt
Created February 3, 2020 09:40
Code for article: "The power of sealed classes in Kotlin"
sealed class AuthenticationState {
data class SignedIn(val userGuid: UUID) : AuthenticationState()
data class StoredCredentials(val credentials: Credentials) : AuthenticationState()
object SignedOut : AuthenticationState()
}
@lordcodes
lordcodes / PlanningActivity.kt
Last active February 4, 2020 08:00
Code for article "Using Kotlin to bind Android views"
fun <ViewT : View> Activity.bindView(@IdRes idRes: Int): Lazy<ViewT> {
return lazyUnsychronized {
findViewById<ViewT>(idRes)
}
}
class PlanningActivity : AppCompatActivity() {
private val planningText by bindView<TextView>(R.id.planning_text)
// or
private val planningText: TextView by bindView(R.id.planning_text)
@lordcodes
lordcodes / PlanningActivity.kt
Last active February 4, 2020 08:00
Code for article "Using Kotlin to bind Android views"
import kotlinx.android.synthetic.main.activity_planning.*
class PlanningActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_planning)
planning_text.text = "Hello"
}
}