Redux in Mobile (Swift & Kotlin)

Cart00nHero8
3 min readAug 26, 2020

Let’s direct your code

In my work, many companies have challenges for managing iOS and Android codes. So some companies will use like Flutter or ReacNative cross platform way. But native code still have the best supports for their platform. But I notice that Swift and Kotlin are similar. So I think is it possible use same architecture in both platform? When I met Redux and Actor Models, I think I found the answer.

Diagram:

Let you be director:

We usually called a flow as a story in Agile Development. Just think about you’re a director. Select what actors you want, design your scene and scenery.

Oh!! don’t forget they are independent. So when you call “Action”, everything start to work.

And the architecture will have bellow abiblities:

Easy to separate — Autonomy
Easy to understand — Understandability
Easy to extend — Extensibility
Easy to change — Changeability
Easy to replace — Replaceability
Easy to scale — Scalability
Easy to recover — Resilience
Easy to connect — Uniform interface
Easy to afford — Cost-efficiency

How to implement it:

Step1: Build Redux as usual

But there’s some secrets in Action~

Design Actions with OCP rule

struct AdjustForKeyboardAction: Action {
let notification: Notification
init(notification: Notification) {
self.notification = notification
}
}

And we’ll get input and output from action, and we can design an uniform state:

// MARK: — SubStates
struct DefaultTemplateState : StateType {
let currentAction : Action?
var receivedParcel: ParcelObject?
}

And let the state easy to design and won’t use huge memory to save your state.

There’s a one more thing: All actions can’t be reused.

In reducer, you just update the action:

// MARK: - SubReducers
func defaultTemplateReducer(
action: Action,
state:DefaultTemplateState?) -> DefaultTemplateState {
var newState = DefaultTemplateState(
currentAction: action,receivedParcel: state?.receivedParcel)
switch newState.currentAction {
case is SendParcelAction:
let action = newState.currentAction as? SendParcelAction
newState.receivedParcel = action?.parcel
case is SignParcelReceiptAction:
newState.receivedParcel = nil
default:break;
}
return newState
}

Step2: Build scene in stage

Stage actually is Controllers or Activities. Their resposibilities are switch Containers or Fragments(Scene), and send data to them when state changed.

For the purpose to prevent refreshing view everytime when Redux triggered, I check action in Container(Fragment).

And there will have presenter to keep data.

Template, just the view developers wanna reused

Step3: Choose Actors

It just libraries or microservice or some methods we used before. And there’s a special Actors I designed is the DeliveryMan. If we need pass values between view and view, I use DeliveryMan to do that. And It will let all scene independent.

I’ve tried this architecture in both Swift and Kotlin. If I finished project in one platform. I could build it quickly in another just like copy-paste.

Right now I start to learn Rust. And I have plan to try to use Rust to build actors. And let iOS and Android could use the same actors code. If I succeed to do it, next time I’ll share it.

If you have any beter idea, please share to me. Thanks for your reading

--

--