mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-15 04:07:03 +05:30
refactor(android): extract shared activity logic to BaseActivity
This commit is contained in:
parent
9a300aaf21
commit
940ff167bf
4 changed files with 102 additions and 127 deletions
|
@ -0,0 +1,76 @@
|
||||||
|
/*
|
||||||
|
* Copyright © 2021-2024 Harsh Shandilya.
|
||||||
|
* Use of this source code is governed by an MIT-style
|
||||||
|
* license that can be found in the LICENSE file or at
|
||||||
|
* https://opensource.org/licenses/MIT.
|
||||||
|
*/
|
||||||
|
package dev.msfjarvis.claw.android
|
||||||
|
|
||||||
|
import android.app.assist.AssistContent
|
||||||
|
import android.graphics.Color
|
||||||
|
import android.net.Uri
|
||||||
|
import android.os.Bundle
|
||||||
|
import androidx.activity.ComponentActivity
|
||||||
|
import androidx.activity.SystemBarStyle
|
||||||
|
import androidx.activity.compose.setContent
|
||||||
|
import androidx.activity.enableEdgeToEdge
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.ui.platform.LocalUriHandler
|
||||||
|
import com.deliveryhero.whetstone.Whetstone
|
||||||
|
import dev.msfjarvis.claw.android.viewmodel.ClawViewModel
|
||||||
|
import dev.msfjarvis.claw.common.comments.HTMLConverter
|
||||||
|
import dev.msfjarvis.claw.common.theme.LobstersTheme
|
||||||
|
import dev.msfjarvis.claw.common.urllauncher.UrlLauncher
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
/** A base class that encapsulates all activities used by Claw. */
|
||||||
|
abstract class BaseActivity : ComponentActivity() {
|
||||||
|
|
||||||
|
@Inject lateinit var urlLauncher: UrlLauncher
|
||||||
|
@Inject lateinit var htmlConverter: HTMLConverter
|
||||||
|
@Inject lateinit var viewModel: ClawViewModel
|
||||||
|
var webUri: String? = null
|
||||||
|
|
||||||
|
/** Entrypoint to show a [Composable] as this [ComponentActivity]'s view. */
|
||||||
|
@Composable abstract fun Content()
|
||||||
|
|
||||||
|
/** Overrideable method to call stuff in [onCreate]. */
|
||||||
|
open fun preLaunch() {}
|
||||||
|
|
||||||
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
|
super.onCreate(savedInstanceState)
|
||||||
|
preLaunch()
|
||||||
|
Whetstone.inject(this)
|
||||||
|
enableEdgeToEdge(
|
||||||
|
statusBarStyle =
|
||||||
|
SystemBarStyle.light(
|
||||||
|
Color.TRANSPARENT,
|
||||||
|
Color.TRANSPARENT,
|
||||||
|
),
|
||||||
|
navigationBarStyle =
|
||||||
|
SystemBarStyle.light(
|
||||||
|
Color.TRANSPARENT,
|
||||||
|
Color.TRANSPARENT,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
setContent {
|
||||||
|
LobstersTheme(
|
||||||
|
dynamicColor = true,
|
||||||
|
providedValues = arrayOf(LocalUriHandler provides urlLauncher),
|
||||||
|
) {
|
||||||
|
Content()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onProvideAssistContent(outContent: AssistContent?) {
|
||||||
|
super.onProvideAssistContent(outContent)
|
||||||
|
if (outContent != null) {
|
||||||
|
if (webUri != null) {
|
||||||
|
outContent.webUri = Uri.parse(webUri)
|
||||||
|
} else {
|
||||||
|
outContent.webUri = null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,80 +1,36 @@
|
||||||
/*
|
/*
|
||||||
* Copyright © 2021-2023 Harsh Shandilya.
|
* Copyright © 2021-2024 Harsh Shandilya.
|
||||||
* Use of this source code is governed by an MIT-style
|
* Use of this source code is governed by an MIT-style
|
||||||
* license that can be found in the LICENSE file or at
|
* license that can be found in the LICENSE file or at
|
||||||
* https://opensource.org/licenses/MIT.
|
* https://opensource.org/licenses/MIT.
|
||||||
*/
|
*/
|
||||||
package dev.msfjarvis.claw.android
|
package dev.msfjarvis.claw.android
|
||||||
|
|
||||||
import android.app.assist.AssistContent
|
|
||||||
import android.graphics.Color
|
|
||||||
import android.net.Uri
|
|
||||||
import android.os.Bundle
|
|
||||||
import androidx.activity.ComponentActivity
|
|
||||||
import androidx.activity.SystemBarStyle
|
|
||||||
import androidx.activity.compose.setContent
|
|
||||||
import androidx.activity.enableEdgeToEdge
|
|
||||||
import androidx.compose.material3.windowsizeclass.ExperimentalMaterial3WindowSizeClassApi
|
import androidx.compose.material3.windowsizeclass.ExperimentalMaterial3WindowSizeClassApi
|
||||||
import androidx.compose.material3.windowsizeclass.calculateWindowSizeClass
|
import androidx.compose.material3.windowsizeclass.calculateWindowSizeClass
|
||||||
import androidx.compose.ui.platform.LocalUriHandler
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
|
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
|
||||||
import com.deliveryhero.whetstone.Whetstone
|
|
||||||
import com.deliveryhero.whetstone.activity.ContributesActivityInjector
|
import com.deliveryhero.whetstone.activity.ContributesActivityInjector
|
||||||
import dev.msfjarvis.claw.android.ui.screens.LobstersPostsScreen
|
import dev.msfjarvis.claw.android.ui.screens.LobstersPostsScreen
|
||||||
import dev.msfjarvis.claw.common.comments.HTMLConverter
|
|
||||||
import dev.msfjarvis.claw.common.theme.LobstersTheme
|
|
||||||
import dev.msfjarvis.claw.common.urllauncher.UrlLauncher
|
|
||||||
import javax.inject.Inject
|
|
||||||
|
|
||||||
@ContributesActivityInjector
|
@ContributesActivityInjector
|
||||||
class MainActivity : ComponentActivity() {
|
class MainActivity : BaseActivity() {
|
||||||
|
|
||||||
@Inject lateinit var urlLauncher: UrlLauncher
|
|
||||||
@Inject lateinit var htmlConverter: HTMLConverter
|
|
||||||
private var webUri: String? = null
|
|
||||||
|
|
||||||
@OptIn(ExperimentalMaterial3WindowSizeClassApi::class)
|
@OptIn(ExperimentalMaterial3WindowSizeClassApi::class)
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
@Composable
|
||||||
super.onCreate(savedInstanceState)
|
override fun Content() {
|
||||||
installSplashScreen()
|
val windowSizeClass = calculateWindowSizeClass(this)
|
||||||
Whetstone.inject(this)
|
LobstersPostsScreen(
|
||||||
enableEdgeToEdge(
|
urlLauncher = urlLauncher,
|
||||||
statusBarStyle =
|
htmlConverter = htmlConverter,
|
||||||
SystemBarStyle.light(
|
windowSizeClass = windowSizeClass,
|
||||||
Color.TRANSPARENT,
|
setWebUri = { url -> webUri = url },
|
||||||
Color.TRANSPARENT,
|
|
||||||
),
|
|
||||||
navigationBarStyle =
|
|
||||||
SystemBarStyle.light(
|
|
||||||
Color.TRANSPARENT,
|
|
||||||
Color.TRANSPARENT,
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
setContent {
|
|
||||||
val windowSizeClass = calculateWindowSizeClass(this)
|
|
||||||
LobstersTheme(
|
|
||||||
dynamicColor = true,
|
|
||||||
providedValues = arrayOf(LocalUriHandler provides urlLauncher),
|
|
||||||
) {
|
|
||||||
LobstersPostsScreen(
|
|
||||||
urlLauncher = urlLauncher,
|
|
||||||
htmlConverter = htmlConverter,
|
|
||||||
windowSizeClass = windowSizeClass,
|
|
||||||
setWebUri = { url -> webUri = url },
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onProvideAssistContent(outContent: AssistContent?) {
|
override fun preLaunch() {
|
||||||
super.onProvideAssistContent(outContent)
|
super.preLaunch()
|
||||||
if (outContent != null) {
|
installSplashScreen()
|
||||||
if (webUri != null) {
|
|
||||||
outContent.webUri = Uri.parse(webUri)
|
|
||||||
} else {
|
|
||||||
outContent.webUri = null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|
|
@ -1,75 +1,24 @@
|
||||||
/*
|
/*
|
||||||
* Copyright © 2021-2023 Harsh Shandilya.
|
* Copyright © 2021-2024 Harsh Shandilya.
|
||||||
* Use of this source code is governed by an MIT-style
|
* Use of this source code is governed by an MIT-style
|
||||||
* license that can be found in the LICENSE file or at
|
* license that can be found in the LICENSE file or at
|
||||||
* https://opensource.org/licenses/MIT.
|
* https://opensource.org/licenses/MIT.
|
||||||
*/
|
*/
|
||||||
package dev.msfjarvis.claw.android
|
package dev.msfjarvis.claw.android
|
||||||
|
|
||||||
import android.app.assist.AssistContent
|
import androidx.compose.runtime.Composable
|
||||||
import android.graphics.Color
|
|
||||||
import android.net.Uri
|
|
||||||
import android.os.Bundle
|
|
||||||
import androidx.activity.ComponentActivity
|
|
||||||
import androidx.activity.SystemBarStyle
|
|
||||||
import androidx.activity.compose.setContent
|
|
||||||
import androidx.activity.enableEdgeToEdge
|
|
||||||
import androidx.compose.ui.platform.LocalUriHandler
|
|
||||||
import com.deliveryhero.whetstone.Whetstone
|
|
||||||
import com.deliveryhero.whetstone.activity.ContributesActivityInjector
|
import com.deliveryhero.whetstone.activity.ContributesActivityInjector
|
||||||
import dev.msfjarvis.claw.android.ui.screens.SearchScreen
|
import dev.msfjarvis.claw.android.ui.screens.SearchScreen
|
||||||
import dev.msfjarvis.claw.android.viewmodel.ClawViewModel
|
|
||||||
import dev.msfjarvis.claw.common.comments.HTMLConverter
|
|
||||||
import dev.msfjarvis.claw.common.theme.LobstersTheme
|
|
||||||
import dev.msfjarvis.claw.common.urllauncher.UrlLauncher
|
|
||||||
import javax.inject.Inject
|
|
||||||
|
|
||||||
@ContributesActivityInjector
|
@ContributesActivityInjector
|
||||||
class SearchActivity : ComponentActivity() {
|
class SearchActivity : BaseActivity() {
|
||||||
|
@Composable
|
||||||
@Inject lateinit var urlLauncher: UrlLauncher
|
override fun Content() {
|
||||||
@Inject lateinit var htmlConverter: HTMLConverter
|
SearchScreen(
|
||||||
@Inject lateinit var viewModel: ClawViewModel
|
urlLauncher = urlLauncher,
|
||||||
private var webUri: String? = null
|
htmlConverter = htmlConverter,
|
||||||
|
setWebUri = { webUri = it },
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
viewModel = viewModel,
|
||||||
super.onCreate(savedInstanceState)
|
|
||||||
Whetstone.inject(this)
|
|
||||||
enableEdgeToEdge(
|
|
||||||
statusBarStyle =
|
|
||||||
SystemBarStyle.light(
|
|
||||||
Color.TRANSPARENT,
|
|
||||||
Color.TRANSPARENT,
|
|
||||||
),
|
|
||||||
navigationBarStyle =
|
|
||||||
SystemBarStyle.light(
|
|
||||||
Color.TRANSPARENT,
|
|
||||||
Color.TRANSPARENT,
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
setContent {
|
|
||||||
LobstersTheme(
|
|
||||||
dynamicColor = true,
|
|
||||||
providedValues = arrayOf(LocalUriHandler provides urlLauncher),
|
|
||||||
) {
|
|
||||||
SearchScreen(
|
|
||||||
urlLauncher = urlLauncher,
|
|
||||||
htmlConverter = htmlConverter,
|
|
||||||
setWebUri = { webUri = it },
|
|
||||||
viewModel = viewModel,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onProvideAssistContent(outContent: AssistContent?) {
|
|
||||||
super.onProvideAssistContent(outContent)
|
|
||||||
if (outContent != null) {
|
|
||||||
if (webUri != null) {
|
|
||||||
outContent.webUri = Uri.parse(webUri)
|
|
||||||
} else {
|
|
||||||
outContent.webUri = null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
<!--
|
<!--
|
||||||
~ Copyright © 2021-2023 Harsh Shandilya.
|
~ Copyright © 2021-2024 Harsh Shandilya.
|
||||||
~ Use of this source code is governed by an MIT-style
|
~ Use of this source code is governed by an MIT-style
|
||||||
~ license that can be found in the LICENSE file or at
|
~ license that can be found in the LICENSE file or at
|
||||||
~ https://opensource.org/licenses/MIT.
|
~ https://opensource.org/licenses/MIT.
|
||||||
|
@ -13,11 +13,5 @@
|
||||||
<item name="postSplashScreenTheme">@style/Theme.Claw</item>
|
<item name="postSplashScreenTheme">@style/Theme.Claw</item>
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
<style name="Platform.Theme.Claw" parent="android:Theme.Material.NoActionBar" />
|
<style name="Theme.Claw" parent="android:Theme.Material.NoActionBar" />
|
||||||
|
|
||||||
<style name="Base.Theme.Claw" parent="Platform.Theme.Claw">
|
|
||||||
<item name="android:windowActionModeOverlay">true</item>
|
|
||||||
</style>
|
|
||||||
|
|
||||||
<style name="Theme.Claw" parent="Base.Theme.Claw" />
|
|
||||||
</resources>
|
</resources>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue