Skip to content

Commit

Permalink
Optimize Login and API
Browse files Browse the repository at this point in the history
  • Loading branch information
teccheck committed Jun 27, 2021
1 parent 922f693 commit 7c5907d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.LinearLayout;
Expand All @@ -15,8 +13,6 @@

import com.google.android.material.textfield.TextInputLayout;

import java.util.Random;

import io.github.storagereloaded.android.R;
import io.github.storagereloaded.android.viewmodel.LoginViewModel;

Expand Down Expand Up @@ -111,6 +107,8 @@ private void userCheckComplete(int status, String sessionId, String serverAddres
displayStatus(status);

if (status == STATUS_OK) {
model.setSessionId(sessionId);

SharedPreferences prefs = getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(getString(R.string.preferences_server_address), serverAddress);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public LoginViewModel(@NonNull Application application) {
stoRe = ((StoReApp) application).getStoRe();
}

public void setSessionId(String sessionId) {
stoRe.setSessionId(sessionId);
}

public LiveData<Boolean> checkServer(String serverAddress) {
MutableLiveData<Boolean> data = new MutableLiveData<Boolean>() {
};
Expand Down
56 changes: 36 additions & 20 deletions app/src/main/java/io/github/storagereloaded/api/StoRe.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package io.github.storagereloaded.api;

import java.util.Arrays;
import java.util.List;

import io.github.storagereloaded.api.impl.DatabaseDummyImpl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
Expand All @@ -29,6 +29,22 @@ public boolean hasLogin() {
return serverAddress != null && sessionId != null;
}

public String getServerAddress() {
return serverAddress;
}

public void setServerAddress(String serverAddress) {
this.serverAddress = serverAddress;
}

public String getSessionId() {
return sessionId;
}

public void setSessionId(String sessionId) {
this.sessionId = sessionId;
}

public Call<InfoResponse> info() {
return service.info();
}
Expand All @@ -45,28 +61,16 @@ public Call<AuthResponse> auth(String serverAddress, String username, String pas
return service.auth(new AuthRequest(username, password));
}

public String getServerAddress() {
return serverAddress;
}

public void setServerAddress(String serverAddress) {
this.serverAddress = serverAddress;
}

public String getSessionId() {
return sessionId;
}

public void setSessionId(String sessionId) {
this.sessionId = sessionId;
public Call<List<Database>> getDatabases() {
return service.getDatabases();
}

public List<Database> getDatabases() {
return Arrays.asList(new DatabaseDummyImpl());
public Call<List<Item>> getItems() {
return service.getItems();
}

public boolean sync(List<Database> databases) {
return false;
public Call<List<Tag>> getTags() {
return service.getTags();
}

private String getServerUrl() {
Expand All @@ -77,8 +81,20 @@ private String getServerUrl() {
}

private void createService() {
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(chain -> {
Request.Builder builder = chain.request().newBuilder()
.header("Content-Type", "application/json")
.header("Accept", "application/json");
if (sessionId != null)
builder = builder.header("X-StoRe-Session", sessionId);

return chain.proceed(builder.build());
});

service = new Retrofit.Builder()
.baseUrl(getServerUrl())
.client(httpClient.build())
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(StoReService.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.Header;
import retrofit2.http.Headers;
import retrofit2.http.POST;

public interface StoReService {
@GET("info")
Call<InfoResponse> info();

@Headers("Content-Type: application/json")
@POST("v1/auth")
Call<AuthResponse> auth(@Body AuthRequest authRequest);

Expand Down

0 comments on commit 7c5907d

Please sign in to comment.