Moved server selector to the navigation menu

This commit is contained in:
Nite 2021-10-10 14:03:53 +02:00
parent ed59136fa6
commit 4b4853374c
No known key found for this signature in database
GPG Key ID: 1D1AD59B1C6386C1
15 changed files with 99 additions and 150 deletions

View File

@ -55,7 +55,6 @@ import static org.moire.ultrasonic.fragment.ServerSelectorFragment.SERVER_SELECT
public class SettingsFragment extends PreferenceFragmentCompat
implements SharedPreferences.OnSharedPreferenceChangeListener {
private Preference addServerPreference;
private ListPreference theme;
private ListPreference maxBitrateWifi;
private ListPreference maxBitrateMobile;
@ -110,7 +109,6 @@ public class SettingsFragment extends PreferenceFragmentCompat
super.onViewCreated(view, savedInstanceState);
FragmentTitle.Companion.setTitle(this, R.string.menu_settings);
addServerPreference = findPreference(Constants.PREFERENCES_KEY_SERVERS_EDIT);
theme = findPreference(Constants.PREFERENCES_KEY_THEME);
maxBitrateWifi = findPreference(Constants.PREFERENCES_KEY_MAX_BITRATE_WIFI);
maxBitrateMobile = findPreference(Constants.PREFERENCES_KEY_MAX_BITRATE_MOBILE);
@ -141,7 +139,6 @@ public class SettingsFragment extends PreferenceFragmentCompat
debugLogToFile = findPreference(Constants.PREFERENCES_KEY_DEBUG_LOG_TO_FILE);
showArtistPicture = findPreference(Constants.PREFERENCES_KEY_SHOW_ARTIST_PICTURE);
setupServersCategory();
sharingDefaultGreeting.setText(Settings.getShareGreeting());
setupClearSearchPreference();
setupGaplessControlSettingsV14();
@ -394,22 +391,6 @@ public class SettingsFragment extends PreferenceFragmentCompat
}
}
private void setupServersCategory() {
addServerPreference.setPersistent(false);
addServerPreference.setTitle(getResources().getString(R.string.settings_server_manage_servers));
addServerPreference.setEnabled(true);
addServerPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
Bundle bundle = new Bundle();
bundle.putBoolean(SERVER_SELECTOR_MANAGE_MODE, true);
Navigation.findNavController(getView()).navigate(R.id.settingsToServerSelector, bundle);
return true;
}
});
}
private void update() {
theme.setSummary(theme.getEntry());
maxBitrateWifi.setSummary(maxBitrateWifi.getEntry());

View File

@ -12,12 +12,14 @@ import android.view.KeyEvent
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.core.view.GravityCompat
import androidx.drawerlayout.widget.DrawerLayout
import androidx.fragment.app.FragmentContainerView
import androidx.navigation.NavController
import androidx.navigation.Navigation
import androidx.navigation.findNavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.AppBarConfiguration
@ -31,7 +33,7 @@ import org.koin.android.ext.android.inject
import org.koin.androidx.viewmodel.ext.android.viewModel
import org.moire.ultrasonic.R
import org.moire.ultrasonic.data.ActiveServerProvider
import org.moire.ultrasonic.data.ActiveServerProvider.Companion.isOffline
import org.moire.ultrasonic.data.ServerSettingDao
import org.moire.ultrasonic.domain.PlayerState
import org.moire.ultrasonic.fragment.OnBackPressedHandler
import org.moire.ultrasonic.fragment.ServerSettingsModel
@ -65,6 +67,7 @@ class NavigationActivity : AppCompatActivity() {
private var navigationView: NavigationView? = null
private var drawerLayout: DrawerLayout? = null
private var host: NavHostFragment? = null
private var selectServerButton: Button? = null
private lateinit var appBarConfiguration: AppBarConfiguration
private lateinit var nowPlayingEventListener: NowPlayingEventListener
@ -77,9 +80,12 @@ class NavigationActivity : AppCompatActivity() {
private val nowPlayingEventDistributor: NowPlayingEventDistributor by inject()
private val themeChangedEventDistributor: ThemeChangedEventDistributor by inject()
private val permissionUtil: PermissionUtil by inject()
private val activeServerProvider: ActiveServerProvider by inject()
private val serverRepository: ServerSettingDao by inject()
private var infoDialogDisplayed = false
private var currentFragmentId: Int = 0
private var cachedServerCount: Int = 0
override fun onCreate(savedInstanceState: Bundle?) {
setUncaughtExceptionHandler()
@ -140,7 +146,7 @@ class NavigationActivity : AppCompatActivity() {
}
// Hides menu items for Offline mode
setMenuForServerSetting()
setMenuForServerCapabilities()
}
// Determine first run and migrate server settings to DB as early as possible
@ -179,12 +185,25 @@ class NavigationActivity : AppCompatActivity() {
nowPlayingEventDistributor.subscribe(nowPlayingEventListener)
themeChangedEventDistributor.subscribe(themeChangedEventListener)
serverRepository.liveServerCount().observe(this, { count ->
cachedServerCount = count ?: 0
setSelectServerButtonText()
})
ActiveServerProvider.liveActiveServerId.observe(this, { setSelectServerButtonText() })
}
private fun setSelectServerButtonText() {
val activeServerName = activeServerProvider.getActiveServer().name
if (cachedServerCount == 0)
selectServerButton?.text = getString(R.string.main_setup_server, activeServerName)
else selectServerButton?.text = activeServerName
}
override fun onResume() {
super.onResume()
setMenuForServerSetting()
setMenuForServerCapabilities()
// Lifecycle support's constructor registers some event receivers so it should be created early
lifecycleSupport.onCreate()
@ -232,6 +251,12 @@ class NavigationActivity : AppCompatActivity() {
bookmarksMenuItem = navigationView?.menu?.findItem(R.id.bookmarksFragment)
sharesMenuItem = navigationView?.menu?.findItem(R.id.sharesFragment)
podcastsMenuItem = navigationView?.menu?.findItem(R.id.podcastFragment)
selectServerButton = navigationView?.getHeaderView(0)?.findViewById(R.id.header_select_server)
selectServerButton?.setOnClickListener {
if (drawerLayout?.isDrawerVisible(GravityCompat.START) == true)
this.drawerLayout?.closeDrawer(GravityCompat.START)
navController.navigate(R.id.serverSelectorFragment)
}
}
private fun setupActionBar(navController: NavController, appBarConfig: AppBarConfiguration) {
@ -381,15 +406,14 @@ class NavigationActivity : AppCompatActivity() {
nowPlayingView?.visibility = View.GONE
}
private fun setMenuForServerSetting() {
if (isOffline()) {
private fun setMenuForServerCapabilities() {
if (ActiveServerProvider.isOffline()) {
chatMenuItem?.isVisible = false
bookmarksMenuItem?.isVisible = false
sharesMenuItem?.isVisible = false
podcastsMenuItem?.isVisible = false
return
}
val activeServerProvider: ActiveServerProvider by inject()
val activeServer = activeServerProvider.getActiveServer()
chatMenuItem?.isVisible = activeServer.chatSupport != false
bookmarksMenuItem?.isVisible = activeServer.bookmarkSupport != false

View File

@ -1,5 +1,7 @@
package org.moire.ultrasonic.data
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.room.Room
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
@ -178,6 +180,7 @@ class ActiveServerProvider(
companion object {
const val METADATA_DB = "$DB_FILENAME-meta-"
val liveActiveServerId: MutableLiveData<Int> = MutableLiveData(getActiveServerId())
/**
* Queries if the Active Server is the "Offline" mode of Ultrasonic
@ -205,6 +208,8 @@ class ActiveServerProvider(
val editor = preferences.edit()
editor.putInt(Constants.PREFERENCES_KEY_SERVER_INSTANCE, serverId)
editor.apply()
liveActiveServerId.postValue(serverId)
}
/**

View File

@ -63,6 +63,12 @@ interface ServerSettingDao {
@Query("SELECT COUNT(*) FROM serverSetting")
suspend fun count(): Int?
/**
* Retrieves the count of rows in the table as a LiveData
*/
@Query("SELECT COUNT(*) FROM serverSetting")
fun liveServerCount(): LiveData<Int?>
/**
* Retrieves the greatest value of the Id column in the table
*/

View File

@ -7,14 +7,10 @@ import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.AdapterView.OnItemClickListener
import android.widget.ListView
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.navigation.Navigation
import java.util.Locale
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import org.moire.ultrasonic.R
import org.moire.ultrasonic.data.ActiveServerProvider
import org.moire.ultrasonic.data.ActiveServerProvider.Companion.isOffline
import org.moire.ultrasonic.util.Constants
import org.moire.ultrasonic.util.MergeAdapter
@ -27,8 +23,6 @@ import org.moire.ultrasonic.util.Util
class MainFragment : Fragment(), KoinComponent {
private var list: ListView? = null
private lateinit var serverButton: View
private lateinit var serverTextView: TextView
private lateinit var musicTitle: View
private lateinit var artistsButton: View
private lateinit var albumsButton: View
@ -48,8 +42,6 @@ class MainFragment : Fragment(), KoinComponent {
private lateinit var albumsAlphaByArtistButton: View
private lateinit var videosButton: View
private val activeServerProvider: ActiveServerProvider by inject()
override fun onCreate(savedInstanceState: Bundle?) {
Util.applyTheme(this.context)
super.onCreate(savedInstanceState)
@ -65,7 +57,6 @@ class MainFragment : Fragment(), KoinComponent {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
list = view.findViewById(R.id.main_list)
cachedActiveServerProperties = getActiveServerProperties()
setupButtons()
@ -78,7 +69,6 @@ class MainFragment : Fragment(), KoinComponent {
super.onResume()
var shouldRestart = false
val currentId3Setting = Settings.shouldUseId3Tags
val currentActiveServerProperties = getActiveServerProperties()
// If setting has changed...
if (currentId3Setting != shouldUseId3) {
@ -86,12 +76,6 @@ class MainFragment : Fragment(), KoinComponent {
shouldRestart = true
}
// or the server has changed...
if (currentActiveServerProperties != cachedActiveServerProperties) {
cachedActiveServerProperties = currentActiveServerProperties
shouldRestart = true
}
// then setup the list anew.
if (shouldRestart) {
if (list != null) setupMenuList(list!!)
@ -100,8 +84,6 @@ class MainFragment : Fragment(), KoinComponent {
private fun setupButtons() {
val buttons = layoutInflater.inflate(R.layout.main_buttons, list, false)
serverButton = buttons.findViewById(R.id.main_select_server)
serverTextView = serverButton.findViewById(R.id.main_select_server_2)
musicTitle = buttons.findViewById(R.id.main_music)
artistsButton = buttons.findViewById(R.id.main_artists_button)
albumsButton = buttons.findViewById(R.id.main_albums_button)
@ -123,15 +105,10 @@ class MainFragment : Fragment(), KoinComponent {
}
private fun setupMenuList(list: ListView) {
// Set title
val activeServerName = activeServerProvider.getActiveServer().name
serverTextView.text = activeServerName
// TODO: Should use RecyclerView
val adapter = MergeAdapter()
adapter.addView(serverButton, true)
shouldUseId3 = Settings.shouldUseId3Tags
if (!isOffline()) {
@ -177,9 +154,6 @@ class MainFragment : Fragment(), KoinComponent {
private val listListener =
OnItemClickListener { _: AdapterView<*>?, view: View, _: Int, _: Long ->
when {
view === serverButton -> {
showServers()
}
view === albumsNewestButton -> {
showAlbumList("newest", R.string.main_albums_newest)
}
@ -225,20 +199,6 @@ class MainFragment : Fragment(), KoinComponent {
}
}
private fun getActiveServerProperties(): String {
val server = activeServerProvider.getActiveServer()
return String.format(
Locale.ROOT,
"%s;%s;%s;%s;%s;%s",
server.url,
server.userName,
server.password,
server.allowSelfSignedCertificate,
server.ldapSupport,
server.minimumApiVersion
)
}
private fun showStarredSongs() {
val bundle = Bundle()
bundle.putInt(Constants.INTENT_EXTRA_NAME_STARRED, 1)
@ -281,12 +241,7 @@ class MainFragment : Fragment(), KoinComponent {
Navigation.findNavController(requireView()).navigate(R.id.mainToTrackCollection, bundle)
}
private fun showServers() {
Navigation.findNavController(requireView()).navigate(R.id.mainToServerSelector)
}
companion object {
private var shouldUseId3 = false
private var cachedActiveServerProperties: String? = null
}
}

View File

@ -54,7 +54,6 @@ object Constants {
// Preferences keys.
const val PREFERENCES_KEY_SERVER_INSTANCE = "serverInstanceId"
const val PREFERENCES_KEY_SERVERS_EDIT = "editServers"
const val PREFERENCES_KEY_THEME = "theme"
const val PREFERENCES_KEY_THEME_LIGHT = "light"
const val PREFERENCES_KEY_THEME_DARK = "dark"

View File

@ -2,38 +2,6 @@
<LinearLayout xmlns:a="http://schemas.android.com/apk/res/android"
a:orientation="vertical" a:layout_width="fill_parent" a:layout_height="fill_parent">
<LinearLayout a:id="@+id/main_select_server"
a:orientation="horizontal" a:paddingTop="2dip" a:paddingBottom="2dip"
a:paddingLeft="10dp" a:layout_width="fill_parent" a:layout_height="wrap_content"
a:minHeight="?android:attr/listPreferredItemHeight">
<ImageView a:layout_width="wrap_content" a:layout_height="wrap_content"
a:layout_gravity="center_vertical" a:src="?attr/select_server" />
<LinearLayout
a:layout_width="wrap_content"
a:layout_height="wrap_content"
a:orientation="vertical">
<TextView
a:id="@+id/main.select_server_1"
a:layout_width="fill_parent"
a:layout_height="wrap_content"
a:layout_marginLeft="10dip"
a:layout_marginTop="6dip"
a:text="@string/main.select_server"
a:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
a:id="@+id/main.select_server_2"
a:layout_width="fill_parent"
a:layout_height="wrap_content"
a:layout_marginLeft="10dip"
a:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
</LinearLayout>
<TextView a:id="@+id/main_music" a:text="@string/main.music"
a:layout_width="fill_parent" a:layout_height="wrap_content"
a:textAppearance="?android:attr/textAppearanceSmall" a:textColor="@color/cyan"

View File

@ -1,8 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:a="http://schemas.android.com/apk/res/android"
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:a="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
a:id="@+id/view_container"
a:layout_width="match_parent"
a:layout_height="96dp"
a:layout_height="wrap_content"
a:gravity="bottom"
a:orientation="vertical"
a:theme="@style/ThemeOverlay.AppCompat.Dark">
@ -10,32 +13,53 @@
<ImageView
a:id="@+id/img_header_bg"
a:layout_width="match_parent"
a:layout_height="match_parent"
a:layout_height="0dp"
a:scaleType="fitXY"
a:src="@drawable/ic_launcher_background" />
a:src="@drawable/ic_launcher_background"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
<ImageView
a:id="@+id/img_profile"
a:layout_width="52dp"
a:layout_height="52dp"
a:layout_marginStart="16dp"
a:layout_marginLeft="16dp"
a:layout_marginTop="32dp"
a:src="@drawable/ic_stat_ultrasonic"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
a:id="@+id/name"
a:layout_width="wrap_content"
a:layout_height="wrap_content"
a:layout_centerVertical="true"
a:orientation="horizontal"
a:padding="16dp">
a:gravity="center_vertical"
a:paddingStart="16dp"
a:paddingLeft="16dp"
a:text="@string/common.appname"
a:textAppearance="@style/TextAppearance.AppCompat.Title"
app:layout_constraintBottom_toBottomOf="@+id/img_profile"
app:layout_constraintStart_toEndOf="@+id/img_profile"
app:layout_constraintTop_toTopOf="@+id/img_profile" />
<ImageView
a:id="@+id/img_profile"
a:layout_width="52dp"
a:layout_height="52dp"
a:src="@drawable/ic_stat_ultrasonic" />
<Button
a:id="@+id/header_select_server"
style="@style/Widget.MaterialComponents.Button.TextButton.Icon"
a:layout_width="wrap_content"
a:layout_height="wrap_content"
a:layout_marginStart="6dp"
a:layout_marginLeft="6dp"
a:textAppearance="@style/MenuDrawer.Widget"
a:textSize="14sp"
app:icon="@drawable/ic_menu_select_server_dark"
app:iconPadding="16dp"
app:iconTint="@color/appwidget_text"
a:padding="16dp"
a:text="@string/main.offline"
a:textColor="@color/appwidget_text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="@+id/img_header_bg"
app:layout_constraintTop_toBottomOf="@+id/img_profile" />
<TextView
a:id="@+id/name"
a:layout_width="wrap_content"
a:layout_height="match_parent"
a:gravity="center_vertical"
a:paddingStart="16dp"
a:paddingLeft="16dp"
a:text="@string/common.appname"
a:textAppearance="@style/TextAppearance.AppCompat.Title" />
</LinearLayout>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -4,8 +4,7 @@
a:layout_width="fill_parent"
a:layout_height="wrap_content"
a:minHeight="?android:attr/listPreferredItemHeight"
a:orientation="horizontal"
a:background="?attr/color_menu_background">
a:orientation="horizontal">
<ImageView
a:id="@+id/song_drag"

View File

@ -19,9 +19,6 @@
<action
android:id="@+id/mainToSelectGenre"
app:destination="@id/selectGenreFragment" />
<action
android:id="@+id/mainToServerSelector"
app:destination="@id/serverSelectorFragment" />
</fragment>
<fragment
android:id="@+id/mediaLibraryFragment"
@ -83,9 +80,6 @@
<fragment
android:id="@+id/settingsFragment"
android:name="org.moire.ultrasonic.fragment.SettingsFragment" >
<action
android:id="@+id/settingsToServerSelector"
app:destination="@id/serverSelectorFragment" />
</fragment>
<fragment
android:id="@+id/aboutFragment"

View File

@ -8,7 +8,8 @@
<color name="md__transparent">#00000000</color>
<color name="translucent">#80000000</color>
<color name="background_color_dark">#ff000000</color>
<color name="background_color_light">#fff3f3f3</color>
<color name="background_color_grey">#ff333333</color>
<color name="background_color_light">#ffffffff</color>
<color name="selected_color_dark">#424242</color>
<color name="selected_color_light">#B1B1B1</color>
<color name="selected_menu_dark">#fff3f3f3</color>

View File

@ -106,6 +106,7 @@
<string name="main.music">Music</string>
<string name="main.offline">Offline</string>
<string name="main.select_server">Select Server</string>
<string name="main.setup_server">%s - Setup Server</string>
<string name="main.shuffle">Shuffle Play</string>
<string name="main.songs_random">Random</string>
<string name="main.songs_starred">Starred</string>

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="NoActionBar" parent="@style/Theme.AppCompat">
<style name="NoActionBar" parent="@style/Theme.MaterialComponents">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>

View File

@ -63,10 +63,11 @@
<item name="list_selector_holo_selected">@drawable/list_selector_holo_dark_selected</item>
</style>
<style name="UltrasonicTheme" parent="Theme.AppCompat">
<style name="UltrasonicTheme" parent="Theme.MaterialComponents">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="color_background">@color/background_color_dark</item>
<item name="color_background">@color/background_color_grey</item>
<item name="android:colorBackground">@color/background_color_grey</item>
<item name="color_selected">@color/selected_color_dark</item>
<item name="color_menu_selected">@color/selected_menu_dark</item>
<item name="color_menu_background">@color/selected_menu_background_dark</item>
@ -126,7 +127,7 @@
<item name="list_selector_holo_selected">@drawable/list_selector_holo_dark_selected</item>
</style>
<style name="UltrasonicTheme.Light" parent="Theme.AppCompat.Light">
<style name="UltrasonicTheme.Light" parent="Theme.MaterialComponents.Light">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="color_background">@color/background_color_light</item>

View File

@ -2,15 +2,6 @@
<PreferenceScreen xmlns:a="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" a:title="@string/common.appname">
<PreferenceCategory
a:key="serversKey"
a:title="@string/settings.servers_title"
app:iconSpaceReserved="false">
<Preference
a:key="editServers"
a:title="@string/settings.server_manage_servers"
app:iconSpaceReserved="false"/>
</PreferenceCategory>
<PreferenceCategory
a:title="@string/settings.appearance_title"
app:iconSpaceReserved="false">