Skip to content

Commit

Permalink
[optimize] Optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
SkyD666 committed Mar 26, 2024
1 parent ec14b56 commit 732da4d
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 110 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ android {
minSdk = 24
targetSdk = 34
versionCode = 10
versionName = "1.1-alpha05"
versionName = "1.1-alpha06"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"

Expand Down
30 changes: 16 additions & 14 deletions app/src/main/java/com/skyd/anivu/base/BaseFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,24 @@ abstract class BaseFragment<T : ViewBinding> : Fragment() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
transitionProvider()
transitionProvider?.let { provider ->
enterTransition = provider.enterTransition
returnTransition = provider.returnTransition
exitTransition = provider.exitTransition
reenterTransition = provider.reenterTransition
}
}

private val defaultTransitionProvider: () -> Unit = {
enterTransition = MaterialSharedAxis(MaterialSharedAxis.Z, /* forward= */ true)
returnTransition = MaterialSharedAxis(MaterialSharedAxis.Z, /* forward= */ false)
exitTransition = MaterialSharedAxis(MaterialSharedAxis.Z, /* forward= */ true)
reenterTransition = MaterialSharedAxis(MaterialSharedAxis.Z, /* forward= */ false)
}
data class TransitionProvider(
var enterTransition: Any? = MaterialSharedAxis(MaterialSharedAxis.Z, true),
var returnTransition: Any? = MaterialSharedAxis(MaterialSharedAxis.Z, false),
var exitTransition: Any? = MaterialSharedAxis(MaterialSharedAxis.Z, true),
var reenterTransition: Any? = MaterialSharedAxis(MaterialSharedAxis.Z, false),
)

protected val nullTransitionProvider: () -> Unit = {
enterTransition = null
returnTransition = null
exitTransition = null
reenterTransition = null
}
private val defaultTransitionProvider = TransitionProvider()

protected val nullTransitionProvider: TransitionProvider? = null

protected open val transitionProvider: () -> Unit = defaultTransitionProvider
protected open val transitionProvider: TransitionProvider? = defaultTransitionProvider
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import androidx.preference.ListPreferenceDialogFragmentCompat
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import com.google.android.material.color.MaterialColors
import com.google.android.material.transition.MaterialSharedAxis
import com.skyd.anivu.base.BaseFragment
import com.skyd.anivu.ui.component.dialog.InputDialogBuilder

abstract class MaterialPreferenceFragmentCompat : PreferenceFragmentCompat() {
Expand Down Expand Up @@ -54,22 +54,18 @@ abstract class MaterialPreferenceFragmentCompat : PreferenceFragmentCompat() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
transitionProvider()
transitionProvider?.let { provider ->
enterTransition = provider.enterTransition
returnTransition = provider.returnTransition
exitTransition = provider.exitTransition
reenterTransition = provider.reenterTransition
}
}

private val defaultTransitionProvider: () -> Unit = {
enterTransition = MaterialSharedAxis(MaterialSharedAxis.Z, /* forward= */ true)
returnTransition = MaterialSharedAxis(MaterialSharedAxis.Z, /* forward= */ false)
exitTransition = MaterialSharedAxis(MaterialSharedAxis.Z, /* forward= */ true)
reenterTransition = MaterialSharedAxis(MaterialSharedAxis.Z, /* forward= */ false)
}
private val defaultTransitionProvider = BaseFragment.TransitionProvider()

protected val nullTransitionProvider: () -> Unit = {
enterTransition = null
returnTransition = null
exitTransition = null
reenterTransition = null
}
protected val nullTransitionProvider: BaseFragment.TransitionProvider? = null

protected open val transitionProvider: () -> Unit = defaultTransitionProvider
protected open val transitionProvider: BaseFragment.TransitionProvider? =
defaultTransitionProvider
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import kotlinx.coroutines.flow.onEach

