|
| 1 | +package com.tldw.app |
| 2 | + |
| 3 | +import android.content.Intent |
| 4 | +import android.os.Bundle |
| 5 | +import androidx.activity.ComponentActivity |
| 6 | +import androidx.activity.compose.setContent |
| 7 | +import androidx.activity.enableEdgeToEdge |
| 8 | +import androidx.activity.viewModels |
| 9 | +import androidx.compose.foundation.layout.Box |
| 10 | +import androidx.compose.foundation.layout.fillMaxSize |
| 11 | +import androidx.compose.material3.CircularProgressIndicator |
| 12 | +import androidx.compose.material3.Surface |
| 13 | +import androidx.compose.runtime.getValue |
| 14 | +import androidx.compose.ui.Alignment |
| 15 | +import androidx.compose.ui.Modifier |
| 16 | +import androidx.lifecycle.compose.collectAsStateWithLifecycle |
| 17 | +import com.tldw.app.data.repository.ModelRepositoryImpl |
| 18 | +import com.tldw.app.domain.usecase.CheckModelDownloadedUseCase |
| 19 | +import com.tldw.app.ui.modelscreen.ModelDownloadScreenRoute |
| 20 | +import com.tldw.app.ui.theme.TldwTheme |
| 21 | +import com.tldw.app.ui.transcriptscreen.TranscriptScreenRoute |
| 22 | + |
| 23 | +class MainActivity : ComponentActivity() { |
| 24 | + |
| 25 | + val mainViewModel: MainViewModel by viewModels { |
| 26 | + val repository = ModelRepositoryImpl(applicationContext) |
| 27 | + MainViewModelFactory(CheckModelDownloadedUseCase(repository)) |
| 28 | + } |
| 29 | + |
| 30 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 31 | + super.onCreate(savedInstanceState) |
| 32 | + enableEdgeToEdge() |
| 33 | + |
| 34 | + val sharedUrl = resolveSharedUrl(intent) |
| 35 | + |
| 36 | + setContent { |
| 37 | + TldwTheme { |
| 38 | + Surface(modifier = Modifier.fillMaxSize()) { |
| 39 | + val state by mainViewModel.state.collectAsStateWithLifecycle() |
| 40 | + |
| 41 | + when { |
| 42 | + state.isCheckingStatus -> { |
| 43 | + Box(contentAlignment = Alignment.Center, modifier = Modifier.fillMaxSize()) { |
| 44 | + CircularProgressIndicator() |
| 45 | + } |
| 46 | + } |
| 47 | + state.isModelDownloaded -> { |
| 48 | + TranscriptScreenRoute( |
| 49 | + sharedUrl = sharedUrl, |
| 50 | + onNavigateToModelDownload = { mainViewModel.checkStatus() }, |
| 51 | + ) |
| 52 | + } |
| 53 | + else -> { |
| 54 | + ModelDownloadScreenRoute(onModelReady = { mainViewModel.checkStatus() }) |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private fun resolveSharedUrl(intent: Intent): String? = |
| 63 | + if (intent.action == Intent.ACTION_SEND && intent.type == "text/plain") { |
| 64 | + intent.getStringExtra(Intent.EXTRA_TEXT) |
| 65 | + } else { |
| 66 | + null |
| 67 | + } |
| 68 | +} |
0 commit comments