use the http responses directly for object creation

This commit is contained in:
dkanada 2021-04-13 17:42:24 +09:00
commit 420b0dff79
3 changed files with 20 additions and 15 deletions

View file

@ -100,9 +100,10 @@ public class LoginActivity extends AbsBaseActivity implements View.OnClickListen
App.getApiClient().ChangeServerLocation(server); App.getApiClient().ChangeServerLocation(server);
App.getApiClient().AuthenticateUserAsync(username, password, new Response<AuthenticationResult>() { App.getApiClient().AuthenticateUserAsync(username, password, new Response<AuthenticationResult>() {
@Override @Override
public void onResponse(AuthenticationResult result) { public void onResponse(AuthenticationResult authenticationResult) {
if (result.getAccessToken() == null) return; if (authenticationResult.getAccessToken() != null) {
check(server, result.getUser().getId(), result.getAccessToken()); check(authenticationResult);
}
} }
@Override @Override
@ -119,13 +120,13 @@ public class LoginActivity extends AbsBaseActivity implements View.OnClickListen
}); });
} }
private void check(String url, String name, String token) { private void check(AuthenticationResult authenticationResult) {
App.getApiClient().GetSystemInfoAsync(new Response<SystemInfo>() { App.getApiClient().GetSystemInfoAsync(new Response<SystemInfo>() {
@Override @Override
public void onResponse(SystemInfo result) { public void onResponse(SystemInfo result) {
if (result.getVersion().charAt(0) == '1') { if (result.getVersion().charAt(0) == '1') {
Server server = new Server(result.getServerName(), url); Server server = new Server(result);
User user = new User(server.id, name, token); User user = new User(authenticationResult);
App.getDatabase().serverDao().insertServer(server); App.getDatabase().serverDao().insertServer(server);
App.getDatabase().userDao().insertUser(user); App.getDatabase().userDao().insertUser(user);

View file

@ -4,6 +4,8 @@ import androidx.annotation.NonNull;
import androidx.room.Entity; import androidx.room.Entity;
import androidx.room.PrimaryKey; import androidx.room.PrimaryKey;
import org.jellyfin.apiclient.model.system.SystemInfo;
import java.util.UUID; import java.util.UUID;
@Entity(tableName = "servers") @Entity(tableName = "servers")
@ -19,10 +21,10 @@ public class Server {
this.id = UUID.randomUUID().toString(); this.id = UUID.randomUUID().toString();
} }
public Server(String name, String url) { public Server(SystemInfo systemInfo) {
this.id = UUID.randomUUID().toString(); this.id = systemInfo.getId();
this.name = name; this.name = systemInfo.getServerName();
this.url = url; this.url = systemInfo.getWanAddress();
} }
} }

View file

@ -5,6 +5,8 @@ import androidx.room.Entity;
import androidx.room.ForeignKey; import androidx.room.ForeignKey;
import androidx.room.PrimaryKey; import androidx.room.PrimaryKey;
import org.jellyfin.apiclient.model.users.AuthenticationResult;
import java.util.UUID; import java.util.UUID;
@Entity(tableName = "users") @Entity(tableName = "users")
@ -28,12 +30,12 @@ public class User {
this.id = UUID.randomUUID().toString(); this.id = UUID.randomUUID().toString();
} }
public User(String serverId, String name, String token) { public User(AuthenticationResult result) {
this.id = UUID.randomUUID().toString(); this.id = result.getUser().getId();
this.serverId = serverId; this.serverId = result.getServerId();
this.name = name; this.name = result.getUser().getName();
this.token = token; this.token = result.getAccessToken();
} }
} }