Skip to content

Commit

Permalink
issue #54: The app requires the user name when he enter in session
Browse files Browse the repository at this point in the history
fix #54

When the user open the Session Activity (after create a
new session or when it click on a shared link) the app will check if
there are an user on shared preferences. If there is not any, the app
will require a name
  • Loading branch information
ericbbraga authored and rodrigoperazzo committed Sep 3, 2018
1 parent 55c6da2 commit 03c6ec6
Show file tree
Hide file tree
Showing 21 changed files with 353 additions and 58 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package br.org.cesar.discordtime.stickysessions.data.local.repository;

import android.content.Context;
import android.content.SharedPreferences;

import br.org.cesar.discordtime.stickysessions.domain.repository.UserRepository;
import io.reactivex.Single;
import io.reactivex.SingleEmitter;
import io.reactivex.SingleOnSubscribe;

public class UserLocalRepository implements UserRepository {

private static final String USER_KEY = "user";

private final Context mContext;
private final String mSharedPrefs;

public UserLocalRepository(Context context, String sharedPrefsName) {
mContext = context;
mSharedPrefs = sharedPrefsName;
}

@Override
public Single<Boolean> saveUser(String user) {
return Single.create(emitter -> {
SharedPreferences prefs =
mContext.getSharedPreferences(mSharedPrefs, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(USER_KEY, user);

if (editor.commit()) {
emitter.onSuccess(true);
} else {
emitter.onError(
new Exception("The user could not be saved into local repository")
);
}
});
}

@Override
public Single<String> getSavedUser() {
return Single.create(emitter -> {
SharedPreferences prefs =
mContext.getSharedPreferences(mSharedPrefs, Context.MODE_PRIVATE);
String userSaved = prefs.getString(USER_KEY, null);
if (userSaved != null) {
emitter.onSuccess(userSaved);
} else {
emitter.onError(new Exception("User Not found"));
}


});

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import br.org.cesar.discordtime.stickysessions.data.remote.service.NoteService;
import br.org.cesar.discordtime.stickysessions.data.repository.mapper.Mapper;
import br.org.cesar.discordtime.stickysessions.domain.model.Note;
import br.org.cesar.discordtime.stickysessions.domain.model.NoteFilter;
import br.org.cesar.discordtime.stickysessions.domain.repository.NoteRepository;
import io.reactivex.Single;

Expand All @@ -30,8 +31,8 @@ public Single<Note> addNote(Note note) {
}

@Override
public Single<List<Note>> listNotesForSession(String id) {
return mNoteService.listNotesForSession(id).map(
public Single<List<Note>> listNotesForSession(NoteFilter noteFilter) {
return mNoteService.listNotesForSession(noteFilter.idSession, noteFilter.user).map(
noteRemotes -> {
List<Note> mNotes = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public interface NoteService {
@POST("/notes")
Single<NoteRemote> addNote(@Body NoteRemote note);

@GET("/notes/{id}")
Single<List<NoteRemote>> listNotesForSession(@Path("id") String id);
@GET("/notes/{id}/{user}")
Single<List<NoteRemote>> listNotesForSession(@Path("id") String id, @Path("user") String user);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package br.org.cesar.discordtime.stickysessions.domain.interactor;

import br.org.cesar.discordtime.stickysessions.domain.repository.UserRepository;
import io.reactivex.Single;
import io.reactivex.SingleObserver;

public class GetLocalUser extends UseCase<Void,String> {

private final UserRepository mUserRepository;

public GetLocalUser(UserRepository repository) {
mUserRepository = repository;
}

@Override
public Single<String> execute(Void params) {
return mUserRepository.getSavedUser();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import java.util.List;

import br.org.cesar.discordtime.stickysessions.domain.model.Note;
import br.org.cesar.discordtime.stickysessions.domain.model.NoteFilter;
import br.org.cesar.discordtime.stickysessions.domain.repository.NoteRepository;
import br.org.cesar.discordtime.stickysessions.domain.repository.SessionRepository;
import io.reactivex.Single;

public class ListNotesForSession extends UseCase<String, List<Note>> {
public class ListNotesForSession extends UseCase<NoteFilter, List<Note>> {

private final NoteRepository mNoteRepository;
private final SessionRepository mSessionRepository;
Expand All @@ -18,14 +19,14 @@ public ListNotesForSession(NoteRepository noteRepository, SessionRepository sess
}

@Override
public Single<List<Note>> execute(String sessionId) {
return mSessionRepository.getSession(sessionId).flatMap(
public Single<List<Note>> execute(NoteFilter noteFilter) {
return mSessionRepository.getSession(noteFilter.idSession).flatMap(
session -> {
if (session == null) {
return Single.error(new Exception("The Session does not exist"));
}

return mNoteRepository.listNotesForSession(sessionId);
return mNoteRepository.listNotesForSession(noteFilter);
}
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package br.org.cesar.discordtime.stickysessions.domain.interactor;

import br.org.cesar.discordtime.stickysessions.domain.repository.UserRepository;
import io.reactivex.Single;

public class SaveCurrentUser extends UseCase<String, Boolean> {

private UserRepository mUserRepository;

public SaveCurrentUser(UserRepository userRepository) {
mUserRepository = userRepository;
}

@Override
public Single<Boolean> execute(String userName) {

if (userName == null || userName.trim().isEmpty()) {
return Single.error(new Exception("The user is invalid (null or empty)"));
}

return mUserRepository.saveUser(userName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package br.org.cesar.discordtime.stickysessions.domain.model;

public class NoteFilter {

public String user;
public String idSession;

public NoteFilter(String idSession, String user) {
this.user = user;
this.idSession = idSession;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import java.util.List;

import br.org.cesar.discordtime.stickysessions.domain.model.Note;
import br.org.cesar.discordtime.stickysessions.domain.model.NoteFilter;
import io.reactivex.Single;

public interface NoteRepository {
Single<Note> addNote(Note note);
Single<List<Note>> listNotesForSession(String id);
Single<List<Note>> listNotesForSession(NoteFilter noteFilter);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package br.org.cesar.discordtime.stickysessions.domain.repository;

import io.reactivex.Single;

public interface UserRepository {
Single<Boolean> saveUser(String user);
Single<String> getSavedUser();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import br.org.cesar.discordtime.stickysessions.injectors.modules.SessionModule;
import br.org.cesar.discordtime.stickysessions.injectors.modules.SessionPresenterModule;
import br.org.cesar.discordtime.stickysessions.injectors.modules.ThreadModule;
import br.org.cesar.discordtime.stickysessions.injectors.modules.UserModule;
import br.org.cesar.discordtime.stickysessions.ui.session.SessionActivity;
import dagger.Component;

Expand All @@ -16,7 +17,8 @@
SessionModule.class,
ThreadModule.class,
ContextModule.class,
ServerModule.class
ServerModule.class,
UserModule.class
}
)
public interface SessionComponent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import br.org.cesar.discordtime.stickysessions.domain.interactor.AddNote;
import br.org.cesar.discordtime.stickysessions.domain.interactor.ListNotesForSession;
import br.org.cesar.discordtime.stickysessions.domain.model.Note;
import br.org.cesar.discordtime.stickysessions.domain.model.NoteFilter;
import br.org.cesar.discordtime.stickysessions.domain.repository.NoteRepository;
import br.org.cesar.discordtime.stickysessions.domain.repository.SessionRepository;
import br.org.cesar.discordtime.stickysessions.executor.ObservableUseCase;
Expand All @@ -35,7 +36,7 @@ public ObservableUseCase<Note, Note> provideObservableAddNoteUseCase(
}

@Provides
public ObservableUseCase<String, List<Note>> provideObservableListNotesUseCase(
public ObservableUseCase<NoteFilter, List<Note>> provideObservableListNotesUseCase(
ListNotesForSession listNotesForSession, ThreadExecutor threadExecutor,
PostExecutionThread postExecutionThread
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.List;

import br.org.cesar.discordtime.stickysessions.domain.model.Note;
import br.org.cesar.discordtime.stickysessions.domain.model.NoteFilter;
import br.org.cesar.discordtime.stickysessions.domain.model.Session;
import br.org.cesar.discordtime.stickysessions.executor.ObservableUseCase;
import br.org.cesar.discordtime.stickysessions.presentation.session.SessionContract;
Expand All @@ -17,9 +18,11 @@ public class SessionPresenterModule {
public SessionContract.Presenter providesPresenter(
ObservableUseCase<String, Session> enterSession,
ObservableUseCase<Note, Note> addNote,
ObservableUseCase<String, List<Note>> listNotes){
ObservableUseCase<NoteFilter, List<Note>> listNotes,
ObservableUseCase<String, Boolean> saveCurrentUser,
ObservableUseCase<Void, String> getSavedUser){

return new SessionPresenter(enterSession, addNote, listNotes);
return new SessionPresenter(enterSession, addNote, listNotes, saveCurrentUser, getSavedUser);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package br.org.cesar.discordtime.stickysessions.injectors.modules;

import android.content.Context;

import javax.inject.Named;

import br.org.cesar.discordtime.stickysessions.data.local.repository.UserLocalRepository;
import br.org.cesar.discordtime.stickysessions.domain.interactor.GetLocalUser;
import br.org.cesar.discordtime.stickysessions.domain.interactor.SaveCurrentUser;
import br.org.cesar.discordtime.stickysessions.domain.repository.UserRepository;
import br.org.cesar.discordtime.stickysessions.executor.ObservableUseCase;
import br.org.cesar.discordtime.stickysessions.executor.PostExecutionThread;
import br.org.cesar.discordtime.stickysessions.executor.ThreadExecutor;
import dagger.Module;
import dagger.Provides;

@Module
public class UserModule {

@Provides
public ObservableUseCase<String, Boolean> providesObservableSaveCurrentUser(
SaveCurrentUser saveCurrentUser,
ThreadExecutor threadExecutor,
PostExecutionThread postExecutionThread) {
return new ObservableUseCase<>(saveCurrentUser, threadExecutor, postExecutionThread);
}

@Provides
public ObservableUseCase<Void, String> providesObservableGetSavedUser(
GetLocalUser getLocalUser, ThreadExecutor threadExecutor,
PostExecutionThread postExecutionThread) {

return new ObservableUseCase<>(getLocalUser, threadExecutor, postExecutionThread);
}

@Provides
public SaveCurrentUser providesSaveCurrentUser(UserRepository repository) {
return new SaveCurrentUser(repository);
}

@Provides
public GetLocalUser providesGetLocalUser(UserRepository repository) {
return new GetLocalUser(repository);
}

@Provides
public UserRepository providesUserRepository(Context context, @Named("prefs") String prefsName) {
return new UserLocalRepository(context, prefsName);
}

@Provides
@Named("prefs")
public String providesPreferenceName() {
return "user_preference";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ public interface SessionContract {
interface Presenter {
void attachView(SessionContract.View view);
void detachView();
void onEnterSession(String sessionId);
void onShareSession();
void onAddNoteClicked();
void addNewNote(String sessionId, String description);
void onNoteWidgetClicked(Note note);
void onResume();
void currentUser(String userName);
void currentSession(String sessionId);
}

interface View {
Expand All @@ -28,5 +30,6 @@ interface View {
void displayErrorInvalidNotes();
void displayNoteContent(Note note);
void addNoteToNoteList(Note note);
void showWidgetAddName();
}
}
Loading

0 comments on commit 03c6ec6

Please sign in to comment.