@AndroidEntryPoint
class FeedFragment : BaseFragment<FragmentFeedBinding>() {
override val transitionProvider: () -> Unit = nullTransitionProvider
override val transitionProvider = nullTransitionProvider

private val feedViewModel by viewModels<FeedViewModel>()
private val intents = Channel<FeedIntent>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class MediaFragment : BaseFragment<FragmentMediaBinding>() {
const val HAS_PARENT_DIR_KEY = "hasParentDir"
}

override val transitionProvider: () -> Unit = nullTransitionProvider
override val transitionProvider = nullTransitionProvider

private val viewModel by viewModels<MediaViewModel>()
private val path by lazy { arguments?.getString(PATH_KEY) ?: Const.VIDEO_DIR.path }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MoreFragment : BaseFragment<FragmentMoreBinding>() {
override val transitionProvider: () -> Unit = nullTransitionProvider
override val transitionProvider = nullTransitionProvider

private val adapter = VarietyAdapter(mutableListOf())

Expand Down
78 changes: 13 additions & 65 deletions app/src/main/res/navigation/nav_graph.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@
</fragment>
<action
android:id="@+id/action_to_article_fragment"
app:destination="@id/article_fragment"
app:enterAnim="@animator/nav_default_enter_anim"
app:exitAnim="@animator/nav_default_exit_anim"
app:popEnterAnim="@animator/nav_default_pop_enter_anim"
app:popExitAnim="@animator/nav_default_pop_exit_anim" />
app:destination="@id/article_fragment" />

<fragment
android:id="@+id/read_fragment"
Expand All @@ -43,11 +39,7 @@
</fragment>
<action
android:id="@+id/action_to_read_fragment"
app:destination="@id/read_fragment"
app:enterAnim="@animator/nav_default_enter_anim"
app:exitAnim="@animator/nav_default_exit_anim"
app:popEnterAnim="@animator/nav_default_pop_enter_anim"
app:popExitAnim="@animator/nav_default_pop_exit_anim" />
app:destination="@id/read_fragment" />

<fragment
android:id="@+id/download_fragment"
Expand All @@ -56,11 +48,7 @@
tools:layout="@layout/fragment_download" />
<action
android:id="@+id/action_to_download_fragment"
app:destination="@id/download_fragment"
app:enterAnim="@animator/nav_default_enter_anim"
app:exitAnim="@animator/nav_default_exit_anim"
app:popEnterAnim="@animator/nav_default_pop_enter_anim"
app:popExitAnim="@animator/nav_default_pop_exit_anim" />
app:destination="@id/download_fragment" />

<fragment
android:id="@+id/video_fragment"
Expand All @@ -79,11 +67,7 @@
</fragment>
<action
android:id="@+id/action_to_video_fragment"
app:destination="@id/video_fragment"
app:enterAnim="@animator/nav_default_enter_anim"
app:exitAnim="@animator/nav_default_exit_anim"
app:popEnterAnim="@animator/nav_default_pop_enter_anim"
app:popExitAnim="@animator/nav_default_pop_exit_anim" />
app:destination="@id/video_fragment" />

<fragment
android:id="@+id/about_fragment"
Expand All @@ -92,11 +76,7 @@
tools:layout="@layout/fragment_about" />
<action
android:id="@+id/action_to_about_fragment"
app:destination="@id/about_fragment"
app:enterAnim="@animator/nav_default_enter_anim"
app:exitAnim="@animator/nav_default_exit_anim"
app:popEnterAnim="@animator/nav_default_pop_enter_anim"
app:popExitAnim="@animator/nav_default_pop_exit_anim" />
app:destination="@id/about_fragment" />

<fragment
android:id="@+id/license_fragment"
Expand All @@ -105,11 +85,7 @@
tools:layout="@layout/fragment_license" />
<action
android:id="@+id/action_to_license_fragment"
app:destination="@id/license_fragment"
app:enterAnim="@animator/nav_default_enter_anim"
app:exitAnim="@animator/nav_default_exit_anim"
app:popEnterAnim="@animator/nav_default_pop_enter_anim"
app:popExitAnim="@animator/nav_default_pop_exit_anim" />
app:destination="@id/license_fragment" />

<fragment
android:id="@+id/search_fragment"
Expand All @@ -118,11 +94,7 @@
tools:layout="@layout/fragment_search" />
<action
android:id="@+id/action_to_search_fragment"
app:destination="@id/search_fragment"
app:enterAnim="@animator/nav_default_enter_anim"
app:exitAnim="@animator/nav_default_exit_anim"
app:popEnterAnim="@animator/nav_default_pop_enter_anim"
app:popExitAnim="@animator/nav_default_pop_exit_anim" />
app:destination="@id/search_fragment" />

<fragment
android:id="@+id/settings_fragment"
Expand All @@ -131,69 +103,45 @@
tools:layout="@layout/fragment_settings" />
<action
android:id="@+id/action_to_settings_fragment"
app:destination="@id/settings_fragment"
app:enterAnim="@animator/nav_default_enter_anim"
app:exitAnim="@animator/nav_default_exit_anim"
app:popEnterAnim="@animator/nav_default_pop_enter_anim"
app:popExitAnim="@animator/nav_default_pop_exit_anim" />
app:destination="@id/settings_fragment" />

<fragment
android:id="@+id/data_fragment"
android:name="com.skyd.anivu.ui.fragment.settings.data.DataFragment"
android:label="@string/data_fragment_name" />
<action
android:id="@+id/action_to_data_fragment"
app:destination="@id/data_fragment"
app:enterAnim="@animator/nav_default_enter_anim"
app:exitAnim="@animator/nav_default_exit_anim"
app:popEnterAnim="@animator/nav_default_pop_enter_anim"
app:popExitAnim="@animator/nav_default_pop_exit_anim" />
app:destination="@id/data_fragment" />

<fragment
android:id="@+id/rss_config_fragment"
android:name="com.skyd.anivu.ui.fragment.settings.rssconfig.RssConfigFragment"
android:label="@string/rss_config_fragment_name" />
<action
android:id="@+id/action_to_rss_config_fragment"
app:destination="@id/rss_config_fragment"
app:enterAnim="@animator/nav_default_enter_anim"
app:exitAnim="@animator/nav_default_exit_anim"
app:popEnterAnim="@animator/nav_default_pop_enter_anim"
app:popExitAnim="@animator/nav_default_pop_exit_anim" />
app:destination="@id/rss_config_fragment" />

<fragment
android:id="@+id/transmission_fragment"
android:name="com.skyd.anivu.ui.fragment.settings.transmission.TransmissionFragment"
android:label="@string/transmission_fragment_name" />
<action
android:id="@+id/action_to_transmission_fragment"
app:destination="@id/transmission_fragment"
app:enterAnim="@animator/nav_default_enter_anim"
app:exitAnim="@animator/nav_default_exit_anim"
app:popEnterAnim="@animator/nav_default_pop_enter_anim"
app:popExitAnim="@animator/nav_default_pop_exit_anim" />
app:destination="@id/transmission_fragment" />

<fragment
android:id="@+id/proxy_fragment"
android:name="com.skyd.anivu.ui.fragment.settings.transmission.proxy.ProxyFragment"
android:label="@string/proxy_fragment_name" />
<action
android:id="@+id/action_to_proxy_fragment"
app:destination="@id/proxy_fragment"
app:enterAnim="@animator/nav_default_enter_anim"
app:exitAnim="@animator/nav_default_exit_anim"
app:popEnterAnim="@animator/nav_default_pop_enter_anim"
app:popExitAnim="@animator/nav_default_pop_exit_anim" />
app:destination="@id/proxy_fragment" />

<fragment
android:id="@+id/auto_delete_article_fragment"
android:name="com.skyd.anivu.ui.fragment.settings.data.autodelete.AutoDeleteArticleFragment"
android:label="@string/auto_delete_article_fragment_name" />
<action
android:id="@+id/action_to_auto_delete_article_fragment"
app:destination="@id/auto_delete_article_fragment"
app:enterAnim="@animator/nav_default_enter_anim"
app:exitAnim="@animator/nav_default_exit_anim"
app:popEnterAnim="@animator/nav_default_pop_enter_anim"
app:popExitAnim="@animator/nav_default_pop_exit_anim" />
app:destination="@id/auto_delete_article_fragment" />
</navigation>
12 changes: 0 additions & 12 deletions app/src/main/res/navigation/nav_graph_bottom_navigation.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,35 +24,23 @@
<action
android:id="@+id/action_feed_fragment"
app:destination="@id/item_bottom_navigation_feed"
app:enterAnim="@animator/nav_default_enter_anim"
app:exitAnim="@animator/nav_default_exit_anim"
app:launchSingleTop="true"
app:popEnterAnim="@animator/nav_default_pop_enter_anim"
app:popExitAnim="@animator/nav_default_pop_exit_anim"
app:popUpTo="@id/item_bottom_navigation_feed"
app:popUpToInclusive="true"
app:popUpToSaveState="true"
app:restoreState="true" />
<action
android:id="@+id/action_video_fragment"
app:destination="@id/item_bottom_navigation_video"
app:enterAnim="@animator/nav_default_enter_anim"
app:exitAnim="@animator/nav_default_exit_anim"
app:launchSingleTop="true"
app:popEnterAnim="@animator/nav_default_pop_enter_anim"
app:popExitAnim="@animator/nav_default_pop_exit_anim"
app:popUpTo="@id/item_bottom_navigation_video"
app:popUpToInclusive="true"
app:popUpToSaveState="true"
app:restoreState="true" />
<action
android:id="@+id/action_more_fragment"
app:destination="@id/item_bottom_navigation_more"
app:enterAnim="@animator/nav_default_enter_anim"
app:exitAnim="@animator/nav_default_exit_anim"
app:launchSingleTop="true"
app:popEnterAnim="@animator/nav_default_pop_enter_anim"
app:popExitAnim="@animator/nav_default_pop_exit_anim"
app:popUpTo="@id/item_bottom_navigation_more"
app:popUpToInclusive="true"
app:popUpToSaveState="true"
Expand Down

0 comments on commit 732da4d

Please sign in to comment.