Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Migrate CreateSessionPresenter to ViewModel #1738

Merged
merged 1 commit into from
Jun 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.eventyay.organizer.core.organizer.update.UpdateOrganizerInfoViewModel;
import com.eventyay.organizer.core.role.list.RoleListViewModel;
import com.eventyay.organizer.core.role.invite.RoleInviteViewModel;
import com.eventyay.organizer.core.session.create.CreateSessionViewModel;
import com.eventyay.organizer.core.settings.autocheckin.AutoCheckInViewModel;
import com.eventyay.organizer.core.settings.restriction.TicketSettingsViewModel;
import com.eventyay.organizer.core.share.ShareEventViewModel;
Expand Down Expand Up @@ -246,6 +247,11 @@ public abstract class ViewModelModule {
@ViewModelKey(AttendeesViewModel.class)
public abstract ViewModel bindAttendeesViewModel(AttendeesViewModel attendeesViewModel);

@Binds
@IntoMap
@ViewModelKey(CreateSessionViewModel.class)
public abstract ViewModel bindCreateSessionViewModel(CreateSessionViewModel createSessionViewModel);

@Binds
public abstract ViewModelProvider.Factory bindViewModelFactory(OrgaViewModelFactory factory);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelProviders;

import com.google.android.material.textfield.TextInputLayout;
import android.text.Editable;
import android.text.TextUtils;
Expand All @@ -25,21 +28,22 @@
import javax.inject.Inject;

import br.com.ilhasoft.support.validation.Validator;
import dagger.Lazy;

import static com.eventyay.organizer.ui.ViewUtils.showView;

public class CreateSessionFragment extends BaseFragment<CreateSessionPresenter> implements CreateSessionView {
public class CreateSessionFragment extends BaseFragment implements CreateSessionView {

@Inject
Lazy<CreateSessionPresenter> presenterProvider;
ViewModelProvider.Factory viewModelFactory;

private SessionCreateLayoutBinding binding;
private Validator validator;
public static final String TRACK_KEY = "track";
private static final String SESSION_KEY = "session_id";
private ArrayAdapter<CharSequence> sessionStateAdapter;

private CreateSessionViewModel createSessionViewModel;

private boolean isSessionUpdating;
private long trackId;
private long eventId;
Expand Down Expand Up @@ -84,14 +88,15 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
binding = DataBindingUtil.inflate(inflater, R.layout.session_create_layout, container, false);
createSessionViewModel = ViewModelProviders.of(this, viewModelFactory).get(CreateSessionViewModel.class);
validator = new Validator(binding.form);

binding.submit.setOnClickListener(view -> {
if (validator.validate()) {
if (isSessionUpdating) {
getPresenter().updateSession(trackId, eventId);
createSessionViewModel.updateSession(trackId, eventId);
} else {
getPresenter().createSession(trackId, eventId);
createSessionViewModel.createSession(trackId, eventId);
}
}
});
Expand All @@ -110,11 +115,15 @@ private void setUpSpinner() {
@Override
public void onStart() {
super.onStart();
getPresenter().attach(this);
binding.setSession(getPresenter().getSession());
createSessionViewModel.getProgress().observe(this, this::showProgress);
createSessionViewModel.getDismiss().observe(this, (dismiss) -> dismiss());
createSessionViewModel.getSuccess().observe(this, this::onSuccess);
createSessionViewModel.getError().observe(this, this::showError);
createSessionViewModel.getSessionLiveData().observe(this, this::setSession);
binding.setSession(createSessionViewModel.getSession());

if (isSessionUpdating) {
getPresenter().loadSession(sessionId);
createSessionViewModel.loadSession(sessionId);
}

validate(binding.form.slidesUrlLayout, ValidateUtils::validateUrl, getResources().getString(R.string.url_validation_error));
Expand Down Expand Up @@ -157,7 +166,7 @@ public void afterTextChanged(Editable editable) {
@Override
public void setSession(Session session) {
binding.setSession(session);
String state = getPresenter().getSession().getState();
String state = createSessionViewModel.getSession().getState();
int statePosition = sessionStateAdapter.getPosition(state);
binding.form.spinner.setSelection(statePosition);
}
Expand All @@ -171,11 +180,6 @@ protected int getTitle() {
}
}

@Override
public Lazy<CreateSessionPresenter> getPresenterProvider() {
return presenterProvider;
}

@Override
public void showProgress(boolean show) {
showView(binding.progressBar, show);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package com.eventyay.organizer.core.session.create;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.ViewModel;

import com.eventyay.organizer.common.livedata.SingleEventLiveData;
import com.eventyay.organizer.data.event.Event;
import com.eventyay.organizer.data.session.Session;
import com.eventyay.organizer.data.session.SessionRepository;
import com.eventyay.organizer.data.tracks.Track;
import com.eventyay.organizer.utils.DateUtils;
import com.eventyay.organizer.utils.ErrorUtils;
import com.eventyay.organizer.utils.StringUtils;

import org.threeten.bp.LocalDateTime;
import org.threeten.bp.ZonedDateTime;
import org.threeten.bp.format.DateTimeParseException;

import javax.inject.Inject;

import io.reactivex.disposables.CompositeDisposable;

public class CreateSessionViewModel extends ViewModel {

private final SessionRepository sessionRepository;
private Session session = new Session();

private final CompositeDisposable compositeDisposable = new CompositeDisposable();
private final SingleEventLiveData<Boolean> progress = new SingleEventLiveData<>();
private final SingleEventLiveData<String> error = new SingleEventLiveData<>();
private final SingleEventLiveData<String> success = new SingleEventLiveData<>();
private final SingleEventLiveData<Void> dismiss = new SingleEventLiveData<>();
private final SingleEventLiveData<Session> sessionLiveData = new SingleEventLiveData<>();

@Inject
public CreateSessionViewModel(SessionRepository sessionRepository) {
this.sessionRepository = sessionRepository;

LocalDateTime current = LocalDateTime.now();

String isoDate = DateUtils.formatDateToIso(current);
session.setStartsAt(isoDate);
session.setEndsAt(isoDate);
}

public LiveData<Boolean> getProgress() {
return progress;
}

public LiveData<String> getSuccess() {
return success;
}

public LiveData<Void> getDismiss() {
return dismiss;
}

public LiveData<String> getError() {
return error;
}

public LiveData<Session> getSessionLiveData() {
return sessionLiveData;
}

public Session getSession() {
return session;
}

private boolean verify() {
try {
ZonedDateTime start = DateUtils.getDate(session.getStartsAt());
ZonedDateTime end = DateUtils.getDate(session.getEndsAt());

if (!end.isAfter(start)) {
error.setValue("End time should be after start time");
return false;
}
return true;
} catch (DateTimeParseException pe) {
error.setValue("Please enter date in correct format");
return false;
}
}

protected void nullifyEmptyFields(Session session) {
session.setSlidesUrl(StringUtils.emptyToNull(session.getSlidesUrl()));
session.setAudioUrl(StringUtils.emptyToNull(session.getAudioUrl()));
session.setVideoUrl(StringUtils.emptyToNull(session.getVideoUrl()));
session.setSignupUrl(StringUtils.emptyToNull(session.getSignupUrl()));
}

// Used for loading the session information on start
public void loadSession(long sessionId) {

compositeDisposable.add(
sessionRepository
.getSession(sessionId, false)
.doOnSubscribe(disposable -> progress.setValue(true))
.doFinally(() -> progress.setValue(false))
.doFinally(this::showSession)
.subscribe(loadedSession -> this.session = loadedSession,
throwable -> error.setValue(ErrorUtils.getMessage(throwable).toString())));
}

private void showSession() {
sessionLiveData.setValue(session);
}

// Method called for updating an session
public void updateSession(long trackId, long eventId) {
Track track = new Track();
Event event = new Event();

track.setId(trackId);
event.setId(eventId);
session.setTrack(track);
session.setEvent(event);
nullifyEmptyFields(session);

compositeDisposable.add(
sessionRepository
.updateSession(session)
.doOnSubscribe(disposable -> progress.setValue(true))
.doFinally(() -> progress.setValue(false))
.subscribe(updatedSession -> {
success.setValue("Session Updated Successfully");
dismiss.call();
}, throwable -> error.setValue(ErrorUtils.getMessage(throwable).toString())));
}

public void createSession(long trackId, long eventId) {
if (!verify())
return;

Track track = new Track();
Event event = new Event();

track.setId(trackId);
event.setId(eventId);
session.setTrack(track);
session.setEvent(event);

nullifyEmptyFields(session);

compositeDisposable.add(
sessionRepository
.createSession(session)
.doOnSubscribe(disposable -> progress.setValue(true))
.doFinally(() -> progress.setValue(false))
.subscribe(createdSession -> {
success.setValue("Session Created");
dismiss.call();
}, throwable -> error.setValue(ErrorUtils.getMessage(throwable).toString())));
}
}
Loading