SlideShare a Scribd company logo
“Basically Redux”
Cody Engel
@POTUS404
Senior Engineer
@POTUS404
Redux Basics
@POTUS404Redux Basics
Actions
Actions are payloads of information that send
data from your application to your store. They
are the only source of information for the
store. Actions do not describe how the actions
will change the state.
https://redux.js.org/basics/actions
@POTUS404Redux Basics
Actions
{
type: ADD_TODO,
text: 'Build my first Redux app'
}
@POTUS404Redux Basics
Reducers
Reducers specify how the application's state
changes in response to actions sent to the
store. Remember that actions only describe
what happened, but don't describe how the
application's state changes.
https://redux.js.org/basics/reducers
@POTUS404Redux Basics
Reducers
function todoApp(state = initialState, action) {
switch (action.type) {
case SET_VISIBILITY_FILTER:
return Object.assign({}, state, {
visibilityFilter: action.filter
})
default:
return state
}
}
@POTUS404Redux Basics
The Store
The Store brings the actions and reducers
together. It holds the application’s state and
allows state to be updated and observed on.
You’ll only ever have one store in a Redux
application.
https://redux.js.org/basics/store
@POTUS404Redux Basics
Data Flow
Redux follows a strict unidirectional data flow.
This means data in your systems follows a
predictable path which is easier to
understand.
https://redux.js.org/basics/data-flow
@POTUS404Redux Basics
Data Flow
Actions Reducers Store
State Tree Updated
@POTUS404
Algebraic Data
Types
@POTUS404Algebraic Data Types
Product Type
A product type is a structure made up of
multiple other types.
@POTUS404Algebraic Data Types
Product Type
data class Gumball(
val size: GumballSize,
val color: GumballColor
)
@POTUS404Algebraic Data Types
Sum Type
A structure used to hold a value that could
take on several different, but fixed types.
@POTUS404Algebraic Data Types
Sum Type
sealed class GumballSize {
object Small : GumballSize()
object Medium : GumballSize()
object Large : GumballSize()
}
@POTUS404
Pure Functions
@POTUS404Pure Functions
Pure Functions
A pure function is a function where the return
value is only determined by its input values,
without observable side effects.
@POTUS404Pure Functions
fun add(left: Int, right: Int): Int {
return left + right
}
Pure Functions
@POTUS404Pure Functions
fun giveMeAGumball(
gumballSize: GumballSize,
gumballColor: GumballColor
): Gumball {
return Gumball(gumballSize, gumballColor)
}
Pure Functions
@POTUS404
Finite-state
Machines
@POTUS404Finite-state Machine
A finite state machine is one that has a
limited or finite number of possible states.
Finite-state Machines
@POTUS404Finite-state Machines
@POTUS404
• How many gumballs are left?
• Is it currently dispensing?
Gumball Machine
States
Finite-state Machines
@POTUS404
REDUCE!
@POTUS404REDUCE!
fun main(args: Array<String>) {
val gumballMachine = GumballMachine()
while(true) {
if (gumballMachine.hasGumballs) {
println("Purchased a ${gumballMachine.buyGumball()}")
} else {
println("Sorry, no more gumballs.")
print("Would you like to refill the gumball machine? y/n: ")
if (readLine() == "y") {
gumballMachine.refill()
println()
} else {
return
}
}
}
}
Gumball Machine Usage
@POTUS404REDUCE!
fun main(args: Array<String>) {
val gumballMachine = GumballMachine()
while(true) {
if (gumballMachine.hasGumballs) {
println("Purchased a ${gumballMachine.buyGumball()}")
} else {
println("Sorry, no more gumballs.")
print("Would you like to refill the gumball machine? y/n: ")
if (readLine() == "y") {
gumballMachine.refill()
println()
} else {
return
}
}
}
}
Gumball Machine Usage
@POTUS404REDUCE!
fun main(args: Array<String>) {
val gumballMachine = GumballMachine()
while(true) {
if (gumballMachine.hasGumballs) {
println("Purchased a ${gumballMachine.buyGumball()}")
} else {
println("Sorry, no more gumballs.")
print("Would you like to refill the gumball machine? y/n: ")
if (readLine() == "y") {
gumballMachine.refill()
println()
} else {
return
}
}
}
}
Gumball Machine Usage
@POTUS404REDUCE!
fun main(args: Array<String>) {
val gumballMachine = GumballMachine()
while(true) {
if (gumballMachine.hasGumballs) {
println("Purchased a ${gumballMachine.buyGumball()}")
} else {
println("Sorry, no more gumballs.")
print("Would you like to refill the gumball machine? y/n: ")
if (readLine() == "y") {
gumballMachine.refill()
println()
} else {
return
}
}
}
}
Gumball Machine Usage
@POTUS404REDUCE!
fun main(args: Array<String>) {
val gumballMachine = GumballMachine()
while(true) {
if (gumballMachine.hasGumballs) {
println("Purchased a ${gumballMachine.buyGumball()}")
} else {
println("Sorry, no more gumballs.")
print("Would you like to refill the gumball machine? y/n: ")
if (readLine() == "y") {
gumballMachine.refill()
println()
} else {
return
}
}
}
}
Gumball Machine Usage
@POTUS404REDUCE!
The Gumball Machine
class GumballMachine(private val timeToDispense: Long = 50) {
object Gumball {
override fun toString() = "$color Gumball"
private val color
get() = when(Random().nextInt(8) + 1) {
1 -> "Blue"
2 -> "Red"
3 -> "Green"
4 -> "Yellow"
5 -> "Pink"
6 -> "Purple"
7 -> "Orange"
else -> "White"
}
}
val hasGumballs
get() = state.remainingGumballs > 0
private val gumballReducer = GumballReducer()
private var state = GumballState()
fun buyGumball(): Gumball {
state = gumballReducer.reduce(state, GumballAction.BuyGumball)
Thread.sleep(timeToDispense)
state = gumballReducer.reduce(state, GumballAction.Dispensed)
return Gumball
}
fun refill() {
state = gumballReducer.reduce(state, GumballAction.RefillGumballs)
}
}
@POTUS404REDUCE!
The Gumball Machine
class GumballMachine(private val timeToDispense: Long = 50) {
object Gumball {
override fun toString() = "$color Gumball"
private val color
get() = when(Random().nextInt(8) + 1) {
1 -> "Blue"
2 -> "Red"
3 -> "Green"
4 -> "Yellow"
5 -> "Pink"
6 -> "Purple"
7 -> "Orange"
else -> "White"
}
}
val hasGumballs
get() = state.remainingGumballs > 0
private val gumballReducer = GumballReducer()
private var state = GumballState()
fun buyGumball(): Gumball {
state = gumballReducer.reduce(state, GumballAction.BuyGumball)
Thread.sleep(timeToDispense)
state = gumballReducer.reduce(state, GumballAction.Dispensed)
return Gumball
}
fun refill() {
state = gumballReducer.reduce(state, GumballAction.RefillGumballs)
}
}
@POTUS404REDUCE!
The Gumball Machine
class GumballMachine(private val timeToDispense: Long = 50) {
object Gumball {
override fun toString() = "$color Gumball"
private val color
get() = when(Random().nextInt(8) + 1) {
1 -> "Blue"
2 -> "Red"
3 -> "Green"
4 -> "Yellow"
5 -> "Pink"
6 -> "Purple"
7 -> "Orange"
else -> "White"
}
}
@POTUS404REDUCE!
The Gumball Machine
class GumballMachine(private val timeToDispense: Long = 50) {
object Gumball {
override fun toString() = "$color Gumball"
private val color
get() = when(Random().nextInt(8) + 1) {
1 -> "Blue"
2 -> "Red"
3 -> "Green"
4 -> "Yellow"
5 -> "Pink"
6 -> "Purple"
7 -> "Orange"
else -> "White"
}
}
val hasGumballs
get() = state.remainingGumballs > 0
private val gumballReducer = GumballReducer()
private var state = GumballState()
fun buyGumball(): Gumball {
state = gumballReducer.reduce(state, GumballAction.BuyGumball)
Thread.sleep(timeToDispense)
state = gumballReducer.reduce(state, GumballAction.Dispensed)
return Gumball
}
fun refill() {
state = gumballReducer.reduce(state, GumballAction.RefillGumballs)
}
}
@POTUS404REDUCE!
The Gumball Machine
class GumballMachine(private val timeToDispense: Long = 50) {
object Gumball {
override fun toString() = "$color Gumball"
private val color
get() = when(Random().nextInt(8) + 1) {
1 -> "Blue"
2 -> "Red"
3 -> "Green"
4 -> "Yellow"
5 -> "Pink"
6 -> "Purple"
7 -> "Orange"
else -> "White"
}
}
val hasGumballs
get() = state.remainingGumballs > 0
private val gumballReducer = GumballReducer()
private var state = GumballState()
fun buyGumball(): Gumball {
state = gumballReducer.reduce(state, GumballAction.BuyGumball)
Thread.sleep(timeToDispense)
state = gumballReducer.reduce(state, GumballAction.Dispensed)
return Gumball
}
fun refill() {
state = gumballReducer.reduce(state, GumballAction.RefillGumballs)
}
}
@POTUS404REDUCE!
The Gumball Machine
class GumballMachine(private val timeToDispense: Long = 50) {
object Gumball {
override fun toString() = "$color Gumball"
private val color
get() = when(Random().nextInt(8) + 1) {
1 -> "Blue"
2 -> "Red"
3 -> "Green"
4 -> "Yellow"
5 -> "Pink"
6 -> "Purple"
7 -> "Orange"
else -> "White"
}
}
val hasGumballs
get() = state.remainingGumballs > 0
private val gumballReducer = GumballReducer()
private var state = GumballState()
fun buyGumball(): Gumball {
state = gumballReducer.reduce(state, GumballAction.BuyGumball)
Thread.sleep(timeToDispense)
state = gumballReducer.reduce(state, GumballAction.Dispensed)
return Gumball
}
fun refill() {
state = gumballReducer.reduce(state, GumballAction.RefillGumballs)
}
}
@POTUS404REDUCE!
The Gumball Machine
class GumballMachine(private val timeToDispense: Long = 50) {
object Gumball {
override fun toString() = "$color Gumball"
private val color
get() = when(Random().nextInt(8) + 1) {
1 -> "Blue"
2 -> "Red"
3 -> "Green"
4 -> "Yellow"
5 -> "Pink"
6 -> "Purple"
7 -> "Orange"
else -> "White"
}
}
val hasGumballs
get() = state.remainingGumballs > 0
private val gumballReducer = GumballReducer()
private var state = GumballState()
fun buyGumball(): Gumball {
state = gumballReducer.reduce(state, GumballAction.BuyGumball)
Thread.sleep(timeToDispense)
state = gumballReducer.reduce(state, GumballAction.Dispensed)
return Gumball
}
fun refill() {
state = gumballReducer.reduce(state, GumballAction.RefillGumballs)
}
}
@POTUS404REDUCE!
The Gumball Machine
class GumballMachine(private val timeToDispense: Long = 50) {
object Gumball {
override fun toString() = "$color Gumball"
private val color
get() = when(Random().nextInt(8) + 1) {
1 -> "Blue"
2 -> "Red"
3 -> "Green"
4 -> "Yellow"
5 -> "Pink"
6 -> "Purple"
7 -> "Orange"
else -> "White"
}
}
val hasGumballs
get() = state.remainingGumballs > 0
private val gumballReducer = GumballReducer()
private var state = GumballState()
fun buyGumball(): Gumball {
state = gumballReducer.reduce(state, GumballAction.BuyGumball)
Thread.sleep(timeToDispense)
state = gumballReducer.reduce(state, GumballAction.Dispensed)
return Gumball
}
fun refill() {
state = gumballReducer.reduce(state, GumballAction.RefillGumballs)
}
}
@POTUS404REDUCE!
sealed class GumballAction : Action {
object BuyGumball : GumballAction()
object RefillGumballs : GumballAction()
object Dispensed : GumballAction()
}
The Gumball Actions
@POTUS404REDUCE!
data class GumballState(
val remainingGumballs: Int = 100,
val isDispensing: Boolean = false
) : State
The Gumball State
@POTUS404REDUCE!
class GumballReducer : Reducer<GumballState, GumballAction> {
override fun reduce(state: GumballState, action: GumballAction): GumballState {
return when(action) {
is GumballAction.BuyGumball -> {
if (state.isDispensing) {
throw IllegalStateException(“…”)
} else if (state.remainingGumballs <= 0) {
throw IllegalStateException(“…”)
}
state.copy(
remainingGumballs = state.remainingGumballs - 1,
isDispensing = true
)
}
is GumballAction.RefillGumballs -> state.copy(
remainingGumballs = 100,
isDispensing = false
)
is GumballAction.Dispensed -> state.copy(isDispensing = false)
}
}
}
The Gumball Reducer
@POTUS404REDUCE!
class GumballReducer : Reducer<GumballState, GumballAction> {
override fun reduce(state: GumballState, action: GumballAction): GumballState{
return when(action) {
is GumballAction.BuyGumball -> {
if (state.isDispensing) {
throw IllegalStateException(“…”)
} else if (state.remainingGumballs <= 0) {
throw IllegalStateException(“…”)
}
state.copy(
remainingGumballs = state.remainingGumballs - 1,
isDispensing = true
)
}
is GumballAction.RefillGumballs -> state.copy(
remainingGumballs = 100,
isDispensing = false
)
is GumballAction.Dispensed -> state.copy(isDispensing = false)
}
}
}
The Gumball Reducer
@POTUS404REDUCE!
class GumballReducer : Reducer<GumballState, GumballAction> {
override fun reduce(state: GumballState, action: GumballAction): GumballState {
return when(action) {
is GumballAction.BuyGumball -> {
if (state.isDispensing) {
throw IllegalStateException(“…”)
} else if (state.remainingGumballs <= 0) {
throw IllegalStateException(“…”)
}
state.copy(
remainingGumballs = state.remainingGumballs - 1,
isDispensing = true
)
}
is GumballAction.RefillGumballs -> state.copy(
remainingGumballs = 100,
isDispensing = false
)
is GumballAction.Dispensed -> state.copy(isDispensing = false)
}
}
}
The Gumball Reducer
@POTUS404REDUCE!
class GumballReducer : Reducer<GumballState, GumballAction> {
override fun reduce(state: GumballState, action: GumballAction): GumballState {
return when(action) {
is GumballAction.BuyGumball -> {
if (state.isDispensing) {
throw IllegalStateException(“…”)
} else if (state.remainingGumballs <= 0) {
throw IllegalStateException(“…”)
}
state.copy(
remainingGumballs = state.remainingGumballs - 1,
isDispensing = true
)
}
is GumballAction.RefillGumballs -> state.copy(
remainingGumballs = 100,
isDispensing = false
)
is GumballAction.Dispensed -> state.copy(isDispensing = false)
}
}
}
The Gumball Reducer
@POTUS404REDUCE!
class GumballReducer : Reducer<GumballState, GumballAction> {
override fun reduce(state: GumballState, action: GumballAction): GumballState {
return when(action) {
is GumballAction.BuyGumball -> {
if (state.isDispensing) {
throw IllegalStateException(“…”)
} else if (state.remainingGumballs <= 0) {
throw IllegalStateException(“…”)
}
state.copy(
remainingGumballs = state.remainingGumballs - 1,
isDispensing = true
)
}
is GumballAction.RefillGumballs -> state.copy(
remainingGumballs = 100,
isDispensing = false
)
is GumballAction.Dispensed -> state.copy(isDispensing = false)
}
}
}
The Gumball Reducer
@POTUS404REDUCE!
class GumballReducer : Reducer<GumballState, GumballAction> {
override fun reduce(state: GumballState, action: GumballAction): GumballState {
return when(action) {
is GumballAction.BuyGumball -> {
if (state.isDispensing) {
throw IllegalStateException(“…”)
} else if (state.remainingGumballs <= 0) {
throw IllegalStateException(“…”)
}
state.copy(
remainingGumballs = state.remainingGumballs - 1,
isDispensing = true
)
}
is GumballAction.RefillGumballs -> state.copy(
remainingGumballs = 100,
isDispensing = false
)
is GumballAction.Dispensed -> state.copy(isDispensing = false)
}
}
}
The Gumball Reducer
@POTUS404
RxJava
@POTUS404RxJava
Observable.fromPublisher<CompanyAction> { subscriber ->
0.until(320).map {
if (it % 11 == 0) CompanyAction.Add(Requisition.requisition) else CompanyAction.Hire(Requisition.requisition,
Employee.employee)
}.forEach { subscriber.onNext(it) }.also { subscriber.onComplete() }
}
.reduceWith({ Company("ActiveCampaign") }, { prevState, action ->
when(action) {
is CompanyAction.Hire -> {
val employees = prevState.employees.toMutableList()
employees.add(action.employee)
prevState.copy(employees = employees)
}
is CompanyAction.Add -> {
val requisitions = prevState.requisitions.toMutableList()
requisitions.add(action.requisition)
prevState.copy(requisitions = requisitions)
}
}
})
.subscribe { state ->
println("These numbers are most likely inaccurate.")
println("ActiveCampaign has ${state.requisitions.size} job openings with around ${state.employees.size} employees.")
println("There are ${state.employees.filter { it.name == "Cody" }.size} employees named Cody.")
println("Here is the raw state output:")
println(state)
}
@POTUS404RxJava
Observable.fromPublisher<CompanyAction> { subscriber ->
0.until(320).map {
if (it % 11 == 0) CompanyAction.Add(Requisition.requisition)
else CompanyAction.Hire(Requisition.requisition, Employee.employee)
}
.forEach { subscriber.onNext(it) }
.also { subscriber.onComplete() }
}
.reduceWith({ Company("ActiveCampaign") }, { prevState, action ->
when(action) {
is CompanyAction.Hire -> {
val employees = prevState.employees.toMutableList()
employees.add(action.employee)
prevState.copy(employees = employees)
}
is CompanyAction.Add -> {
val requisitions = prevState.requisitions.toMutableList()
requisitions.add(action.requisition)
prevState.copy(requisitions = requisitions)
}
}
})
.subscribe { state ->
println("These numbers are most likely inaccurate.")
println("ActiveCampaign has ${state.requisitions.size} job openings with around ${state.employees.size} employees.")
println("There are ${state.employees.filter { it.name == "Cody" }.size} employees named Cody.")
println("Here is the raw state output:")
println(state)
}
@POTUS404RxJava
Observable.fromPublisher<CompanyAction> { subscriber ->
0.until(320).map {
if (it % 11 == 0) CompanyAction.Add(Requisition.requisition) else CompanyAction.Hire(Requisition.requisition, Employee.employee)
}.forEach { subscriber.onNext(it) }.also { subscriber.onComplete() }
}
.reduceWith({ Company("ActiveCampaign") }, { prevState, action ->
when(action) {
is CompanyAction.Hire -> {
val employees = prevState.employees.toMutableList()
employees.add(action.employee)
prevState.copy(employees = employees)
}
is CompanyAction.Add -> {
val requisitions = prevState.requisitions.toMutableList()
requisitions.add(action.requisition)
prevState.copy(requisitions = requisitions)
}
}
})
.subscribe { state ->
println("These numbers are most likely inaccurate.")
println("ActiveCampaign has ${state.requisitions.size} job openings with around ${state.employees.size} employees.")
println("There are ${state.employees.filter { it.name == "Cody" }.size} employees named Cody.")
println("Here is the raw state output:")
println(state)
}
@POTUS404RxJava
Observable.fromPublisher<CompanyAction> { subscriber ->
0.until(320).map {
if (it % 11 == 0) CompanyAction.Add(Requisition.requisition) else CompanyAction.Hire(Requisition.requisition, Employee.employee)
}.forEach { subscriber.onNext(it) }.also { subscriber.onComplete() }
}
.reduceWith({ Company("ActiveCampaign") }, { prevState, action ->
when(action) {
is CompanyAction.Hire -> {
val employees = prevState.employees.toMutableList()
employees.add(action.employee)
prevState.copy(employees = employees)
}
is CompanyAction.Add -> {
val requisitions = prevState.requisitions.toMutableList()
requisitions.add(action.requisition)
prevState.copy(requisitions = requisitions)
}
}
})
.subscribe { state ->
println("These numbers are most likely inaccurate.")
println("ActiveCampaign has ${state.requisitions.size} job openings
with around ${state.employees.size} employees.")
println("There are ${state.employees.filter { it.name == "Cody" }.size}
employees named Cody.")
println("Here is the raw state output:")
println(state)
}
These numbers are most likely inaccurate.
ActiveCampaign has 30 job openings with around 290 employees.
There are 19 employees named Cody.
Here is the raw state output:
Company(name=ActiveCampaign,
requisitions=[Requisition(name=Full Stack Engineer, department=Engineering),
…)
@POTUS404RxJava
Cody Engel
@POTUS404
Senior Engineer
(We’re Hiring)
Thanks!

More Related Content

PPTX
I regret nothing
PDF
Stop Guessing and Start Measuring - Benchmarking Practice (Poly Version)
PDF
The Ring programming language version 1.10 book - Part 70 of 212
PDF
How fast is it really? Benchmarking in Practice (Ruby Version)
PDF
How te bring common UI patterns to ADF
PPTX
How to Bring Common UI Patterns to ADF
PDF
Jquery 13 cheatsheet_v1
PDF
Jquery 1.3 cheatsheet_v1
I regret nothing
Stop Guessing and Start Measuring - Benchmarking Practice (Poly Version)
The Ring programming language version 1.10 book - Part 70 of 212
How fast is it really? Benchmarking in Practice (Ruby Version)
How te bring common UI patterns to ADF
How to Bring Common UI Patterns to ADF
Jquery 13 cheatsheet_v1
Jquery 1.3 cheatsheet_v1

What's hot (20)

PPTX
The Groovy Puzzlers – The Complete 01 and 02 Seasons
PPTX
DroidKnight 2018 State machine by Selaed class
PDF
Matteo Antony Mistretta - Refactoring into React hooks - Codemotion Rome 2019
PDF
Spock and Geb
PDF
How else can you write the code in PHP?
PDF
Magic of Ruby
PDF
Chaining et composition de fonctions avec lodash / underscore
PPTX
Working with Groovy Collections
PDF
Google I/O 2021 Recap
PDF
Famo.us: From Zero to UI
PDF
Everything About PowerShell
PDF
MongoDB With Style
PPTX
Indeed My Jobs: A case study in ReactJS and Redux (Meetup talk March 2016)
PDF
知っておきたいSpring Batch Tips
PDF
Instant Dynamic Forms with #states
PPTX
Oh Composable World!
PDF
Symfony World - Symfony components and design patterns
PDF
Pragmatic Real-World Scala (short version)
PDF
ES6 patterns in the wild
PDF
Bubbles & Trees with jQuery
The Groovy Puzzlers – The Complete 01 and 02 Seasons
DroidKnight 2018 State machine by Selaed class
Matteo Antony Mistretta - Refactoring into React hooks - Codemotion Rome 2019
Spock and Geb
How else can you write the code in PHP?
Magic of Ruby
Chaining et composition de fonctions avec lodash / underscore
Working with Groovy Collections
Google I/O 2021 Recap
Famo.us: From Zero to UI
Everything About PowerShell
MongoDB With Style
Indeed My Jobs: A case study in ReactJS and Redux (Meetup talk March 2016)
知っておきたいSpring Batch Tips
Instant Dynamic Forms with #states
Oh Composable World!
Symfony World - Symfony components and design patterns
Pragmatic Real-World Scala (short version)
ES6 patterns in the wild
Bubbles & Trees with jQuery
Ad

Similar to Basically Redux - Android Listeners (6)

PDF
Concurrent applications with free monads and stm
PDF
Compose로 Android:Desktop 멀티플랫폼 만들기.pdf
PDF
Capability Driven Design - Andrzej Jóźwiak - TomTom Dev Day 2021
PPT
Gearmam, from the_worker's_perspective copy
PPT
Gearmam, from the_worker's_perspective copy
PPT
OO JS for AS3 Devs
Concurrent applications with free monads and stm
Compose로 Android:Desktop 멀티플랫폼 만들기.pdf
Capability Driven Design - Andrzej Jóźwiak - TomTom Dev Day 2021
Gearmam, from the_worker's_perspective copy
Gearmam, from the_worker's_perspective copy
OO JS for AS3 Devs
Ad

Recently uploaded (20)

PDF
Hindi spoken digit analysis for native and non-native speakers
PPTX
cloud_computing_Infrastucture_as_cloud_p
PDF
Mushroom cultivation and it's methods.pdf
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PDF
Heart disease approach using modified random forest and particle swarm optimi...
PDF
Enhancing emotion recognition model for a student engagement use case through...
PPTX
Tartificialntelligence_presentation.pptx
PDF
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PPTX
A Presentation on Artificial Intelligence
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
WOOl fibre morphology and structure.pdf for textiles
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
1 - Historical Antecedents, Social Consideration.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PPTX
A Presentation on Touch Screen Technology
PPTX
OMC Textile Division Presentation 2021.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
Hindi spoken digit analysis for native and non-native speakers
cloud_computing_Infrastucture_as_cloud_p
Mushroom cultivation and it's methods.pdf
MIND Revenue Release Quarter 2 2025 Press Release
Univ-Connecticut-ChatGPT-Presentaion.pdf
Heart disease approach using modified random forest and particle swarm optimi...
Enhancing emotion recognition model for a student engagement use case through...
Tartificialntelligence_presentation.pptx
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
SOPHOS-XG Firewall Administrator PPT.pptx
A Presentation on Artificial Intelligence
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
WOOl fibre morphology and structure.pdf for textiles
Digital-Transformation-Roadmap-for-Companies.pptx
1 - Historical Antecedents, Social Consideration.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
NewMind AI Weekly Chronicles - August'25-Week II
A Presentation on Touch Screen Technology
OMC Textile Division Presentation 2021.pptx
Encapsulation_ Review paper, used for researhc scholars

Basically Redux - Android Listeners

  • 3. @POTUS404Redux Basics Actions Actions are payloads of information that send data from your application to your store. They are the only source of information for the store. Actions do not describe how the actions will change the state. https://redux.js.org/basics/actions
  • 5. @POTUS404Redux Basics Reducers Reducers specify how the application's state changes in response to actions sent to the store. Remember that actions only describe what happened, but don't describe how the application's state changes. https://redux.js.org/basics/reducers
  • 6. @POTUS404Redux Basics Reducers function todoApp(state = initialState, action) { switch (action.type) { case SET_VISIBILITY_FILTER: return Object.assign({}, state, { visibilityFilter: action.filter }) default: return state } }
  • 7. @POTUS404Redux Basics The Store The Store brings the actions and reducers together. It holds the application’s state and allows state to be updated and observed on. You’ll only ever have one store in a Redux application. https://redux.js.org/basics/store
  • 8. @POTUS404Redux Basics Data Flow Redux follows a strict unidirectional data flow. This means data in your systems follows a predictable path which is easier to understand. https://redux.js.org/basics/data-flow
  • 9. @POTUS404Redux Basics Data Flow Actions Reducers Store State Tree Updated
  • 11. @POTUS404Algebraic Data Types Product Type A product type is a structure made up of multiple other types.
  • 12. @POTUS404Algebraic Data Types Product Type data class Gumball( val size: GumballSize, val color: GumballColor )
  • 13. @POTUS404Algebraic Data Types Sum Type A structure used to hold a value that could take on several different, but fixed types.
  • 14. @POTUS404Algebraic Data Types Sum Type sealed class GumballSize { object Small : GumballSize() object Medium : GumballSize() object Large : GumballSize() }
  • 16. @POTUS404Pure Functions Pure Functions A pure function is a function where the return value is only determined by its input values, without observable side effects.
  • 17. @POTUS404Pure Functions fun add(left: Int, right: Int): Int { return left + right } Pure Functions
  • 18. @POTUS404Pure Functions fun giveMeAGumball( gumballSize: GumballSize, gumballColor: GumballColor ): Gumball { return Gumball(gumballSize, gumballColor) } Pure Functions
  • 20. @POTUS404Finite-state Machine A finite state machine is one that has a limited or finite number of possible states. Finite-state Machines
  • 22. @POTUS404 • How many gumballs are left? • Is it currently dispensing? Gumball Machine States Finite-state Machines
  • 24. @POTUS404REDUCE! fun main(args: Array<String>) { val gumballMachine = GumballMachine() while(true) { if (gumballMachine.hasGumballs) { println("Purchased a ${gumballMachine.buyGumball()}") } else { println("Sorry, no more gumballs.") print("Would you like to refill the gumball machine? y/n: ") if (readLine() == "y") { gumballMachine.refill() println() } else { return } } } } Gumball Machine Usage
  • 25. @POTUS404REDUCE! fun main(args: Array<String>) { val gumballMachine = GumballMachine() while(true) { if (gumballMachine.hasGumballs) { println("Purchased a ${gumballMachine.buyGumball()}") } else { println("Sorry, no more gumballs.") print("Would you like to refill the gumball machine? y/n: ") if (readLine() == "y") { gumballMachine.refill() println() } else { return } } } } Gumball Machine Usage
  • 26. @POTUS404REDUCE! fun main(args: Array<String>) { val gumballMachine = GumballMachine() while(true) { if (gumballMachine.hasGumballs) { println("Purchased a ${gumballMachine.buyGumball()}") } else { println("Sorry, no more gumballs.") print("Would you like to refill the gumball machine? y/n: ") if (readLine() == "y") { gumballMachine.refill() println() } else { return } } } } Gumball Machine Usage
  • 27. @POTUS404REDUCE! fun main(args: Array<String>) { val gumballMachine = GumballMachine() while(true) { if (gumballMachine.hasGumballs) { println("Purchased a ${gumballMachine.buyGumball()}") } else { println("Sorry, no more gumballs.") print("Would you like to refill the gumball machine? y/n: ") if (readLine() == "y") { gumballMachine.refill() println() } else { return } } } } Gumball Machine Usage
  • 28. @POTUS404REDUCE! fun main(args: Array<String>) { val gumballMachine = GumballMachine() while(true) { if (gumballMachine.hasGumballs) { println("Purchased a ${gumballMachine.buyGumball()}") } else { println("Sorry, no more gumballs.") print("Would you like to refill the gumball machine? y/n: ") if (readLine() == "y") { gumballMachine.refill() println() } else { return } } } } Gumball Machine Usage
  • 29. @POTUS404REDUCE! The Gumball Machine class GumballMachine(private val timeToDispense: Long = 50) { object Gumball { override fun toString() = "$color Gumball" private val color get() = when(Random().nextInt(8) + 1) { 1 -> "Blue" 2 -> "Red" 3 -> "Green" 4 -> "Yellow" 5 -> "Pink" 6 -> "Purple" 7 -> "Orange" else -> "White" } } val hasGumballs get() = state.remainingGumballs > 0 private val gumballReducer = GumballReducer() private var state = GumballState() fun buyGumball(): Gumball { state = gumballReducer.reduce(state, GumballAction.BuyGumball) Thread.sleep(timeToDispense) state = gumballReducer.reduce(state, GumballAction.Dispensed) return Gumball } fun refill() { state = gumballReducer.reduce(state, GumballAction.RefillGumballs) } }
  • 30. @POTUS404REDUCE! The Gumball Machine class GumballMachine(private val timeToDispense: Long = 50) { object Gumball { override fun toString() = "$color Gumball" private val color get() = when(Random().nextInt(8) + 1) { 1 -> "Blue" 2 -> "Red" 3 -> "Green" 4 -> "Yellow" 5 -> "Pink" 6 -> "Purple" 7 -> "Orange" else -> "White" } } val hasGumballs get() = state.remainingGumballs > 0 private val gumballReducer = GumballReducer() private var state = GumballState() fun buyGumball(): Gumball { state = gumballReducer.reduce(state, GumballAction.BuyGumball) Thread.sleep(timeToDispense) state = gumballReducer.reduce(state, GumballAction.Dispensed) return Gumball } fun refill() { state = gumballReducer.reduce(state, GumballAction.RefillGumballs) } }
  • 31. @POTUS404REDUCE! The Gumball Machine class GumballMachine(private val timeToDispense: Long = 50) { object Gumball { override fun toString() = "$color Gumball" private val color get() = when(Random().nextInt(8) + 1) { 1 -> "Blue" 2 -> "Red" 3 -> "Green" 4 -> "Yellow" 5 -> "Pink" 6 -> "Purple" 7 -> "Orange" else -> "White" } }
  • 32. @POTUS404REDUCE! The Gumball Machine class GumballMachine(private val timeToDispense: Long = 50) { object Gumball { override fun toString() = "$color Gumball" private val color get() = when(Random().nextInt(8) + 1) { 1 -> "Blue" 2 -> "Red" 3 -> "Green" 4 -> "Yellow" 5 -> "Pink" 6 -> "Purple" 7 -> "Orange" else -> "White" } } val hasGumballs get() = state.remainingGumballs > 0 private val gumballReducer = GumballReducer() private var state = GumballState() fun buyGumball(): Gumball { state = gumballReducer.reduce(state, GumballAction.BuyGumball) Thread.sleep(timeToDispense) state = gumballReducer.reduce(state, GumballAction.Dispensed) return Gumball } fun refill() { state = gumballReducer.reduce(state, GumballAction.RefillGumballs) } }
  • 33. @POTUS404REDUCE! The Gumball Machine class GumballMachine(private val timeToDispense: Long = 50) { object Gumball { override fun toString() = "$color Gumball" private val color get() = when(Random().nextInt(8) + 1) { 1 -> "Blue" 2 -> "Red" 3 -> "Green" 4 -> "Yellow" 5 -> "Pink" 6 -> "Purple" 7 -> "Orange" else -> "White" } } val hasGumballs get() = state.remainingGumballs > 0 private val gumballReducer = GumballReducer() private var state = GumballState() fun buyGumball(): Gumball { state = gumballReducer.reduce(state, GumballAction.BuyGumball) Thread.sleep(timeToDispense) state = gumballReducer.reduce(state, GumballAction.Dispensed) return Gumball } fun refill() { state = gumballReducer.reduce(state, GumballAction.RefillGumballs) } }
  • 34. @POTUS404REDUCE! The Gumball Machine class GumballMachine(private val timeToDispense: Long = 50) { object Gumball { override fun toString() = "$color Gumball" private val color get() = when(Random().nextInt(8) + 1) { 1 -> "Blue" 2 -> "Red" 3 -> "Green" 4 -> "Yellow" 5 -> "Pink" 6 -> "Purple" 7 -> "Orange" else -> "White" } } val hasGumballs get() = state.remainingGumballs > 0 private val gumballReducer = GumballReducer() private var state = GumballState() fun buyGumball(): Gumball { state = gumballReducer.reduce(state, GumballAction.BuyGumball) Thread.sleep(timeToDispense) state = gumballReducer.reduce(state, GumballAction.Dispensed) return Gumball } fun refill() { state = gumballReducer.reduce(state, GumballAction.RefillGumballs) } }
  • 35. @POTUS404REDUCE! The Gumball Machine class GumballMachine(private val timeToDispense: Long = 50) { object Gumball { override fun toString() = "$color Gumball" private val color get() = when(Random().nextInt(8) + 1) { 1 -> "Blue" 2 -> "Red" 3 -> "Green" 4 -> "Yellow" 5 -> "Pink" 6 -> "Purple" 7 -> "Orange" else -> "White" } } val hasGumballs get() = state.remainingGumballs > 0 private val gumballReducer = GumballReducer() private var state = GumballState() fun buyGumball(): Gumball { state = gumballReducer.reduce(state, GumballAction.BuyGumball) Thread.sleep(timeToDispense) state = gumballReducer.reduce(state, GumballAction.Dispensed) return Gumball } fun refill() { state = gumballReducer.reduce(state, GumballAction.RefillGumballs) } }
  • 36. @POTUS404REDUCE! The Gumball Machine class GumballMachine(private val timeToDispense: Long = 50) { object Gumball { override fun toString() = "$color Gumball" private val color get() = when(Random().nextInt(8) + 1) { 1 -> "Blue" 2 -> "Red" 3 -> "Green" 4 -> "Yellow" 5 -> "Pink" 6 -> "Purple" 7 -> "Orange" else -> "White" } } val hasGumballs get() = state.remainingGumballs > 0 private val gumballReducer = GumballReducer() private var state = GumballState() fun buyGumball(): Gumball { state = gumballReducer.reduce(state, GumballAction.BuyGumball) Thread.sleep(timeToDispense) state = gumballReducer.reduce(state, GumballAction.Dispensed) return Gumball } fun refill() { state = gumballReducer.reduce(state, GumballAction.RefillGumballs) } }
  • 37. @POTUS404REDUCE! sealed class GumballAction : Action { object BuyGumball : GumballAction() object RefillGumballs : GumballAction() object Dispensed : GumballAction() } The Gumball Actions
  • 38. @POTUS404REDUCE! data class GumballState( val remainingGumballs: Int = 100, val isDispensing: Boolean = false ) : State The Gumball State
  • 39. @POTUS404REDUCE! class GumballReducer : Reducer<GumballState, GumballAction> { override fun reduce(state: GumballState, action: GumballAction): GumballState { return when(action) { is GumballAction.BuyGumball -> { if (state.isDispensing) { throw IllegalStateException(“…”) } else if (state.remainingGumballs <= 0) { throw IllegalStateException(“…”) } state.copy( remainingGumballs = state.remainingGumballs - 1, isDispensing = true ) } is GumballAction.RefillGumballs -> state.copy( remainingGumballs = 100, isDispensing = false ) is GumballAction.Dispensed -> state.copy(isDispensing = false) } } } The Gumball Reducer
  • 40. @POTUS404REDUCE! class GumballReducer : Reducer<GumballState, GumballAction> { override fun reduce(state: GumballState, action: GumballAction): GumballState{ return when(action) { is GumballAction.BuyGumball -> { if (state.isDispensing) { throw IllegalStateException(“…”) } else if (state.remainingGumballs <= 0) { throw IllegalStateException(“…”) } state.copy( remainingGumballs = state.remainingGumballs - 1, isDispensing = true ) } is GumballAction.RefillGumballs -> state.copy( remainingGumballs = 100, isDispensing = false ) is GumballAction.Dispensed -> state.copy(isDispensing = false) } } } The Gumball Reducer
  • 41. @POTUS404REDUCE! class GumballReducer : Reducer<GumballState, GumballAction> { override fun reduce(state: GumballState, action: GumballAction): GumballState { return when(action) { is GumballAction.BuyGumball -> { if (state.isDispensing) { throw IllegalStateException(“…”) } else if (state.remainingGumballs <= 0) { throw IllegalStateException(“…”) } state.copy( remainingGumballs = state.remainingGumballs - 1, isDispensing = true ) } is GumballAction.RefillGumballs -> state.copy( remainingGumballs = 100, isDispensing = false ) is GumballAction.Dispensed -> state.copy(isDispensing = false) } } } The Gumball Reducer
  • 42. @POTUS404REDUCE! class GumballReducer : Reducer<GumballState, GumballAction> { override fun reduce(state: GumballState, action: GumballAction): GumballState { return when(action) { is GumballAction.BuyGumball -> { if (state.isDispensing) { throw IllegalStateException(“…”) } else if (state.remainingGumballs <= 0) { throw IllegalStateException(“…”) } state.copy( remainingGumballs = state.remainingGumballs - 1, isDispensing = true ) } is GumballAction.RefillGumballs -> state.copy( remainingGumballs = 100, isDispensing = false ) is GumballAction.Dispensed -> state.copy(isDispensing = false) } } } The Gumball Reducer
  • 43. @POTUS404REDUCE! class GumballReducer : Reducer<GumballState, GumballAction> { override fun reduce(state: GumballState, action: GumballAction): GumballState { return when(action) { is GumballAction.BuyGumball -> { if (state.isDispensing) { throw IllegalStateException(“…”) } else if (state.remainingGumballs <= 0) { throw IllegalStateException(“…”) } state.copy( remainingGumballs = state.remainingGumballs - 1, isDispensing = true ) } is GumballAction.RefillGumballs -> state.copy( remainingGumballs = 100, isDispensing = false ) is GumballAction.Dispensed -> state.copy(isDispensing = false) } } } The Gumball Reducer
  • 44. @POTUS404REDUCE! class GumballReducer : Reducer<GumballState, GumballAction> { override fun reduce(state: GumballState, action: GumballAction): GumballState { return when(action) { is GumballAction.BuyGumball -> { if (state.isDispensing) { throw IllegalStateException(“…”) } else if (state.remainingGumballs <= 0) { throw IllegalStateException(“…”) } state.copy( remainingGumballs = state.remainingGumballs - 1, isDispensing = true ) } is GumballAction.RefillGumballs -> state.copy( remainingGumballs = 100, isDispensing = false ) is GumballAction.Dispensed -> state.copy(isDispensing = false) } } } The Gumball Reducer
  • 46. @POTUS404RxJava Observable.fromPublisher<CompanyAction> { subscriber -> 0.until(320).map { if (it % 11 == 0) CompanyAction.Add(Requisition.requisition) else CompanyAction.Hire(Requisition.requisition, Employee.employee) }.forEach { subscriber.onNext(it) }.also { subscriber.onComplete() } } .reduceWith({ Company("ActiveCampaign") }, { prevState, action -> when(action) { is CompanyAction.Hire -> { val employees = prevState.employees.toMutableList() employees.add(action.employee) prevState.copy(employees = employees) } is CompanyAction.Add -> { val requisitions = prevState.requisitions.toMutableList() requisitions.add(action.requisition) prevState.copy(requisitions = requisitions) } } }) .subscribe { state -> println("These numbers are most likely inaccurate.") println("ActiveCampaign has ${state.requisitions.size} job openings with around ${state.employees.size} employees.") println("There are ${state.employees.filter { it.name == "Cody" }.size} employees named Cody.") println("Here is the raw state output:") println(state) }
  • 47. @POTUS404RxJava Observable.fromPublisher<CompanyAction> { subscriber -> 0.until(320).map { if (it % 11 == 0) CompanyAction.Add(Requisition.requisition) else CompanyAction.Hire(Requisition.requisition, Employee.employee) } .forEach { subscriber.onNext(it) } .also { subscriber.onComplete() } } .reduceWith({ Company("ActiveCampaign") }, { prevState, action -> when(action) { is CompanyAction.Hire -> { val employees = prevState.employees.toMutableList() employees.add(action.employee) prevState.copy(employees = employees) } is CompanyAction.Add -> { val requisitions = prevState.requisitions.toMutableList() requisitions.add(action.requisition) prevState.copy(requisitions = requisitions) } } }) .subscribe { state -> println("These numbers are most likely inaccurate.") println("ActiveCampaign has ${state.requisitions.size} job openings with around ${state.employees.size} employees.") println("There are ${state.employees.filter { it.name == "Cody" }.size} employees named Cody.") println("Here is the raw state output:") println(state) }
  • 48. @POTUS404RxJava Observable.fromPublisher<CompanyAction> { subscriber -> 0.until(320).map { if (it % 11 == 0) CompanyAction.Add(Requisition.requisition) else CompanyAction.Hire(Requisition.requisition, Employee.employee) }.forEach { subscriber.onNext(it) }.also { subscriber.onComplete() } } .reduceWith({ Company("ActiveCampaign") }, { prevState, action -> when(action) { is CompanyAction.Hire -> { val employees = prevState.employees.toMutableList() employees.add(action.employee) prevState.copy(employees = employees) } is CompanyAction.Add -> { val requisitions = prevState.requisitions.toMutableList() requisitions.add(action.requisition) prevState.copy(requisitions = requisitions) } } }) .subscribe { state -> println("These numbers are most likely inaccurate.") println("ActiveCampaign has ${state.requisitions.size} job openings with around ${state.employees.size} employees.") println("There are ${state.employees.filter { it.name == "Cody" }.size} employees named Cody.") println("Here is the raw state output:") println(state) }
  • 49. @POTUS404RxJava Observable.fromPublisher<CompanyAction> { subscriber -> 0.until(320).map { if (it % 11 == 0) CompanyAction.Add(Requisition.requisition) else CompanyAction.Hire(Requisition.requisition, Employee.employee) }.forEach { subscriber.onNext(it) }.also { subscriber.onComplete() } } .reduceWith({ Company("ActiveCampaign") }, { prevState, action -> when(action) { is CompanyAction.Hire -> { val employees = prevState.employees.toMutableList() employees.add(action.employee) prevState.copy(employees = employees) } is CompanyAction.Add -> { val requisitions = prevState.requisitions.toMutableList() requisitions.add(action.requisition) prevState.copy(requisitions = requisitions) } } }) .subscribe { state -> println("These numbers are most likely inaccurate.") println("ActiveCampaign has ${state.requisitions.size} job openings with around ${state.employees.size} employees.") println("There are ${state.employees.filter { it.name == "Cody" }.size} employees named Cody.") println("Here is the raw state output:") println(state) }
  • 50. These numbers are most likely inaccurate. ActiveCampaign has 30 job openings with around 290 employees. There are 19 employees named Cody. Here is the raw state output: Company(name=ActiveCampaign, requisitions=[Requisition(name=Full Stack Engineer, department=Engineering), …) @POTUS404RxJava