Fixed first time migration

Minor fixes
This commit is contained in:
Nite 2020-09-19 11:56:10 +02:00
parent 6721500202
commit 234e4703a1
No known key found for this signature in database
GPG Key ID: 1D1AD59B1C6386C1
6 changed files with 82 additions and 41 deletions

View File

@ -69,6 +69,14 @@ public class MainActivity extends SubsonicTabActivity
{ {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
// Determine first run and migrate server settings to DB as early as possible
boolean showWelcomeScreen = Util.isFirstRun(this);
if (showWelcomeScreen)
{
// If settings were migrated, do not show welcome screen
showWelcomeScreen = !serverSettingsModel.getValue().migrateFromPreferences();
}
if (getIntent().hasExtra(Constants.INTENT_EXTRA_NAME_EXIT)) if (getIntent().hasExtra(Constants.INTENT_EXTRA_NAME_EXIT))
{ {
setResult(Constants.RESULT_CLOSE_ALL); setResult(Constants.RESULT_CLOSE_ALL);
@ -230,10 +238,7 @@ public class MainActivity extends SubsonicTabActivity
// Remember the current theme. // Remember the current theme.
theme = Util.getTheme(this); theme = Util.getTheme(this);
boolean shouldShowDialog = Util.shouldShowWelcomeScreen(this); showInfoDialog(showWelcomeScreen);
// This will convert the server settings from the Preferences to the DB
if (shouldShowDialog) serverSettingsModel.getValue().getServerList();
showInfoDialog(shouldShowDialog);
} }
private void loadSettings() private void loadSettings()

View File

@ -130,7 +130,7 @@ public final class Constants
public static final String PREFERENCES_KEY_FF_IMAGE_LOADER = "ff_new_image_loader"; public static final String PREFERENCES_KEY_FF_IMAGE_LOADER = "ff_new_image_loader";
public static final String PREFERENCES_KEY_USE_FIVE_STAR_RATING = "use_five_star_rating"; public static final String PREFERENCES_KEY_USE_FIVE_STAR_RATING = "use_five_star_rating";
public static final String PREFERENCES_KEY_CATEGORY_NOTIFICATIONS = "notificationsCategory"; public static final String PREFERENCES_KEY_CATEGORY_NOTIFICATIONS = "notificationsCategory";
public static final String PREFERENCES_KEY_WELCOME_SCREEN_SHOWN = "welcomeScreenShown"; public static final String PREFERENCES_KEY_FIRST_RUN_EXECUTED = "firstRunExecuted";
// Number of free trial days for non-licensed servers. // Number of free trial days for non-licensed servers.
public static final int FREE_TRIAL_DAYS = 30; public static final int FREE_TRIAL_DAYS = 30;

View File

@ -1437,13 +1437,13 @@ public class Util
return typedValue.resourceId; return typedValue.resourceId;
} }
public static boolean shouldShowWelcomeScreen(Context context) public static boolean isFirstRun(Context context)
{ {
SharedPreferences preferences = getPreferences(context); SharedPreferences preferences = getPreferences(context);
boolean shown = preferences.getBoolean(Constants.PREFERENCES_KEY_WELCOME_SCREEN_SHOWN, false); boolean firstExecuted = preferences.getBoolean(Constants.PREFERENCES_KEY_FIRST_RUN_EXECUTED, false);
if (shown) return false; if (firstExecuted) return false;
SharedPreferences.Editor editor = preferences.edit(); SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(Constants.PREFERENCES_KEY_WELCOME_SCREEN_SHOWN, true); editor.putBoolean(Constants.PREFERENCES_KEY_FIRST_RUN_EXECUTED, true);
editor.apply(); editor.apply();
return true; return true;
} }

View File

