Skip to content

Commit

Permalink
Merge branch 'dev' into 'main'
Browse files Browse the repository at this point in the history
Random article button, Coil 3 migration, animated splash screen
  • Loading branch information
nsh07 committed Nov 16, 2024
2 parents ce2887d + e29dfbf commit 107dea2
Show file tree
Hide file tree
Showing 20 changed files with 246 additions and 76 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Supports light mode, dark mode and Material You dynamic colors
quick.
- **Article image:** View an image of the topic from its Wikipedia page. Click on it to enlarge it
and view in full-screen
- **Random article:** Feeling lucky? Click the random article button to read a random article
- **One-handed use:** Use the floating action buttons at the bottom for a complete one-handed
experience
- **Lightweight:** The app starts instantly, and works smoothly
Expand Down
14 changes: 7 additions & 7 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ android {
applicationId = "org.nsh07.wikireader"
minSdk = 26
targetSdk = 35
versionCode = 14
versionName = "1.7.0"
versionCode = 15
versionName = "1.7.1"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
Expand Down Expand Up @@ -42,9 +42,6 @@ android {
buildFeatures {
compose = true
}
composeOptions {
kotlinCompilerExtensionVersion = "1.5.1"
}
packaging {
resources {
excludes += "/META-INF/{AL2.0,LGPL2.1}"
Expand All @@ -67,14 +64,17 @@ dependencies {
implementation(libs.androidx.ui.tooling.preview)
implementation(libs.androidx.material3)
implementation(libs.androidx.navigation.compose)
implementation(libs.androidx.core.splashscreen)

implementation(libs.androidx.datastore.preferences)
implementation(libs.kotlinx.serialization.json)
implementation(libs.retrofit2.retrofit)
implementation(libs.retrofit2.kotlinx.serialization.converter)
implementation(libs.okhttp)
implementation(libs.coil.kt.coil.compose)
implementation(libs.coil.svg)
implementation(libs.coil3.compose)
implementation(libs.coil3.network.okhttp)
implementation(libs.coil3.coil.svg)
implementation(libs.coil3.coil.gif)
implementation(libs.material.kolor)

testImplementation(libs.junit)
Expand Down
5 changes: 2 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.WikiReader"
android:theme="@style/Theme.WikiReader.Starting"
android:enableOnBackInvokedCallback="true"
tools:targetApi="35">
<activity
android:name="org.nsh07.wikireader.MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.WikiReader">
android:theme="@style/Theme.WikiReader.Starting">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
34 changes: 25 additions & 9 deletions app/src/main/java/org/nsh07/wikireader/AppScreen.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package org.nsh07.wikireader

import android.os.Build.VERSION.SDK_INT
import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.scaleIn
import androidx.compose.animation.scaleOut
import androidx.compose.animation.slideInHorizontally
import androidx.compose.animation.slideOutHorizontally
import androidx.compose.foundation.background
Expand Down Expand Up @@ -36,11 +35,15 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.TransformOrigin
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import coil3.ImageLoader
import coil3.gif.AnimatedImageDecoder
import coil3.gif.GifDecoder
import coil3.svg.SvgDecoder
import kotlinx.coroutines.launch
import org.nsh07.wikireader.ui.AppHomeScreen
import org.nsh07.wikireader.ui.FullScreenImage
Expand All @@ -60,16 +63,23 @@ fun AppScreen(
val searchBarState by viewModel.searchBarState.collectAsState()
val homeScreenState by viewModel.homeScreenState.collectAsState()
val listState by viewModel.listState.collectAsState()
val imageLoader = ImageLoader.Builder(LocalContext.current)
.components {
add(SvgDecoder.Factory())
if (SDK_INT >= 28) {
add(AnimatedImageDecoder.Factory())
} else {
add(GifDecoder.Factory())
}
}
.build()

val coroutineScope = rememberCoroutineScope()

val index by remember { derivedStateOf { listState.firstVisibleItemIndex } }
val (showDeleteDialog, setShowDeleteDialog) = remember { mutableStateOf(false) }
var (historyItem, setHistoryItem) = remember { mutableStateOf("") }

val fabEnter = scaleIn(transformOrigin = TransformOrigin(1f, 1f)) + fadeIn()
val fabExit = scaleOut(transformOrigin = TransformOrigin(1f, 1f)) + fadeOut()

val navController = rememberNavController()

NavHost(
Expand Down Expand Up @@ -138,9 +148,13 @@ fun AppScreen(
AppFab(
focusSearch = { viewModel.focusSearchBar() },
scrollToTop = { coroutineScope.launch { listState.animateScrollToItem(0) } },
index = index,
fabEnter = fabEnter,
fabExit = fabExit
performRandomPageSearch = {
viewModel.performSearch(
query = null,
random = true
)
},
index = index
)
},
modifier = Modifier.fillMaxSize()
Expand All @@ -149,6 +163,7 @@ fun AppScreen(
homeScreenState = homeScreenState,
listState = listState,
preferencesState = preferencesState,
imageLoader = imageLoader,
onImageClick = {
if (homeScreenState.photo != null)
navController.navigate("FullScreenImage")
Expand All @@ -166,6 +181,7 @@ fun AppScreen(
FullScreenImage(
photo = homeScreenState.photo,
photoDesc = homeScreenState.photoDesc,
imageLoader = imageLoader,
onBack = { navController.navigateUp() }
)
}
Expand Down
12 changes: 10 additions & 2 deletions app/src/main/java/org/nsh07/wikireader/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,30 @@ import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import org.nsh07.wikireader.data.toColor
import org.nsh07.wikireader.ui.theme.WikiReaderTheme
import org.nsh07.wikireader.ui.viewModel.UiViewModel

class MainActivity : ComponentActivity() {

val viewModel: UiViewModel by viewModels<UiViewModel>(factoryProducer = {UiViewModel.Factory})

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
viewModel.startAnimDuration()
installSplashScreen().setKeepOnScreenCondition {
!viewModel.isReady || !viewModel.isAnimDurationComplete
}
enableEdgeToEdge()

setContent {
val viewModel: UiViewModel = viewModel(factory = UiViewModel.Factory)
val preferencesState by viewModel.preferencesState.collectAsState()

val darkTheme = when (preferencesState.theme) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@ package org.nsh07.wikireader.data
import org.nsh07.wikireader.network.WikipediaApiService

interface WikipediaRepository {
suspend fun searchWikipedia(query: String): WikiApiResponse
suspend fun getSearchResult(query: String): WikiApiResponse
suspend fun getRandomResult(): WikiApiResponse
}

class NetworkWikipediaRepository(
private val wikipediaApiService: WikipediaApiService
) : WikipediaRepository {
override suspend fun searchWikipedia(query: String): WikiApiResponse {
return wikipediaApiService.searchWikipedia(query)
override suspend fun getSearchResult(query: String): WikiApiResponse {
return wikipediaApiService.getSearchResult(query)
}

override suspend fun getRandomResult(): WikiApiResponse {
return wikipediaApiService.getRandomResult()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ import retrofit2.http.Query
private const val API_QUERY =
"w/api.php?format=json&action=query&prop=extracts|pageimages|pageterms&piprop=original&pilicense=any&explaintext&generator=search&gsrlimit=1&redirects=1&formatversion=2"

private const val RANDOM_QUERY =
"w/api.php?format=json&action=query&prop=extracts|pageimages|pageterms&piprop=original&pilicense=any&explaintext&generator=random&redirects=1&formatversion=2&grnnamespace=0"

interface WikipediaApiService {
@GET(API_QUERY)
suspend fun searchWikipedia(@Query("gsrsearch") query: String): WikiApiResponse
suspend fun getSearchResult(@Query("gsrsearch") query: String): WikiApiResponse

@GET(RANDOM_QUERY)
suspend fun getRandomResult(): WikiApiResponse
}
3 changes: 3 additions & 0 deletions app/src/main/java/org/nsh07/wikireader/ui/AppHomeScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import coil3.ImageLoader
import org.nsh07.wikireader.R
import org.nsh07.wikireader.ui.viewModel.HomeScreenState
import org.nsh07.wikireader.ui.viewModel.PreferencesState
Expand All @@ -43,6 +44,7 @@ fun AppHomeScreen(
homeScreenState: HomeScreenState,
listState: LazyListState,
preferencesState: PreferencesState,
imageLoader: ImageLoader,
onImageClick: () -> Unit,
insets: PaddingValues,
modifier: Modifier = Modifier
Expand Down Expand Up @@ -74,6 +76,7 @@ fun AppHomeScreen(
ImageCard(
photo = photo,
photoDesc = photoDesc,
imageLoader = imageLoader,
showPhoto = !preferencesState.dataSaver,
onClick = onImageClick,
)
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/org/nsh07/wikireader/ui/FullScreenImage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import androidx.compose.ui.layout.onSizeChanged
import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.unit.IntSize
import androidx.core.view.WindowCompat
import coil3.ImageLoader
import kotlinx.coroutines.launch
import org.nsh07.wikireader.data.WikiPhoto
import org.nsh07.wikireader.data.WikiPhotoDesc
Expand All @@ -40,6 +41,7 @@ import org.nsh07.wikireader.ui.scaffoldComponents.FullScreenImageTopBar
fun FullScreenImage(
photo: WikiPhoto?,
photoDesc: WikiPhotoDesc?,
imageLoader: ImageLoader,
onBack: () -> Unit,
modifier: Modifier = Modifier
) {
Expand Down Expand Up @@ -90,6 +92,7 @@ fun FullScreenImage(
PageImage(
photo = photo,
photoDesc = photoDesc,
imageLoader = imageLoader,
contentScale = ContentScale.Inside,
modifier = Modifier
.onSizeChanged { size = it }
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/java/org/nsh07/wikireader/ui/ImageCard.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import coil.compose.SubcomposeAsyncImage
import coil3.ImageLoader
import coil3.compose.SubcomposeAsyncImage
import org.nsh07.wikireader.data.WikiPhoto
import org.nsh07.wikireader.data.WikiPhotoDesc

Expand All @@ -36,6 +37,7 @@ import org.nsh07.wikireader.data.WikiPhotoDesc
fun ImageCard(
photo: WikiPhoto?,
photoDesc: WikiPhotoDesc,
imageLoader: ImageLoader,
showPhoto: Boolean,
onClick: () -> Unit,
modifier: Modifier = Modifier
Expand All @@ -59,6 +61,7 @@ fun ImageCard(
photo = photo,
photoDesc = photoDesc,
contentScale = ContentScale.Crop,
imageLoader = imageLoader,
modifier = Modifier.fillMaxWidth()
)
}
Expand Down
15 changes: 6 additions & 9 deletions app/src/main/java/org/nsh07/wikireader/ui/PageImage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import coil.ImageLoader
import coil.compose.SubcomposeAsyncImage
import coil.decode.SvgDecoder
import coil.request.ImageRequest
import coil3.ImageLoader
import coil3.compose.SubcomposeAsyncImage
import coil3.request.ImageRequest
import coil3.request.crossfade
import org.nsh07.wikireader.R
import org.nsh07.wikireader.data.WikiPhoto
import org.nsh07.wikireader.data.WikiPhotoDesc
Expand All @@ -25,6 +25,7 @@ import org.nsh07.wikireader.data.WikiPhotoDesc
fun PageImage(
photo: WikiPhoto,
photoDesc: WikiPhotoDesc,
imageLoader: ImageLoader,
contentScale: ContentScale,
modifier: Modifier = Modifier
) {
Expand Down Expand Up @@ -55,11 +56,7 @@ fun PageImage(
)
},
contentDescription = photoDesc.description?.get(0) ?: "",
imageLoader = ImageLoader.Builder(LocalContext.current)
.components {
add(SvgDecoder.Factory())
}
.build(),
imageLoader = imageLoader,
contentScale = contentScale,
modifier = modifier
)
Expand Down
Loading

0 comments on commit 107dea2

Please sign in to comment.