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);
// 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))
{
setResult(Constants.RESULT_CLOSE_ALL);
@ -230,10 +238,7 @@ public class MainActivity extends SubsonicTabActivity
// Remember the current theme.
theme = Util.getTheme(this);
boolean shouldShowDialog = Util.shouldShowWelcomeScreen(this);
// This will convert the server settings from the Preferences to the DB
if (shouldShowDialog) serverSettingsModel.getValue().getServerList();
showInfoDialog(shouldShowDialog);
showInfoDialog(showWelcomeScreen);
}
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_USE_FIVE_STAR_RATING = "use_five_star_rating";
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.
public static final int FREE_TRIAL_DAYS = 30;

View File

@ -1437,13 +1437,13 @@ public class Util
return typedValue.resourceId;
}
public static boolean shouldShowWelcomeScreen(Context context)
public static boolean isFirstRun(Context context)
{
SharedPreferences preferences = getPreferences(context);
boolean shown = preferences.getBoolean(Constants.PREFERENCES_KEY_WELCOME_SCREEN_SHOWN, false);
if (shown) return false;
boolean firstExecuted = preferences.getBoolean(Constants.PREFERENCES_KEY_FIRST_RUN_EXECUTED, false);
if (firstExecuted) return false;
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();
return true;
}

View File

@ -97,8 +97,8 @@ internal class EditServerActivity : AppCompatActivity() {
} else {
// Creating a new server
setTitle(R.string.server_editor_new_label)
saveButton!!.setOnClickListener {
currentServerSetting = ServerSetting()
saveButton!!.setOnClickListener {
if (getFields()) {
serverSettingsModel.saveNewItem(currentServerSetting)
finish()
@ -115,27 +115,16 @@ internal class EditServerActivity : AppCompatActivity() {
override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (item.itemId == android.R.id.home) {
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()
}
finishActivity()
return true
}
return super.onOptionsItemSelected(item)
}
override fun onBackPressed() {
finishActivity()
}
private fun applyTheme() {
val theme = Util.getTheme(this)
if (
@ -232,7 +221,7 @@ internal class EditServerActivity : AppCompatActivity() {
private fun areFieldsChanged(): Boolean {
if (currentServerSetting == null) {
return !serverNameEditText!!.editText?.text!!.isBlank() ||
!serverAddressEditText!!.editText?.text!!.isBlank() ||
serverAddressEditText!!.editText?.text.toString() != "http://" ||
!userNameEditText!!.editText?.text!!.isBlank() ||
!passwordEditText!!.editText?.text!!.isBlank()
}
@ -324,4 +313,26 @@ internal class EditServerActivity : AppCompatActivity() {
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.viewModelScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.moire.ultrasonic.R
import org.moire.ultrasonic.data.ActiveServerProvider
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 also try to convert existing settings from the Preferences
* This function is asynchronous, uses LiveData to provide the Setting.
* This function will try and convert settings from the Preferences to the Database
* @return True, if the migration was executed, False otherwise
*/
fun getServerList(): LiveData<List<ServerSetting>> {
viewModelScope.launch {
fun migrateFromPreferences(): Boolean {
var migrated = true
runBlocking {
val dbServerList = repository.loadAllServerSettings().toMutableList()
if (dbServerList.isEmpty()) {
@ -55,15 +57,32 @@ class ServerSettingsModel(
if (serverNum != 0) {
for (x in 1 until serverNum + 1) {
val newServerSetting = loadServerSettingFromPreferences(x, settings)
if (newServerSetting != null) {
dbServerList.add(newServerSetting)
repository.insert(newServerSetting)
Log.i(
TAG,
"Imported server from Preferences to Database: ${newServerSetting.name}"
"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), ""))
serverList.value = dbServerList
@ -186,13 +205,18 @@ class ServerSettingsModel(
private fun loadServerSettingFromPreferences(
id: Int,
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(
id,
id,
settings.getString(PREFERENCES_KEY_SERVER_NAME + id, "")!!,
settings.getString(PREFERENCES_KEY_SERVER_URL + id, "")!!,
settings.getString(PREFERENCES_KEY_USERNAME + id, "")!!,
url,
userName,
settings.getString(PREFERENCES_KEY_PASSWORD + id, "")!!,
settings.getBoolean(PREFERENCES_KEY_JUKEBOX_BY_DEFAULT + id, false),
settings.getBoolean(PREFERENCES_KEY_ALLOW_SELF_SIGNED_CERTIFICATE + id, false),

View File

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