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 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.

View File

@ -377,10 +377,10 @@ class NavigationActivity : AppCompatActivity() {
podcastsMenuItem?.isVisible = false
return
}
val activeServerProvider = inject(ActiveServerProvider::class.java)
chatMenuItem?.isVisible = activeServerProvider.value.getActiveServer().chatSupport
bookmarksMenuItem?.isVisible = activeServerProvider.value.getActiveServer().bookmarkSupport
sharesMenuItem?.isVisible = activeServerProvider.value.getActiveServer().shareSupport
podcastsMenuItem?.isVisible = activeServerProvider.value.getActiveServer().podcastSupport
val activeServerProvider = inject(ActiveServerProvider::class.java).value.getActiveServer()
chatMenuItem?.isVisible = activeServerProvider.chatSupport != false
bookmarksMenuItem?.isVisible = activeServerProvider.bookmarkSupport != false
sharesMenuItem?.isVisible = activeServerProvider.shareSupport != false
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) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL(
"ALTER TABLE ServerSetting ADD COLUMN chatSupport INTEGER NOT NULL DEFAULT(1)"
"ALTER TABLE ServerSetting ADD COLUMN chatSupport INTEGER"
)
database.execSQL(
"ALTER TABLE ServerSetting ADD COLUMN bookmarkSupport INTEGER NOT NULL DEFAULT(1)"
"ALTER TABLE ServerSetting ADD COLUMN bookmarkSupport INTEGER"
)
database.execSQL(
"ALTER TABLE ServerSetting ADD COLUMN shareSupport INTEGER NOT NULL DEFAULT(1)"
"ALTER TABLE ServerSetting ADD COLUMN shareSupport INTEGER"
)
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 = "musicFolderId") var musicFolderId: String?,
@ColumnInfo(name = "minimumApiVersion") var minimumApiVersion: String?,
@ColumnInfo(name = "chatSupport") var chatSupport: Boolean = true,
@ColumnInfo(name = "bookmarkSupport") var bookmarkSupport: Boolean = true,
@ColumnInfo(name = "shareSupport") var shareSupport: Boolean = true,
@ColumnInfo(name = "podcastSupport") var podcastSupport: Boolean = true
@ColumnInfo(name = "chatSupport") var chatSupport: Boolean? = null,
@ColumnInfo(name = "bookmarkSupport") var bookmarkSupport: Boolean? = null,
@ColumnInfo(name = "shareSupport") var shareSupport: Boolean? = null,
@ColumnInfo(name = "podcastSupport") var podcastSupport: Boolean? = null
) {
constructor() : this (
-1, 0, "", "", "", "", false, false, false, null, null

View File

@ -11,9 +11,6 @@ import androidx.lifecycle.Observer
import androidx.navigation.fragment.findNavController
import com.google.android.material.switchmaterial.SwitchMaterial
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.viewmodel.ext.android.viewModel
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.ModalBackgroundTask
import org.moire.ultrasonic.util.Util
import retrofit2.Call
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
@ -300,18 +301,36 @@ class EditServerFragment : Fragment(), OnBackPressedHandler {
activity,
false
) {
fun boolToMark(value: Boolean?): String {
if (value == null)
return ""
return if (value) "✔️" else ""
}
@Throws(Throwable::class)
override fun doInBackground(): String {
var progressString =
"""
fun getProgress(): String {
return String.format(
"""
|%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_shares)}
|%s - ${activity.resources.getString(R.string.button_bar_podcasts)}
""".trimMargin()
updateProgress(String.format(progressString, "", "", "", ""))
""".trimMargin(),
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(
currentServerSetting!!.url,
@ -340,72 +359,41 @@ class EditServerFragment : Fragment(), OnBackPressedHandler {
pingResponse = subsonicApiClient.api.ping().execute()
ApiCallResponseChecker.checkResponseSuccessful(pingResponse)
updateProgress(String.format(progressString, "", "", "", ""))
currentServerSetting!!.chatSupport = try {
subsonicApiClient.api.getChatMessages().execute()
true
} catch (e: IOException) { false }
updateProgress(
String.format(
progressString,
Util.boolToMark(currentServerSetting!!.chatSupport),
"", "", ""
)
)
updateProgress(getProgress())
currentServerSetting!!.bookmarkSupport = try {
subsonicApiClient.api.getBookmarks().execute()
true
} catch (e: IOException) { false }
updateProgress(
String.format(
progressString,
Util.boolToMark(currentServerSetting!!.chatSupport),
Util.boolToMark(currentServerSetting!!.bookmarkSupport),
"", ""
)
)
updateProgress(getProgress())
currentServerSetting!!.shareSupport = try {
subsonicApiClient.api.getShares().execute()
true
} catch (e: IOException) { false }
updateProgress(
String.format(
progressString,
Util.boolToMark(currentServerSetting!!.chatSupport),
Util.boolToMark(currentServerSetting!!.bookmarkSupport),
Util.boolToMark(currentServerSetting!!.shareSupport),
""
)
)
updateProgress(getProgress())
currentServerSetting!!.podcastSupport = try {
subsonicApiClient.api.getPodcasts().execute()
true
} catch (e: IOException) { false }
// Finalize String before displaying it to Dialog
progressString = String.format(
progressString,
Util.boolToMark(currentServerSetting!!.chatSupport),
Util.boolToMark(currentServerSetting!!.bookmarkSupport),
Util.boolToMark(currentServerSetting!!.shareSupport),
Util.boolToMark(currentServerSetting!!.podcastSupport)
)
updateProgress(progressString)
updateProgress(getProgress())
val licenseResponse = subsonicApiClient.api.getLicense().execute()
ApiCallResponseChecker.checkResponseSuccessful(licenseResponse)
if (!licenseResponse.body()!!.license.valid) {
progressString += "\n" +
return getProgress() + "\n" +
activity.resources.getString(R.string.settings_testing_unlicensed)
}
return progressString
return getProgress()
}
override fun done(responseString: String) {