From fd48367cab22fc3e27a578cbe167d78249207bd4 Mon Sep 17 00:00:00 2001 From: Maxence G Date: Sat, 29 May 2021 15:00:46 +0200 Subject: [PATCH] Reduce function length --- .../java/org/moire/ultrasonic/util/Util.java | 14 ---- .../ultrasonic/activity/NavigationActivity.kt | 10 +-- .../org/moire/ultrasonic/data/AppDatabase.kt | 8 +- .../moire/ultrasonic/data/ServerSetting.kt | 8 +- .../ultrasonic/fragment/EditServerFragment.kt | 82 ++++++++----------- 5 files changed, 48 insertions(+), 74 deletions(-) diff --git a/ultrasonic/src/main/java/org/moire/ultrasonic/util/Util.java b/ultrasonic/src/main/java/org/moire/ultrasonic/util/Util.java index fc4fce23..82366ed6 100644 --- a/ultrasonic/src/main/java/org/moire/ultrasonic/util/Util.java +++ b/ultrasonic/src/main/java/org/moire/ultrasonic/util/Util.java @@ -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. diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/activity/NavigationActivity.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/activity/NavigationActivity.kt index 2a1f97c0..f81c99b8 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/activity/NavigationActivity.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/activity/NavigationActivity.kt @@ -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 } } diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/data/AppDatabase.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/data/AppDatabase.kt index a29f895f..e4aa77dc 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/data/AppDatabase.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/data/AppDatabase.kt @@ -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" ) } } diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/data/ServerSetting.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/data/ServerSetting.kt index f1597e87..705f526a 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/data/ServerSetting.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/data/ServerSetting.kt @@ -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 diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/EditServerFragment.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/EditServerFragment.kt index d5767ce1..b2f6bf4a 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/EditServerFragment.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/EditServerFragment.kt @@ -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) {