Skip to content

Commit

Permalink
Merge pull request #299 from novoda/tpbot/save_last_server
Browse files Browse the repository at this point in the history
Tpbot/save last server
  • Loading branch information
Ryan Feline authored May 30, 2017
2 parents 4f3d5ad + 7d46748 commit 020b711
Show file tree
Hide file tree
Showing 13 changed files with 143 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
package com.novoda.tpbot.bot;

import com.novoda.tpbot.Direction;
import com.novoda.tpbot.Result;
import com.novoda.support.Observable;
import com.novoda.support.Observer;
import com.novoda.tpbot.Direction;
import com.novoda.tpbot.Result;
import com.novoda.tpbot.controls.LastServerPersistence;

import static com.novoda.support.Observable.unsubscribe;

class BotPresenter {

private final BotTelepresenceService tpService;
private final BotView botView;
private final LastServerPersistence lastServerPersistence;
private final String serverAddress;

private Observable<Result> connectionObservable;
private Observable<Direction> directionObservable;

BotPresenter(BotTelepresenceService tpService, BotView botView, String serverAddress) {
BotPresenter(BotTelepresenceService tpService, BotView botView, LastServerPersistence lastServerPersistence, String serverAddress) {
this.tpService = tpService;
this.botView = botView;
this.lastServerPersistence = lastServerPersistence;
this.serverAddress = serverAddress;
}

Expand All @@ -42,7 +45,8 @@ public void update(Result result) {
if (result.isError()) {
botView.onError(result.exception().get().getMessage());
} else {
botView.onConnect(result.message().get());
lastServerPersistence.saveLastConnectedServer(serverAddress);
botView.onConnect(result.message().get(), serverAddress);

directionObservable = tpService.listen()
.attach(new DirectionObserver())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

interface BotView {

void onConnect(String room);
void onConnect(String room, String serverAddress);

void onDisconnect();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.novoda.tpbot.controls;

public interface LastServerPersistence {

void saveLastConnectedServer(String serverAddress);

boolean containsLastConnectedServer();

String getLastConnectedServer();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,27 @@
import com.novoda.tpbot.Result;
import com.novoda.support.Observable;
import com.novoda.support.Observer;
import com.novoda.tpbot.controls.LastServerPersistence;

import static com.novoda.support.Observable.unsubscribe;

class HumanPresenter {

private final HumanTelepresenceService humanTelepresenceService;
private final HumanView humanView;
private final LastServerPersistence lastServerPersistence;

private Observable<Result> observable;
private String serverAddress;

HumanPresenter(HumanTelepresenceService humanTelepresenceService, HumanView humanView) {
HumanPresenter(HumanTelepresenceService humanTelepresenceService, HumanView humanView, LastServerPersistence lastServerPersistence) {
this.humanTelepresenceService = humanTelepresenceService;
this.humanView = humanView;
this.lastServerPersistence = lastServerPersistence;
}

void startPresenting(String serverAddress) {
this.serverAddress = serverAddress;
observable = humanTelepresenceService.connectTo(serverAddress)
.attach(new ConnectionObserver())
.start();
Expand All @@ -42,6 +47,7 @@ public void update(Result result) {
if (result.isError()) {
humanView.onError(result.exception().get().getMessage());
} else {
lastServerPersistence.saveLastConnectedServer(serverAddress);
humanView.onConnect(result.message().get());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.novoda.support.Observable;
import com.novoda.tpbot.Direction;
import com.novoda.tpbot.Result;
import com.novoda.tpbot.controls.LastServerPersistence;

import org.junit.Before;
import org.junit.Rule;
Expand All @@ -27,14 +28,17 @@ public class BotPresenterTest {
@Mock
BotTelepresenceService tpService;

@Mock
private LastServerPersistence lastServerPersistence;

@Mock
BotView botView;

private BotPresenter presenter;

@Before
public void setUp() throws Exception {
presenter = new BotPresenter(tpService, botView, SERVER_ADDRESS);
presenter = new BotPresenter(tpService, botView, lastServerPersistence, SERVER_ADDRESS);

when(tpService.listen()).thenReturn(Observable.just(DIRECTION));
}
Expand All @@ -45,7 +49,7 @@ public void givenSuccessfulConnection_whenStartPresenting_thenBotViewOnConnectIs

presenter.startPresenting();

verify(botView).onConnect(SUCCESS_RESULT.message().get());
verify(botView).onConnect(SUCCESS_RESULT.message().get(), SERVER_ADDRESS);
}

@Test
Expand Down Expand Up @@ -97,6 +101,24 @@ public void givenUnsuccessfulConnection_whenStartPresenting_thenDoesNotStartList
verify(tpService, never()).listen();
}

@Test
public void givenSuccessfulConnection_whenStartPresenting_thenServerAddressIsStored() {
when(tpService.connectTo(SERVER_ADDRESS)).thenReturn(Observable.just(SUCCESS_RESULT));

presenter.startPresenting();

verify(lastServerPersistence).saveLastConnectedServer(SERVER_ADDRESS);
}

@Test
public void givenUnsuccessfulConnection_whenStartPresenting_thenServerAddressIsNotStored() {
when(tpService.connectTo(SERVER_ADDRESS)).thenReturn(Observable.just(FAILURE_RESULT));

presenter.startPresenting();

verify(lastServerPersistence, never()).saveLastConnectedServer(anyString());
}

@Test
public void givenStartedListeningForDirections_whenDirectionIsEmitted_thenBotViewMoveInDirectionIsCalled() {
givenStartedListeningForDirections();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.novoda.tpbot.Result;
import com.novoda.support.Observable;
import com.novoda.tpbot.controls.LastServerPersistence;

import org.junit.Before;
import org.junit.Rule;
Expand All @@ -12,6 +13,7 @@
import org.mockito.junit.MockitoRule;

import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

Expand All @@ -30,11 +32,14 @@ public class HumanPresenterTest {
@Mock
HumanView humanView;

@Mock
private LastServerPersistence lastServerPersistence;

private HumanPresenter presenter;

@Before
public void setUp() throws Exception {
presenter = new HumanPresenter(tpService, humanView);
presenter = new HumanPresenter(tpService, humanView, lastServerPersistence);
}

@Test
Expand Down Expand Up @@ -77,4 +82,22 @@ public void givenAlreadyPresenting_whenStopPresentingIsCalled_thenConnectionObse
verify(spyObservable).detachObservers();
}

@Test
public void givenSuccessfulConnection_whenStartPresenting_thenServerAddressIsStored() {
when(tpService.connectTo(anyString())).thenReturn(Observable.just(SUCCESS_RESULT));

presenter.startPresenting(SERVER_ADDRESS);

verify(lastServerPersistence).saveLastConnectedServer(SERVER_ADDRESS);
}

@Test
public void givenUnsuccessfulConnection_whenStartPresenting_thenServerAddressIsNotStored() {
when(tpService.connectTo(anyString())).thenReturn(Observable.just(FAILURE_RESULT));

presenter.startPresenting(SERVER_ADDRESS);

verify(lastServerPersistence, never()).saveLastConnectedServer(anyString());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import com.novoda.tpbot.controls.CommandRepeater;
import com.novoda.tpbot.controls.ControllerListener;
import com.novoda.tpbot.controls.ControllerView;
import com.novoda.tpbot.human.ServerDeclarationView;
import com.novoda.tpbot.controls.ServerDeclarationView;

import java.util.HashMap;

Expand Down Expand Up @@ -201,7 +201,7 @@ public void onServiceDisconnected(ComponentName componentName) {
};

@Override
public void onConnect(String room) {
public void onConnect(String room, String serverAddress) {
debugView.showPermanently(getString(R.string.connected));
switchableView.setDisplayedChild(1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.preference.PreferenceManager;

import com.novoda.tpbot.controls.LastServerPersistence;
import com.novoda.tpbot.controls.LastServerPreferences;

class BotServiceCreator {

Expand All @@ -27,8 +32,15 @@ void create() {

@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
LastServerPersistence lastServerPersistence = new LastServerPreferences(sharedPreferences);
BotPresenter botPresenter = new BotPresenter(
SocketIOTelepresenceService.getInstance(),
botView,
lastServerPersistence,
serverAddress
);
BotService.BotServiceBinder binder = (BotService.BotServiceBinder) iBinder;
BotPresenter botPresenter = new BotPresenter(SocketIOTelepresenceService.getInstance(), botView, serverAddress);
binder.setBotPresenter(botPresenter);
binder.onDependenciesBound();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.novoda.tpbot.controls;

import android.content.SharedPreferences;

public class LastServerPreferences implements LastServerPersistence {

private static final String KEY_LAST_SERVER = "last_server";

private final SharedPreferences preferences;

public LastServerPreferences(SharedPreferences preferences) {
this.preferences = preferences;
}

@Override
public void saveLastConnectedServer(String serverAddress) {
preferences.edit()
.putString(KEY_LAST_SERVER, serverAddress)
.apply();
}

@Override
public boolean containsLastConnectedServer() {
return preferences.contains(KEY_LAST_SERVER);
}

@Override
public String getLastConnectedServer() {
return preferences.getString(KEY_LAST_SERVER, "");
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.novoda.tpbot.human;
package com.novoda.tpbot.controls;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
Expand Down Expand Up @@ -33,6 +35,12 @@ public void onClick(View view) {
serverDeclarationListener.onConnect(serverAddress);
}
});

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext());
LastServerPreferences lastServerPreferences = new LastServerPreferences(sharedPreferences);
if (lastServerPreferences.containsLastConnectedServer()) {
serverAddressDeclaration.setText(lastServerPreferences.getLastConnectedServer());
}
}

public void setServerDeclarationListener(ServerDeclarationListener serverDeclarationListener) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package com.novoda.tpbot.human;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;

import com.novoda.notils.caster.Views;
import com.novoda.support.SelfDestructingMessageView;
import com.novoda.support.SwitchableView;
import com.novoda.tpbot.Direction;
import com.novoda.tpbot.R;
import com.novoda.tpbot.ServerDeclarationListener;
import com.novoda.tpbot.controls.CommandRepeater;
import com.novoda.tpbot.controls.ControllerListener;
import com.novoda.tpbot.controls.ControllerView;
import com.novoda.support.SelfDestructingMessageView;
import com.novoda.tpbot.ServerDeclarationListener;
import com.novoda.support.SwitchableView;
import com.novoda.tpbot.controls.LastServerPersistence;
import com.novoda.tpbot.controls.LastServerPreferences;
import com.novoda.tpbot.controls.ServerDeclarationView;

public class HumanActivity extends AppCompatActivity implements HumanView {

Expand All @@ -32,7 +37,9 @@ protected void onCreate(Bundle savedInstanceState) {
debugView = Views.findById(this, R.id.bot_controller_debug_view);
switchableView = Views.findById(this, R.id.bot_switchable_view);

presenter = new HumanPresenter(SocketIOTelepresenceService.getInstance(), this);
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
LastServerPersistence lastServerPersistence = new LastServerPreferences(sharedPreferences);
presenter = new HumanPresenter(SocketIOTelepresenceService.getInstance(), this, lastServerPersistence);

Handler handler = new Handler();
commandRepeater = new CommandRepeater(commandRepeatedListener, handler);
Expand All @@ -42,6 +49,8 @@ protected void onCreate(Bundle savedInstanceState) {

ServerDeclarationView serverDeclarationView = Views.findById(switchableView, R.id.bot_server_declaration_view);
serverDeclarationView.setServerDeclarationListener(serverDeclarationListener);


}

private final CommandRepeater.Listener commandRepeatedListener = new CommandRepeater.Listener() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<com.novoda.tpbot.human.ServerDeclarationView xmlns:android="http://schemas.android.com/apk/res/android"
<com.novoda.tpbot.controls.ServerDeclarationView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/bot_server_declaration_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand Down
Binary file modified TelepresenceBot/schematics/schematic.fzz
Binary file not shown.

0 comments on commit 020b711

Please sign in to comment.