Skip to content

Commit f02fdae

Browse files
authored
Merge pull request #98 from NaikSoftware/forresthopkins-feature/deep-refactor
Forresthopkins/DrStranges deep refactor
2 parents fae04e6 + d67eaeb commit f02fdae

File tree

17 files changed

+469
-373
lines changed

17 files changed

+469
-373
lines changed

.gitignore

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
*.iml
22
.gradle
33
/local.properties
4-
/.idea/workspace.xml
5-
/.idea/libraries
4+
.idea
65
.DS_Store
76
/build
8-
/captures
7+
/captures

build.gradle

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
buildscript {
44
repositories {
55
jcenter()
6+
maven {
7+
url "https://maven.google.com"
8+
}
9+
google()
610
}
711
dependencies {
8-
classpath 'com.android.tools.build:gradle:2.3.0'
9-
classpath 'me.tatarka:gradle-retrolambda:3.4.0'
10-
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
12+
classpath 'com.android.tools.build:gradle:3.0.1'
13+
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
1114

1215
// NOTE: Do not place your application dependencies here; they belong
1316
// in the individual module build.gradle files
@@ -18,7 +21,7 @@ allprojects {
1821
repositories {
1922
jcenter()
2023
maven { url "https://jitpack.io" }
21-
maven { url "http://clojars.org/repo" }
24+
google()
2225
}
2326
}
2427

example-client/build.gradle

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
apply plugin: 'com.android.application'
2-
apply plugin: 'me.tatarka.retrolambda'
32

43
android {
54
compileSdkVersion 25
6-
buildToolsVersion "25.0.2"
5+
buildToolsVersion '26.0.2'
76

87
defaultConfig {
98
applicationId "ua.naiksoftware.stompclientexample"
@@ -29,7 +28,7 @@ dependencies {
2928
compile fileTree(dir: 'libs', include: ['*.jar'])
3029
testCompile 'junit:junit:4.12'
3130
compile 'com.android.support:appcompat-v7:25.3.1'
32-
compile 'org.java-websocket:java-websocket:1.3.2'
31+
compile 'org.java-websocket:Java-WebSocket:1.3.6'
3332
compile 'com.android.support:recyclerview-v7:25.3.1'
3433
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
3534
compile 'com.squareup.retrofit2:converter-gson:2.3.0'

example-client/src/main/java/ua/naiksoftware/stompclientexample/ExampleRepository.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package ua.naiksoftware.stompclientexample;
22

3+
import io.reactivex.Completable;
34
import io.reactivex.Flowable;
45
import retrofit2.http.POST;
56
import retrofit2.http.Query;
@@ -10,5 +11,5 @@
1011
public interface ExampleRepository {
1112

1213
@POST("hello-convert-and-send")
13-
Flowable<Void> sendRestEcho(@Query("msg") String message);
14+
Completable sendRestEcho(@Query("msg") String message);
1415
}

example-client/src/main/java/ua/naiksoftware/stompclientexample/MainActivity.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,13 @@
1111
import com.google.gson.Gson;
1212
import com.google.gson.GsonBuilder;
1313

14-
import org.java_websocket.WebSocket;
15-
1614
import java.text.SimpleDateFormat;
1715
import java.util.ArrayList;
1816
import java.util.Date;
1917
import java.util.List;
2018
import java.util.Locale;
2119

22-
import io.reactivex.FlowableTransformer;
20+
import io.reactivex.CompletableTransformer;
2321
import io.reactivex.android.schedulers.AndroidSchedulers;
2422
import io.reactivex.disposables.Disposable;
2523
import io.reactivex.schedulers.Schedulers;
@@ -56,7 +54,7 @@ public void disconnectStomp(View view) {
5654
}
5755

5856
public void connectStomp(View view) {
59-
mStompClient = Stomp.over(WebSocket.class, "ws://" + ANDROID_EMULATOR_LOCALHOST
57+
mStompClient = Stomp.over(Stomp.ConnectionProvider.JWS, "ws://" + ANDROID_EMULATOR_LOCALHOST
6058
+ ":" + RestClient.SERVER_PORT + "/example-endpoint/websocket");
6159

6260
mStompClient.lifecycle()
@@ -91,7 +89,7 @@ public void connectStomp(View view) {
9189
public void sendEchoViaStomp(View v) {
9290
mStompClient.send("/topic/hello-msg-mapping", "Echo STOMP " + mTimeFormat.format(new Date()))
9391
.compose(applySchedulers())
94-
.subscribe(aVoid -> {
92+
.subscribe(() -> {
9593
Log.d(TAG, "STOMP echo send successfully");
9694
}, throwable -> {
9795
Log.e(TAG, "Error send STOMP echo", throwable);
@@ -103,7 +101,7 @@ public void sendEchoViaRest(View v) {
103101
mRestPingDisposable = RestClient.getInstance().getExampleRepository()
104102
.sendRestEcho("Echo REST " + mTimeFormat.format(new Date()))
105103
.compose(applySchedulers())
106-
.subscribe(aVoid -> {
104+
.subscribe(() -> {
107105
Log.d(TAG, "REST echo send successfully");
108106
}, throwable -> {
109107
Log.e(TAG, "Error send REST echo", throwable);
@@ -122,8 +120,8 @@ private void toast(String text) {
122120
Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
123121
}
124122

125-
protected <T> FlowableTransformer<T, T> applySchedulers() {
126-
return tFlowable -> tFlowable
123+
protected CompletableTransformer applySchedulers() {
124+
return upstream -> upstream
127125
.unsubscribeOn(Schedulers.newThread())
128126
.subscribeOn(Schedulers.io())
129127
.observeOn(AndroidSchedulers.mainThread());
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Thu Feb 23 17:37:13 EET 2017
1+
#Tue Sep 05 08:26:08 MST 2017
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip

lib/build.gradle

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
apply plugin: 'com.android.library'
2-
apply plugin: 'me.tatarka.retrolambda'
32
apply plugin: 'com.github.dcendents.android-maven'
43

54
group='com.github.NaikSoftware'
65

76
android {
87
compileSdkVersion 25
9-
buildToolsVersion "25.0.1"
108

119
defaultConfig {
1210
minSdkVersion 16
@@ -30,12 +28,12 @@ android {
3028

3129

3230
dependencies {
33-
compile fileTree(include: ['*.jar'], dir: 'libs')
34-
testCompile 'junit:junit:4.12'
35-
compile "io.reactivex.rxjava2:rxjava:2.1.2"
31+
implementation "io.reactivex.rxjava2:rxjava:2.1.8"
3632
// Supported transports
37-
provided "org.java-websocket:java-websocket:1.3.2"
38-
provided 'com.squareup.okhttp3:okhttp:3.8.0'
33+
compileOnly 'org.java-websocket:Java-WebSocket:1.3.6'
34+
compileOnly 'com.squareup.okhttp3:okhttp:3.9.1'
35+
36+
implementation 'com.android.support:support-annotations:27.1.0'
3937
}
4038

4139
task sourcesJar(type: Jar) {

lib/src/androidTest/java/ua/naiksoftware/stomp/ApplicationTest.java

Lines changed: 0 additions & 13 deletions
This file was deleted.

lib/src/main/AndroidManifest.xml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2-
package="ua.naiksoftware.stomp">
1+
<manifest package="com.github.forresthopkinsa">
32

4-
<application/>
3+
<application />
54

65
</manifest>
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package ua.naiksoftware.stomp;
2+
3+
import android.support.annotation.NonNull;
4+
import android.support.annotation.Nullable;
5+
import android.util.Log;
6+
7+
import io.reactivex.Completable;
8+
import io.reactivex.Observable;
9+
import io.reactivex.subjects.PublishSubject;
10+
11+
/**
12+
* Created by forresthopkinsa on 8/8/2017.
13+
* <p>
14+
* Created because there was a lot of shared code between JWS and OkHttp connection providers.
15+
*/
16+
17+
abstract class AbstractConnectionProvider implements ConnectionProvider {
18+
19+
private static final String TAG = AbstractConnectionProvider.class.getSimpleName();
20+
21+
@NonNull
22+
private final PublishSubject<LifecycleEvent> mLifecycleStream;
23+
@NonNull
24+
private final PublishSubject<String> mMessagesStream;
25+
26+
AbstractConnectionProvider() {
27+
mLifecycleStream = PublishSubject.create();
28+
mMessagesStream = PublishSubject.create();
29+
}
30+
31+
@NonNull
32+
@Override
33+
public Observable<String> messages() {
34+
return mMessagesStream.startWith(initSocket().toObservable());
35+
}
36+
37+
/**
38+
* Simply close socket.
39+
* <p>
40+
* For example:
41+
* <pre>
42+
* webSocket.close();
43+
* </pre>
44+
*/
45+
abstract void rawDisconnect();
46+
47+
@Override
48+
public Completable disconnect() {
49+
return Completable
50+
.fromAction(this::rawDisconnect);
51+
}
52+
53+
private Completable initSocket() {
54+
return Completable
55+
.fromAction(this::createWebSocketConnection);
56+
}
57+
58+
// Doesn't do anything at all, only here as a stub
59+
public Completable setHeartbeat(int ms) {
60+
return Completable.complete();
61+
}
62+
63+
/**
64+
* Most important method: connects to websocket and notifies program of messages.
65+
* <p>
66+
* See implementations in OkHttpConnectionProvider and WebSocketsConnectionProvider.
67+
*/
68+
abstract void createWebSocketConnection();
69+
70+
@NonNull
71+
@Override
72+
public Completable send(String stompMessage) {
73+
return Completable.fromCallable(() -> {
74+
if (getSocket() == null) {
75+
throw new IllegalStateException("Not connected yet");
76+
} else {
77+
Log.d(TAG, "Send STOMP message: " + stompMessage);
78+
rawSend(stompMessage);
79+
return null;
80+
}
81+
});
82+
}
83+
84+
/**
85+
* Just a simple message send.
86+
* <p>
87+
* For example:
88+
* <pre>
89+
* webSocket.send(stompMessage);
90+
* </pre>
91+
*
92+
* @param stompMessage message to send
93+
*/
94+
abstract void rawSend(String stompMessage);
95+
96+
/**
97+
* Get socket object.
98+
* Used for null checking; this object is expected to be null when the connection is not yet established.
99+
* <p>
100+
* For example:
101+
* <pre>
102+
* return webSocket;
103+
* </pre>
104+
*/
105+
@Nullable
106+
abstract Object getSocket();
107+
108+
void emitLifecycleEvent(@NonNull LifecycleEvent lifecycleEvent) {
109+
Log.d(TAG, "Emit lifecycle event: " + lifecycleEvent.getType().name());
110+
mLifecycleStream.onNext(lifecycleEvent);
111+
}
112+
113+
void emitMessage(String stompMessage) {
114+
Log.d(TAG, "Emit STOMP message: " + stompMessage);
115+
mMessagesStream.onNext(stompMessage);
116+
}
117+
118+
@NonNull
119+
@Override
120+
public Observable<LifecycleEvent> lifecycle() {
121+
return mLifecycleStream;
122+
}
123+
}

0 commit comments

Comments
 (0)