@ -97,8 +97,8 @@ internal class EditServerActivity : AppCompatActivity() {
} else { } else {
// Creating a new server // Creating a new server
setTitle(R.string.server_editor_new_label) setTitle(R.string.server_editor_new_label)
currentServerSetting = ServerSetting()
saveButton!!.setOnClickListener { saveButton!!.setOnClickListener {
currentServerSetting = ServerSetting()
if (getFields()) { if (getFields()) {
serverSettingsModel.saveNewItem(currentServerSetting) serverSettingsModel.saveNewItem(currentServerSetting)
finish() finish()
@ -115,27 +115,16 @@ internal class EditServerActivity : AppCompatActivity() {
override fun onOptionsItemSelected(item: MenuItem): Boolean { override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (item.itemId == android.R.id.home) { if (item.itemId == android.R.id.home) {
if (areFieldsChanged()) { finishActivity()
AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle(R.string.common_confirm)
.setMessage(R.string.server_editor_leave_confirmation)
.setPositiveButton(R.string.common_ok) { dialog, _ ->
dialog.dismiss()
finish()
}
.setNegativeButton(R.string.common_cancel) { dialog, _ ->
dialog.dismiss()
}
.show()
} else {
finish()
}
return true return true
} }
return super.onOptionsItemSelected(item) return super.onOptionsItemSelected(item)
} }
override fun onBackPressed() {
finishActivity()
}
private fun applyTheme() { private fun applyTheme() {
val theme = Util.getTheme(this) val theme = Util.getTheme(this)
if ( if (
@ -232,7 +221,7 @@ internal class EditServerActivity : AppCompatActivity() {
private fun areFieldsChanged(): Boolean { private fun areFieldsChanged(): Boolean {
if (currentServerSetting == null) { if (currentServerSetting == null) {
return !serverNameEditText!!.editText?.text!!.isBlank() || return !serverNameEditText!!.editText?.text!!.isBlank() ||
!serverAddressEditText!!.editText?.text!!.isBlank() || serverAddressEditText!!.editText?.text.toString() != "http://" ||
!userNameEditText!!.editText?.text!!.isBlank() || !userNameEditText!!.editText?.text!!.isBlank() ||
!passwordEditText!!.editText?.text!!.isBlank() !passwordEditText!!.editText?.text!!.isBlank()
} }
@ -324,4 +313,26 @@ internal class EditServerActivity : AppCompatActivity() {
throw IOException("Failed to perform request: " + response.code()) throw IOException("Failed to perform request: " + response.code())
} }
} }
/**
* Finishes the Activity, after confirmation from the user if needed
*/
private fun finishActivity() {
if (areFieldsChanged()) {
AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle(R.string.common_confirm)
.setMessage(R.string.server_editor_leave_confirmation)
.setPositiveButton(R.string.common_ok) { dialog, _ ->
dialog.dismiss()
finish()
}
.setNegativeButton(R.string.common_cancel) { dialog, _ ->
dialog.dismiss()
}
.show()
} else {
finish()
}
}
} }

View File

@ -9,6 +9,7 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.moire.ultrasonic.R import org.moire.ultrasonic.R
import org.moire.ultrasonic.data.ActiveServerProvider import org.moire.ultrasonic.data.ActiveServerProvider
import org.moire.ultrasonic.data.ServerSetting import org.moire.ultrasonic.data.ServerSetting
@ -39,12 +40,13 @@ class ServerSettingsModel(
} }
/** /**
* Retrieves the list of the configured servers from the database. * This function will try and convert settings from the Preferences to the Database
* This function will also try to convert existing settings from the Preferences * @return True, if the migration was executed, False otherwise
* This function is asynchronous, uses LiveData to provide the Setting.
*/ */
fun getServerList(): LiveData<List<ServerSetting>> { fun migrateFromPreferences(): Boolean {
viewModelScope.launch { var migrated = true
runBlocking {
val dbServerList = repository.loadAllServerSettings().toMutableList() val dbServerList = repository.loadAllServerSettings().toMutableList()
if (dbServerList.isEmpty()) { if (dbServerList.isEmpty()) {
@ -55,15 +57,32 @@ class ServerSettingsModel(
if (serverNum != 0) { if (serverNum != 0) {
for (x in 1 until serverNum + 1) { for (x in 1 until serverNum + 1) {
val newServerSetting = loadServerSettingFromPreferences(x, settings) val newServerSetting = loadServerSettingFromPreferences(x, settings)
dbServerList.add(newServerSetting) if (newServerSetting != null) {
repository.insert(newServerSetting) dbServerList.add(newServerSetting)
Log.i( repository.insert(newServerSetting)
TAG, Log.i(
"Imported server from Preferences to Database: ${newServerSetting.name}" TAG,
) "Imported server from Preferences to Database:" +
" ${newServerSetting.name}"
)
}
} }
} else {
migrated = false
} }
} }
}
return migrated
}
/**
* Retrieves the list of the configured servers from the database.
* This function is asynchronous, uses LiveData to provide the Setting.
*/
fun getServerList(): LiveData<List<ServerSetting>> {
viewModelScope.launch {
val dbServerList = repository.loadAllServerSettings().toMutableList()
dbServerList.add(0, ServerSetting(context.getString(R.string.main_offline), "")) dbServerList.add(0, ServerSetting(context.getString(R.string.main_offline), ""))
serverList.value = dbServerList serverList.value = dbServerList
@ -186,13 +205,18 @@ class ServerSettingsModel(
private fun loadServerSettingFromPreferences( private fun loadServerSettingFromPreferences(
id: Int, id: Int,
settings: SharedPreferences settings: SharedPreferences
): ServerSetting { ): ServerSetting? {
val url = settings.getString(PREFERENCES_KEY_SERVER_URL + id, "")
val userName = settings.getString(PREFERENCES_KEY_USERNAME + id, "")
if (url.isNullOrEmpty() || userName.isNullOrEmpty()) return null
return ServerSetting( return ServerSetting(
id, id,
id, id,
settings.getString(PREFERENCES_KEY_SERVER_NAME + id, "")!!, settings.getString(PREFERENCES_KEY_SERVER_NAME + id, "")!!,
settings.getString(PREFERENCES_KEY_SERVER_URL + id, "")!!, url,
settings.getString(PREFERENCES_KEY_USERNAME + id, "")!!, userName,
settings.getString(PREFERENCES_KEY_PASSWORD + id, "")!!, settings.getString(PREFERENCES_KEY_PASSWORD + id, "")!!,
settings.getBoolean(PREFERENCES_KEY_JUKEBOX_BY_DEFAULT + id, false), settings.getBoolean(PREFERENCES_KEY_JUKEBOX_BY_DEFAULT + id, false),
settings.getBoolean(PREFERENCES_KEY_ALLOW_SELF_SIGNED_CERTIFICATE + id, false), settings.getBoolean(PREFERENCES_KEY_ALLOW_SELF_SIGNED_CERTIFICATE + id, false),

View File

@ -41,6 +41,7 @@
<com.google.android.material.textfield.TextInputEditText <com.google.android.material.textfield.TextInputEditText
a:layout_width="match_parent" a:layout_width="match_parent"
a:layout_height="wrap_content" a:layout_height="wrap_content"
a:text="http://"
a:inputType="textWebEmailAddress" /> a:inputType="textWebEmailAddress" />
</com.google.android.material.textfield.TextInputLayout> </com.google.android.material.textfield.TextInputLayout>