Hello, I am writing an application for android. The essence of the application: 1) the user’s window displays certain information, 2) This information is updated at regular intervals by a timer 3) During the update, a request is sent to another device, from which the answer comes later. 4) The answer is processed and the information on the screen of the android device is updated.
I would like to implement this application in accordance with the MVP pattern, i.e. View <-> Presenter <-> Model. I am still new to programming, so I would like to clarify my algorithm:
There is a main loop in which updates occur:
class ParameterRepresenter { private ParameterInteractor mParInteract; private ParameterView mParView; public ParameterRepresenter(ParameterView parView){ mParView = parView; // Первая итерация через 1 с. Timer mNewTimer = new Timer(); MainTimerTask mMainTimerTask = new MainTimerTask(); mNewTimer.schedule( mMainTimerTask, 1000 ); } class MainTimerTask extends TimerPresenterTask{ @Override public void run() { runOnUiThread(new Runnable() { @Override public void run() { //Здесь будет реализована группа Presenter updateRadioButtonStar() // далее обновление остальных параметров updateTextView1(); updateTextView2(); ...... // Следующая итерация через 1 с Timer mNewTimer = new Timer(); MainTimerTask mMainTimerTask = new MainTimerTask(); mNewTimer.schedule( mMainTimerTask, 1000 ); } }); // Разберу подробно только для одного объекта View // Например RadioButtonStar остальные элементы по // аналогии public void updateRadioButtonStar(){ if ( mParInteract.isRadioButtonStarChecked() ){ mParView.checkRadioButtonStar(); } } } } } Further implementation of the View group:
class ParameterView extends AppCompatActivity(){ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ... // Здесь какой-то код инициализации // Создаем объект презентера, начинаем обновление // параметров mParPresenter = new ParameterPresenter(this); } public void checkRadioButtonStar(){ mRadioButtonStar.setEnabled(true); } public void uncheckRadioButtonStar(){ mRadioButtonStar.setEnabled(false); } ... // Остальные элементы } Next, the model group:
class ParameterInteractor () {
public boolean isRadioButtonStarChecked(){ ... // Здесь происходит запрос, получение ответа // от другого устройства в виде обновленного // параметра и выносится решение // включать radioButtonStar или нет return ... } }
Is it possible in this case to put the inner class MainTimerTask (that is, the actual data update cycle itself) in the presentation from the MVP point of view? Or is it ( MainTimerTask ) better placed in the interactor?
Thanks in advance to everyone for the answers.