add separate table for users
This commit is contained in:
parent
996ece8ee5
commit
af37f7b307
8 changed files with 126 additions and 42 deletions
|
|
@ -54,6 +54,7 @@ public class App extends Application {
|
|||
return Room.databaseBuilder(context, JellyDatabase.class, "database")
|
||||
.allowMainThreadQueries()
|
||||
.addMigrations(JellyDatabase.Migration2)
|
||||
.addMigrations(JellyDatabase.Migration3)
|
||||
.build();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ import com.dkanada.gramophone.R;
|
|||
import com.dkanada.gramophone.databinding.ActivityLoginBinding;
|
||||
import com.dkanada.gramophone.activities.base.AbsBaseActivity;
|
||||
import com.dkanada.gramophone.model.Server;
|
||||
import com.dkanada.gramophone.model.User;
|
||||
import com.dkanada.gramophone.util.PreferenceUtil;
|
||||
import com.kabouzeid.appthemehelper.ThemeStore;
|
||||
|
||||
|
|
@ -118,14 +119,18 @@ public class LoginActivity extends AbsBaseActivity implements View.OnClickListen
|
|||
});
|
||||
}
|
||||
|
||||
private void check(String url, String user, String token) {
|
||||
private void check(String url, String name, String token) {
|
||||
App.getApiClient().GetSystemInfoAsync(new Response<SystemInfo>() {
|
||||
@Override
|
||||
public void onResponse(SystemInfo result) {
|
||||
if (result.getVersion().charAt(0) == '1') {
|
||||
Server server = new Server(result.getServerName(), url, user, token);
|
||||
PreferenceUtil.getInstance(LoginActivity.this).setServer(server.id);
|
||||
Server server = new Server(result.getServerName(), url);
|
||||
User user = new User(server.id, name, token);
|
||||
|
||||
App.getDatabase().serverDao().insertServer(server);
|
||||
App.getDatabase().userDao().insertUser(user);
|
||||
PreferenceUtil.getInstance(LoginActivity.this).setServer(server.id);
|
||||
PreferenceUtil.getInstance(LoginActivity.this).setUser(user.id);
|
||||
|
||||
Intent intent = new Intent(LoginActivity.this, SplashActivity.class);
|
||||
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ import com.dkanada.gramophone.App;
|
|||
import com.dkanada.gramophone.R;
|
||||
import com.dkanada.gramophone.activities.base.AbsBaseActivity;
|
||||
import com.dkanada.gramophone.model.Server;
|
||||
import com.dkanada.gramophone.model.User;
|
||||
import com.dkanada.gramophone.util.NavigationUtil;
|
||||
import com.dkanada.gramophone.util.PreferenceUtil;
|
||||
|
||||
|
|
@ -78,32 +79,34 @@ public class SplashActivity extends AbsBaseActivity {
|
|||
}
|
||||
|
||||
public void login() {
|
||||
if (PreferenceUtil.getInstance(this).getServer().isEmpty()) {
|
||||
Context context = this;
|
||||
Server server = App.getDatabase().serverDao().getServer(PreferenceUtil.getInstance(this).getServer());
|
||||
User user = App.getDatabase().userDao().getUser(PreferenceUtil.getInstance(this).getUser());
|
||||
|
||||
if (server == null || user == null) {
|
||||
NavigationUtil.goToLogin(this);
|
||||
} else {
|
||||
final Context context = this;
|
||||
Server server = App.getDatabase().serverDao().getServer(PreferenceUtil.getInstance(this).getServer());
|
||||
|
||||
App.getApiClient().ChangeServerLocation(server.url);
|
||||
App.getApiClient().SetAuthenticationInfo(server.token, server.user);
|
||||
App.getApiClient().GetSystemInfoAsync(new Response<SystemInfo>() {
|
||||
@Override
|
||||
public void onResponse(SystemInfo result) {
|
||||
ClientCapabilities clientCapabilities = new ClientCapabilities();
|
||||
clientCapabilities.setSupportsMediaControl(true);
|
||||
clientCapabilities.setSupportsPersistentIdentifier(true);
|
||||
|
||||
App.getApiClient().ensureWebSocket();
|
||||
App.getApiClient().ReportCapabilities(clientCapabilities, new EmptyResponse());
|
||||
|
||||
NavigationUtil.goToMain(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Exception exception) {
|
||||
NavigationUtil.goToLogin(context);
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
App.getApiClient().ChangeServerLocation(server.url);
|
||||
App.getApiClient().SetAuthenticationInfo(user.token, user.name);
|
||||
App.getApiClient().GetSystemInfoAsync(new Response<SystemInfo>() {
|
||||
@Override
|
||||
public void onResponse(SystemInfo result) {
|
||||
ClientCapabilities clientCapabilities = new ClientCapabilities();
|
||||
clientCapabilities.setSupportsMediaControl(true);
|
||||
clientCapabilities.setSupportsPersistentIdentifier(true);
|
||||
|
||||
App.getApiClient().ensureWebSocket();
|
||||
App.getApiClient().ReportCapabilities(clientCapabilities, new EmptyResponse());
|
||||
|
||||
NavigationUtil.goToMain(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Exception exception) {
|
||||
NavigationUtil.goToLogin(context);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,20 +7,23 @@ import androidx.sqlite.db.SupportSQLiteDatabase;
|
|||
|
||||
import com.dkanada.gramophone.model.Server;
|
||||
import com.dkanada.gramophone.model.Song;
|
||||
import com.dkanada.gramophone.model.User;
|
||||
|
||||
@androidx.room.Database(
|
||||
entities = {
|
||||
Server.class,
|
||||
Song.class,
|
||||
QueueSong.class,
|
||||
Server.class
|
||||
User.class
|
||||
},
|
||||
version = 2,
|
||||
version = 3,
|
||||
exportSchema = false
|
||||
)
|
||||
public abstract class JellyDatabase extends RoomDatabase {
|
||||
public abstract QueueSongDao queueSongDao();
|
||||
public abstract SongDao songDao();
|
||||
public abstract ServerDao serverDao();
|
||||
public abstract SongDao songDao();
|
||||
public abstract QueueSongDao queueSongDao();
|
||||
public abstract UserDao userDao();
|
||||
|
||||
public static final Migration Migration2 = new Migration(1, 2) {
|
||||
@Override
|
||||
|
|
@ -29,4 +32,16 @@ public abstract class JellyDatabase extends RoomDatabase {
|
|||
+ "url TEXT, user TEXT, token TEXT)");
|
||||
}
|
||||
};
|
||||
|
||||
public static final Migration Migration3 = new Migration(2, 3) {
|
||||
@Override
|
||||
public void migrate(@NonNull SupportSQLiteDatabase database) {
|
||||
database.execSQL("DROP TABLE servers");
|
||||
|
||||
database.execSQL("CREATE TABLE servers (id TEXT NOT NULL PRIMARY KEY,"
|
||||
+ "name TEXT, url TEXT)");
|
||||
database.execSQL("CREATE TABLE users (id TEXT NOT NULL PRIMARY KEY,"
|
||||
+ "serverId TEXT, name TEXT, token TEXT)");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,26 @@
|
|||
package com.dkanada.gramophone.database;
|
||||
|
||||
import androidx.room.Dao;
|
||||
import androidx.room.Delete;
|
||||
import androidx.room.Insert;
|
||||
import androidx.room.OnConflictStrategy;
|
||||
import androidx.room.Query;
|
||||
|
||||
import com.dkanada.gramophone.model.User;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Dao
|
||||
public interface UserDao {
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
void insertUser(User user);
|
||||
|
||||
@Delete
|
||||
void deleteUser(User user);
|
||||
|
||||
@Query("SELECT * FROM users")
|
||||
List<User> getUsers();
|
||||
|
||||
@Query("SELECT * FROM users WHERE id = :id")
|
||||
User getUser(String id);
|
||||
}
|
||||
|
|
@ -11,6 +11,8 @@ import com.afollestad.materialdialogs.MaterialDialog;
|
|||
import com.dkanada.gramophone.App;
|
||||
import com.dkanada.gramophone.R;
|
||||
import com.dkanada.gramophone.activities.login.LoginActivity;
|
||||
import com.dkanada.gramophone.helper.MusicPlayerRemote;
|
||||
import com.dkanada.gramophone.util.NavigationUtil;
|
||||
|
||||
import org.jellyfin.apiclient.interaction.EmptyResponse;
|
||||
|
||||
|
|
@ -30,10 +32,9 @@ public class ConfirmLogoutDialog extends DialogFragment {
|
|||
.negativeText(android.R.string.cancel)
|
||||
.onPositive((dialog, which) -> {
|
||||
App.getApiClient().Logout(new EmptyResponse());
|
||||
MusicPlayerRemote.clearQueue();
|
||||
|
||||
Intent intent = new Intent(getActivity(), LoginActivity.class);
|
||||
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
|
||||
this.startActivity(intent);
|
||||
NavigationUtil.goToLogin(requireContext());
|
||||
})
|
||||
.build();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,20 +15,14 @@ public class Server {
|
|||
public String name;
|
||||
public String url;
|
||||
|
||||
public String user;
|
||||
public String token;
|
||||
|
||||
public Server() {
|
||||
this.id = UUID.randomUUID().toString();
|
||||
}
|
||||
|
||||
public Server(String name, String url, String user, String token) {
|
||||
public Server(String name, String url) {
|
||||
this.id = UUID.randomUUID().toString();
|
||||
|
||||
this.name = name;
|
||||
this.url = url;
|
||||
|
||||
this.user = user;
|
||||
this.token = token;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
39
app/src/main/java/com/dkanada/gramophone/model/User.java
Normal file
39
app/src/main/java/com/dkanada/gramophone/model/User.java
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
package com.dkanada.gramophone.model;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.room.Entity;
|
||||
import androidx.room.ForeignKey;
|
||||
import androidx.room.PrimaryKey;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@Entity(tableName = "users")
|
||||
public class User {
|
||||
@NonNull
|
||||
@PrimaryKey
|
||||
public String id;
|
||||
|
||||
@ForeignKey(
|
||||
entity = Server.class,
|
||||
parentColumns = {"id"},
|
||||
childColumns = {"serverId"},
|
||||
onDelete = ForeignKey.CASCADE
|
||||
)
|
||||
public String serverId;
|
||||
|
||||
public String name;
|
||||
public String token;
|
||||
|
||||
public User() {
|
||||
this.id = UUID.randomUUID().toString();
|
||||
}
|
||||
|
||||
public User(String serverId, String name, String token) {
|
||||
this.id = UUID.randomUUID().toString();
|
||||
|
||||
this.serverId = serverId;
|
||||
|
||||
this.name = name;
|
||||
this.token = token;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue