Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android specific code in clean architecture

I've read multiple clean architecture tutorials for android, but all of them had very basic structure with simple network calls which leaves me wondering.

  1. What I want to know is where should I put code that handles accelerometer, geofencing, location, sms and similar? My initial thought was to put them in separate repositories. But as these are separate long running processes that could be started or stopped, it just doesn't feel right. I've tried renaming these classes from repository to service, just so the intent would be more clearly expressed. But still seems a bit off. What would be a good alternative?

  2. To be able to receive sent sms status, I need to register a receiver. Who should be responsible for that? Would the repository itself, upon receiving a call to sendSms(), should register that receiver, send message and release receiver, or would a UseCase first would have to check if SmsRepository is started, and if not would start it, thus registering receiver, and then call the sendSms() method? Or maybe there should be another approach taken to register receiver on app start and unregister on close?

  3. Is this an appropriate package structure?

enter image description here

EDIT: 4. What to do in case you want to constantly listen for server result? Do you directly subscribe to repository from viewmodel or do you keep calling same usecase (whose purpose is to fetch result once) after it finishes? My understanding of UseCase is that it only returns single result and is not supposed to be subscribable.

like image 806
SMGhost Avatar asked Nov 06 '22 10:11

SMGhost


1 Answers

My humble opinions:

1) Since accelerometer, gyroscope.. are kind of Data Sources, you can put them in SensorRepository/SensorDataSource/ (Data Layer which sits outside of Domain/Core layer and can have Android code). When you need to access them, you can go through UseCase class, where you would have this logics: sampling frequency, data filter, format changing, maybe buffer count etc...

To put accelerometer, gyroscope, location into same or different classes, should be decided feature based, not by sensor type based. If one feature (e.g. profile) needs both location and sms, they should be under same class.

Starting/stopping them can happen by calling StopLocationUseCase, StartLocationUseCase which will call start()/stop() of those Repository etc... StartLocationUseCase should be idempotent, if there is already started (UseCase will ask Repository), it won't start again.

2) Knowing 'sms sent/not sent' status feels like it is part of sendSms() operation. So all register/send/unregister should be called in sendSms() synchronously if possible.

3) Feature based is preferred (even project modules should be feature-based)

4) UseCase can return Observable kind of data type but IMHO it should not contain state.

like image 109
Jemshit Iskenderov Avatar answered Nov 15 '22 12:11

Jemshit Iskenderov