Reduce function length

This commit is contained in:
Maxence G 2021-05-29 15:00:46 +02:00
parent f510638571
commit fd48367cab
No known key found for this signature in database
GPG Key ID: DC1FD9409E3FE284
5 changed files with 48 additions and 74 deletions

View File

@ -359,20 +359,6 @@ public class Util
return Math.min(Math.max(percent,0),100) + " %"; return Math.min(Math.max(percent,0),100) + " %";
} }
/**
* Return a Mark depending on boolean ( or )
*
* @param value Is true or false
* @return The corresponding mark.
*/
public static String boolToMark(boolean value)
{
if (value)
return "✔️";
else
return "";
}
/** /**
* Converts a byte-count to a formatted string suitable for display to the user. * Converts a byte-count to a formatted string suitable for display to the user.

View File

@ -377,10 +377,10 @@ class NavigationActivity : AppCompatActivity() {
podcastsMenuItem?.isVisible = false podcastsMenuItem?.isVisible = false
return return
} }
val activeServerProvider = inject(ActiveServerProvider::class.java) val activeServerProvider = inject(ActiveServerProvider::class.java).value.getActiveServer()
chatMenuItem?.isVisible = activeServerProvider.value.getActiveServer().chatSupport chatMenuItem?.isVisible = activeServerProvider.chatSupport != false
bookmarksMenuItem?.isVisible = activeServerProvider.value.getActiveServer().bookmarkSupport bookmarksMenuItem?.isVisible = activeServerProvider.bookmarkSupport != false
sharesMenuItem?.isVisible = activeServerProvider.value.getActiveServer().shareSupport sharesMenuItem?.isVisible = activeServerProvider.shareSupport != false
podcastsMenuItem?.isVisible = activeServerProvider.value.getActiveServer().podcastSupport podcastsMenuItem?.isVisible = activeServerProvider.podcastSupport != false
} }
} }

View File

@ -28,16 +28,16 @@ val MIGRATION_1_2: Migration = object : Migration(1, 2) {
val MIGRATION_2_3: Migration = object : Migration(2, 3) { val MIGRATION_2_3: Migration = object : Migration(2, 3) {
override fun migrate(database: SupportSQLiteDatabase) { override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL( database.execSQL(
"ALTER TABLE ServerSetting ADD COLUMN chatSupport INTEGER NOT NULL DEFAULT(1)" "ALTER TABLE ServerSetting ADD COLUMN chatSupport INTEGER"
) )
database.execSQL( database.execSQL(
"ALTER TABLE ServerSetting ADD COLUMN bookmarkSupport INTEGER NOT NULL DEFAULT(1)" "ALTER TABLE ServerSetting ADD COLUMN bookmarkSupport INTEGER"
) )
database.execSQL( database.execSQL(
"ALTER TABLE ServerSetting ADD COLUMN shareSupport INTEGER NOT NULL DEFAULT(1)" "ALTER TABLE ServerSetting ADD COLUMN shareSupport INTEGER"
) )
database.execSQL( database.execSQL(
"ALTER TABLE ServerSetting ADD COLUMN podcastSupport INTEGER NOT NULL DEFAULT(1)" "ALTER TABLE ServerSetting ADD COLUMN podcastSupport INTEGER"
) )
} }
} }

View File

@ -30,10 +30,10 @@ data class ServerSetting(
@ColumnInfo(name = "ldapSupport") var ldapSupport: Boolean, @ColumnInfo(name = "ldapSupport") var ldapSupport: Boolean,
@ColumnInfo(name = "musicFolderId") var musicFolderId: String?, @ColumnInfo(name = "musicFolderId") var musicFolderId: String?,
@ColumnInfo(name = "minimumApiVersion") var minimumApiVersion: String?, @ColumnInfo(name = "minimumApiVersion") var minimumApiVersion: String?,
@ColumnInfo(name = "chatSupport") var chatSupport: Boolean = true, @ColumnInfo(name = "chatSupport") var chatSupport: Boolean? = null,
@ColumnInfo(name = "bookmarkSupport") var bookmarkSupport: Boolean = true, @ColumnInfo(name = "bookmarkSupport") var bookmarkSupport: Boolean? = null,
@ColumnInfo(name = "shareSupport") var shareSupport: Boolean = true, @ColumnInfo(name = "shareSupport") var shareSupport: Boolean? = null,
@ColumnInfo(name = "podcastSupport") var podcastSupport: Boolean = true @ColumnInfo(name = "podcastSupport") var podcastSupport: Boolean? = null
) { ) {
constructor() : this ( constructor() : this (
-1, 0, "", "", "", "", false, false, false, null, null -1, 0, "", "", "", "", false, false, false, null, null

View File

@ -11,9 +11,6 @@ import androidx.lifecycle.Observer
import androidx.navigation.fragment.findNavController import androidx.navigation.fragment.findNavController
import com.google.android.material.switchmaterial.SwitchMaterial import com.google.android.material.switchmaterial.SwitchMaterial
import com.google.android.material.textfield.TextInputLayout import com.google.android.material.textfield.TextInputLayout
import java.io.IOException
import java.net.MalformedURLException
import java.net.URL
import org.koin.android.ext.android.inject import org.koin.android.ext.android.inject
import org.koin.android.viewmodel.ext.android.viewModel import org.koin.android.viewmodel.ext.android.viewModel
import org.moire.ultrasonic.BuildConfig import org.moire.ultrasonic.BuildConfig
@ -29,7 +26,11 @@ import org.moire.ultrasonic.util.Constants
import org.moire.ultrasonic.util.ErrorDialog import org.moire.ultrasonic.util.ErrorDialog
import org.moire.ultrasonic.util.ModalBackgroundTask import org.moire.ultrasonic.util.ModalBackgroundTask
import org.moire.ultrasonic.util.Util import org.moire.ultrasonic.util.Util
import retrofit2.Call
import timber.log.Timber import timber.log.Timber
import java.io.IOException
import java.net.MalformedURLException
import java.net.URL
/** /**
* Displays a form where server settings can be created / edited * Displays a form where server settings can be created / edited
@ -300,18 +301,36 @@ class EditServerFragment : Fragment(), OnBackPressedHandler {
activity, activity,
false false
) { ) {
fun boolToMark(value: Boolean?): String {
if (value == null)
return ""
return if (value) "✔️" else ""
}
@Throws(Throwable::class) fun getProgress(): String {
override fun doInBackground(): String { return String.format(
"""
var progressString =
"""
|%s - ${activity.resources.getString(R.string.button_bar_chat)} |%s - ${activity.resources.getString(R.string.button_bar_chat)}
|%s - ${activity.resources.getString(R.string.button_bar_bookmarks)} |%s - ${activity.resources.getString(R.string.button_bar_bookmarks)}
|%s - ${activity.resources.getString(R.string.button_bar_shares)} |%s - ${activity.resources.getString(R.string.button_bar_shares)}
|%s - ${activity.resources.getString(R.string.button_bar_podcasts)} |%s - ${activity.resources.getString(R.string.button_bar_podcasts)}
""".trimMargin() """.trimMargin(),
updateProgress(String.format(progressString, "", "", "", "")) boolToMark(currentServerSetting!!.chatSupport),
boolToMark(currentServerSetting!!.bookmarkSupport),
boolToMark(currentServerSetting!!.shareSupport),
boolToMark(currentServerSetting!!.podcastSupport)
)
}
@Throws(Throwable::class)
override fun doInBackground(): String {
currentServerSetting!!.chatSupport = null
currentServerSetting!!.bookmarkSupport = null
currentServerSetting!!.shareSupport = null
currentServerSetting!!.podcastSupport = null
updateProgress(getProgress())
val configuration = SubsonicClientConfiguration( val configuration = SubsonicClientConfiguration(
currentServerSetting!!.url, currentServerSetting!!.url,
@ -340,72 +359,41 @@ class EditServerFragment : Fragment(), OnBackPressedHandler {
pingResponse = subsonicApiClient.api.ping().execute() pingResponse = subsonicApiClient.api.ping().execute()
ApiCallResponseChecker.checkResponseSuccessful(pingResponse) ApiCallResponseChecker.checkResponseSuccessful(pingResponse)
updateProgress(String.format(progressString, "", "", "", ""))
currentServerSetting!!.chatSupport = try { currentServerSetting!!.chatSupport = try {
subsonicApiClient.api.getChatMessages().execute() subsonicApiClient.api.getChatMessages().execute()
true true
} catch (e: IOException) { false } } catch (e: IOException) { false }
updateProgress( updateProgress(getProgress())
String.format(
progressString,
Util.boolToMark(currentServerSetting!!.chatSupport),
"", "", ""
)
)
currentServerSetting!!.bookmarkSupport = try { currentServerSetting!!.bookmarkSupport = try {
subsonicApiClient.api.getBookmarks().execute() subsonicApiClient.api.getBookmarks().execute()
true true
} catch (e: IOException) { false } } catch (e: IOException) { false }
updateProgress( updateProgress(getProgress())
String.format(
progressString,
Util.boolToMark(currentServerSetting!!.chatSupport),
Util.boolToMark(currentServerSetting!!.bookmarkSupport),
"", ""
)
)
currentServerSetting!!.shareSupport = try { currentServerSetting!!.shareSupport = try {
subsonicApiClient.api.getShares().execute() subsonicApiClient.api.getShares().execute()
true true
} catch (e: IOException) { false } } catch (e: IOException) { false }
updateProgress( updateProgress(getProgress())
String.format(
progressString,
Util.boolToMark(currentServerSetting!!.chatSupport),
Util.boolToMark(currentServerSetting!!.bookmarkSupport),
Util.boolToMark(currentServerSetting!!.shareSupport),
""
)
)
currentServerSetting!!.podcastSupport = try { currentServerSetting!!.podcastSupport = try {
subsonicApiClient.api.getPodcasts().execute() subsonicApiClient.api.getPodcasts().execute()
true true
} catch (e: IOException) { false } } catch (e: IOException) { false }
// Finalize String before displaying it to Dialog updateProgress(getProgress())
progressString = String.format(
progressString,
Util.boolToMark(currentServerSetting!!.chatSupport),
Util.boolToMark(currentServerSetting!!.bookmarkSupport),
Util.boolToMark(currentServerSetting!!.shareSupport),
Util.boolToMark(currentServerSetting!!.podcastSupport)
)
updateProgress(progressString)
val licenseResponse = subsonicApiClient.api.getLicense().execute() val licenseResponse = subsonicApiClient.api.getLicense().execute()
ApiCallResponseChecker.checkResponseSuccessful(licenseResponse) ApiCallResponseChecker.checkResponseSuccessful(licenseResponse)
if (!licenseResponse.body()!!.license.valid) { if (!licenseResponse.body()!!.license.valid) {
progressString += "\n" + return getProgress() + "\n" +
activity.resources.getString(R.string.settings_testing_unlicensed) activity.resources.getString(R.string.settings_testing_unlicensed)
} }
return progressString return getProgress()
} }
override fun done(responseString: String) { override fun done(responseString: String) {