add servers to the new database
This commit is contained in:
parent
a45ae46d31
commit
eb5b4787ed
7 changed files with 82 additions and 31 deletions
|
|
@ -53,6 +53,7 @@ public class App extends Application {
|
|||
public static JellyDatabase createDatabase(Context context) {
|
||||
return Room.databaseBuilder(context, JellyDatabase.class, "database")
|
||||
.allowMainThreadQueries()
|
||||
.addMigrations(JellyDatabase.Migration2)
|
||||
.build();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ import com.dkanada.gramophone.App;
|
|||
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.util.PreferenceUtil;
|
||||
import com.kabouzeid.appthemehelper.ThemeStore;
|
||||
|
||||
|
|
@ -117,14 +118,14 @@ public class LoginActivity extends AbsBaseActivity implements View.OnClickListen
|
|||
});
|
||||
}
|
||||
|
||||
private void check(String server, String user, String token) {
|
||||
private void check(String url, String user, String token) {
|
||||
App.getApiClient().GetSystemInfoAsync(new Response<SystemInfo>() {
|
||||
@Override
|
||||
public void onResponse(SystemInfo result) {
|
||||
if (result.getVersion().charAt(0) == '1') {
|
||||
PreferenceUtil.getInstance(LoginActivity.this).setServer(server);
|
||||
PreferenceUtil.getInstance(LoginActivity.this).setUser(user);
|
||||
PreferenceUtil.getInstance(LoginActivity.this).setToken(token);
|
||||
Server server = new Server(result.getServerName(), url, user, token);
|
||||
PreferenceUtil.getInstance(LoginActivity.this).setServer(server.id);
|
||||
App.getDatabase().serverDao().insertServer(server);
|
||||
|
||||
Intent intent = new Intent(LoginActivity.this, SplashActivity.class);
|
||||
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ import androidx.annotation.RequiresApi;
|
|||
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.util.PreferenceUtil;
|
||||
|
||||
import org.jellyfin.apiclient.interaction.EmptyResponse;
|
||||
|
|
@ -75,13 +76,14 @@ public class SplashActivity extends AbsBaseActivity {
|
|||
}
|
||||
|
||||
public void login() {
|
||||
if (PreferenceUtil.getInstance(this).getToken() == null) {
|
||||
if (PreferenceUtil.getInstance(this).getServer().isEmpty()) {
|
||||
launchLoginActivity();
|
||||
} else {
|
||||
final Context context = this;
|
||||
Server server = App.getDatabase().serverDao().getServer(PreferenceUtil.getInstance(this).getServer());
|
||||
|
||||
App.getApiClient().ChangeServerLocation(PreferenceUtil.getInstance(this).getServer());
|
||||
App.getApiClient().SetAuthenticationInfo(PreferenceUtil.getInstance(this).getToken(), PreferenceUtil.getInstance(this).getUser());
|
||||
App.getApiClient().ChangeServerLocation(server.url);
|
||||
App.getApiClient().SetAuthenticationInfo(server.token, server.user);
|
||||
App.getApiClient().GetSystemInfoAsync(new Response<SystemInfo>() {
|
||||
@Override
|
||||
public void onResponse(SystemInfo result) {
|
||||
|
|
|
|||
|
|
@ -1,18 +1,32 @@
|
|||
package com.dkanada.gramophone.database;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.room.RoomDatabase;
|
||||
import androidx.room.migration.Migration;
|
||||
import androidx.sqlite.db.SupportSQLiteDatabase;
|
||||
|
||||
import com.dkanada.gramophone.model.Server;
|
||||
import com.dkanada.gramophone.model.Song;
|
||||
|
||||
@androidx.room.Database(
|
||||
entities = {
|
||||
Song.class,
|
||||
QueueSong.class
|
||||
QueueSong.class,
|
||||
Server.class
|
||||
},
|
||||
version = 1,
|
||||
version = 2,
|
||||
exportSchema = false
|
||||
)
|
||||
public abstract class JellyDatabase extends RoomDatabase {
|
||||
public abstract QueueSongDao queueSongDao();
|
||||
public abstract SongDao songDao();
|
||||
public abstract ServerDao serverDao();
|
||||
|
||||
public static final Migration Migration2 = new Migration(1, 2) {
|
||||
@Override
|
||||
public void migrate(@NonNull SupportSQLiteDatabase database) {
|
||||
database.execSQL("CREATE TABLE servers (id TEXT NOT NULL PRIMARY KEY, name TEXT,"
|
||||
+ "url TEXT, user TEXT, token TEXT)");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,21 @@
|
|||
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.Server;
|
||||
|
||||
@Dao
|
||||
public interface ServerDao {
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
void insertServer(Server server);
|
||||
|
||||
@Delete
|
||||
void deleteServer(Server server);
|
||||
|
||||
@Query("SELECT * FROM servers WHERE id = :id")
|
||||
Server getServer(String id);
|
||||
}
|
||||
34
app/src/main/java/com/dkanada/gramophone/model/Server.java
Normal file
34
app/src/main/java/com/dkanada/gramophone/model/Server.java
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
package com.dkanada.gramophone.model;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.room.Entity;
|
||||
import androidx.room.PrimaryKey;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@Entity(tableName = "servers")
|
||||
public class Server {
|
||||
@NonNull
|
||||
@PrimaryKey
|
||||
public String id;
|
||||
|
||||
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) {
|
||||
this.id = UUID.randomUUID().toString();
|
||||
|
||||
this.name = name;
|
||||
this.url = url;
|
||||
|
||||
this.user = user;
|
||||
this.token = token;
|
||||
}
|
||||
}
|
||||
|
|
@ -25,8 +25,6 @@ import java.util.Set;
|
|||
|
||||
public final class PreferenceUtil {
|
||||
public static final String SERVER = "server";
|
||||
public static final String USER = "user";
|
||||
public static final String TOKEN = "token";
|
||||
|
||||
public static final String SHUFFLE = "shuffle";
|
||||
public static final String REPEAT = "repeat";
|
||||
|
|
@ -476,24 +474,4 @@ public final class PreferenceUtil {
|
|||
editor.putString(SERVER, server);
|
||||
editor.apply();
|
||||
}
|
||||
|
||||
public String getUser() {
|
||||
return mPreferences.getString(USER, "");
|
||||
}
|
||||
|
||||
public void setUser(String user) {
|
||||
final SharedPreferences.Editor editor = mPreferences.edit();
|
||||
editor.putString(USER, user);
|
||||
editor.apply();
|
||||
}
|
||||
|
||||
public String getToken() {
|
||||
return mPreferences.getString(TOKEN, "");
|
||||
}
|
||||
|
||||
public void setToken(String token) {
|
||||
final SharedPreferences.Editor editor = mPreferences.edit();
|
||||
editor.putString(TOKEN, token);
|
||||
editor.apply();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue