Skip to content

Commit

Permalink
Updated various android dependencies.
Browse files Browse the repository at this point in the history
Updated the pdfium dependencies.
  • Loading branch information
johngray1965 committed Sep 28, 2024
1 parent 3aef723 commit a2aae80
Show file tree
Hide file tree
Showing 53 changed files with 1,857 additions and 1,524 deletions.
48 changes: 34 additions & 14 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
# Created by https://www.toptal.com/developers/gitignore/api/androidstudio,kotlin,c++
# Edit at https://www.toptal.com/developers/gitignore?templates=androidstudio,kotlin,c++
# Created by https://www.toptal.com/developers/gitignore/api/androidstudio,android,c++,kotlin
# Edit at https://www.toptal.com/developers/gitignore?templates=androidstudio,android,c++,kotlin
### Android ###
# Gradle files
.gradle/
build/
# Local configuration file (sdk path, etc)
local.properties
# Log/OS Files
*.log
# Android Studio generated files and folders
captures/
.externalNativeBuild/
.cxx/
*.apk
output.json
# IntelliJ
*.iml
.idea/
misc.xml
deploymentTargetDropDown.xml
render.experimental.xml
# Keystore files
*.jks
*.keystore
# Google Services (e.g. APIs or Firebase)
google-services.json
# Android Profiling
*.hprof
### Android Patch ###
gen-external-apklibs
# Replacement of .externalNativeBuild directories introduced
# with Android Studio 3.5.

### C++ ###
# Prerequisites
Expand All @@ -16,7 +47,6 @@
*.pch

# Compiled Dynamic libraries
*.so
*.dylib
*.dll

Expand All @@ -40,7 +70,6 @@
*.class

# Log file
*.log

# BlueJ files
*.ctxt
Expand All @@ -65,7 +94,6 @@ replay_pid*
# Covers files to be ignored for android development using Android Studio.

# Built application files
*.apk
*.ap_
*.aab

Expand All @@ -81,14 +109,11 @@ out/

# Gradle files
.gradle
.gradle/
build/

# Signing files
.signing/

# Local configuration file (sdk path, etc)
local.properties

# Proguard folder generated by Eclipse
proguard/
Expand All @@ -101,21 +126,17 @@ proguard/
/*/out
/*/*/build
/*/*/production
captures/
.navigation/
*.ipr
*~
*.swp

# Keystore files
*.jks
*.keystore

# Google Services (e.g. APIs or Firebase)
# google-services.json

# Android Patch
gen-external-apklibs

# External native build folder generated in Android Studio 2.2 and later
.externalNativeBuild
Expand All @@ -124,7 +145,6 @@ gen-external-apklibs
obj/

# IntelliJ IDEA
*.iml
*.iws
/out/

Expand Down Expand Up @@ -187,7 +207,7 @@ fabric.properties

!/gradle/wrapper/gradle-wrapper.jar

# End of https://www.toptal.com/developers/gitignore/api/androidstudio,kotlin,c++
# End of https://www.toptal.com/developers/gitignore/api/androidstudio,android,c++,kotlin

/*/.cxx
/x/**
150 changes: 82 additions & 68 deletions app/src/main/java/io/legere/pdfiumandroidkt/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ private const val MAX_DISK_CACHE_SIZE_PERCENTAGE = 0.1

@AndroidEntryPoint
class MainActivity : ComponentActivity() {

private val viewModel: MainViewModel by viewModels()

private val openFileContract =
Expand All @@ -73,30 +72,33 @@ class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val imageLoader = ImageLoader.Builder(this)
.components(fun ComponentRegistry.Builder.() {
add(PdfiumFetcher.Factory())
})
.allowRgb565(true)
.memoryCache {
MemoryCache.Builder(this)
.maxSizePercent(MAX_MEMORY_CACHE_SIZE_PERCENTAGE)
.build()
}
.diskCache {
DiskCache.Builder()
.directory(cacheDir.resolve("image_cache"))
.maxSizePercent(MAX_DISK_CACHE_SIZE_PERCENTAGE)
.build()
}
.build()
val imageLoader =
ImageLoader.Builder(this)
.components(
fun ComponentRegistry.Builder.() {
add(PdfiumFetcher.Factory())
},
)
.allowRgb565(true)
.memoryCache {
MemoryCache.Builder(this)
.maxSizePercent(MAX_MEMORY_CACHE_SIZE_PERCENTAGE)
.build()
}
.diskCache {
DiskCache.Builder()
.directory(cacheDir.resolve("image_cache"))
.maxSizePercent(MAX_DISK_CACHE_SIZE_PERCENTAGE)
.build()
}
.build()

setContent {
PdfiumAndroidKtTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
color = MaterialTheme.colorScheme.background,
) {
MyUI(viewModel, openFileContract, imageLoader)
}
Expand All @@ -110,7 +112,7 @@ class MainActivity : ComponentActivity() {
fun MyUI(
viewModel: MainViewModel,
openFileContract: ActivityResultLauncher<Array<String>>?,
imageLoader: ImageLoader
imageLoader: ImageLoader,
) {
Scaffold(
topBar = {
Expand All @@ -124,12 +126,12 @@ fun MyUI(
}) {
Icon(
painter = painterResource(id = R.drawable.outline_file_open_24),
contentDescription = "Open"
contentDescription = "Open",
)
}
}
},
)
}
},
) { contentPadding ->
Box(modifier = Modifier.padding(contentPadding)) {
MainContent(viewModel, imageLoader)
Expand All @@ -138,7 +140,10 @@ fun MyUI(
}

@Composable
fun MainContent(viewModel: MainViewModel, imageLoader: ImageLoader) {
fun MainContent(
viewModel: MainViewModel,
imageLoader: ImageLoader,
) {
val state = viewModel.state.collectAsState()
when (state.value.loadState) {
MainViewModel.LoadStatus.Loading -> MaxSizeCenterBox { Message("Loading") }
Expand All @@ -151,10 +156,11 @@ fun MainContent(viewModel: MainViewModel, imageLoader: ImageLoader) {
@Composable
private fun MaxSizeCenterBox(content: @Composable () -> Unit) {
Box(
modifier = Modifier
.fillMaxWidth()
.fillMaxHeight(),
contentAlignment = Alignment.Center
modifier =
Modifier
.fillMaxWidth()
.fillMaxHeight(),
contentAlignment = Alignment.Center,
) {
content()
}
Expand All @@ -169,53 +175,59 @@ private fun Message(text: String = "Error") {

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun MyPager(viewModel: MainViewModel, imageLoader: ImageLoader) {
fun MyPager(
viewModel: MainViewModel,
imageLoader: ImageLoader,
) {
val state = viewModel.state.collectAsState()
val pagerState = rememberPagerState(
initialPage = 0,
initialPageOffsetFraction = 0f
) {
state.value.pageCount
}
val pagerState =
rememberPagerState(
initialPage = 0,
initialPageOffsetFraction = 0f,
) {
state.value.pageCount
}
var componentWidth by remember { mutableIntStateOf(0) }
var componentHeight by remember { mutableIntStateOf(0) }

// get local density from composable
val density = LocalDensity.current

Surface(
modifier = Modifier
.onGloballyPositioned {
componentWidth = it.size.width
componentHeight = it.size.height
}

modifier =
Modifier
.onGloballyPositioned {
componentWidth = it.size.width
componentHeight = it.size.height
},
) {
VerticalPager(
modifier = Modifier
.fillMaxWidth()
.fillMaxHeight(),
modifier =
Modifier
.fillMaxWidth()
.fillMaxHeight(),
state = pagerState,
pageSpacing = 0.dp,
userScrollEnabled = true,
reverseLayout = false,
contentPadding = PaddingValues(0.dp),
beyondBoundsPageCount = 0,
key = null,
pageNestedScrollConnection = PagerDefaults.pageNestedScrollConnection(
pagerState,
Orientation.Horizontal
),
pageNestedScrollConnection =
PagerDefaults.pageNestedScrollConnection(
pagerState,
Orientation.Horizontal,
),
pageContent = {
PagerScope(
page = it,
viewModel = viewModel,
componentWidth,
componentHeight,
density,
imageLoader
imageLoader,
)
}
},
)
}
}
Expand All @@ -228,7 +240,7 @@ fun PagerScope(
componentWidth: Int,
componentHeight: Int,
density: Density,
imageLoader: ImageLoader
imageLoader: ImageLoader,
) {
if (componentWidth <= 0 || componentHeight <= 0) {
return
Expand All @@ -238,28 +250,30 @@ fun PagerScope(
"PagerScope: page: $page, " +
"componentWidth: $componentWidth, " +
"componentHeight: $componentHeight, " +
"density: $density"
"density: $density",
)

AsyncImage(
model = ImageRequest.Builder(LocalContext.current)
.data(
PdfiumFetcherData(
page = page,
width = componentWidth,
height = componentHeight,
density = density.density.roundToInt(),
viewModel = viewModel
model =
ImageRequest.Builder(LocalContext.current)
.data(
PdfiumFetcherData(
page = page,
width = componentWidth,
height = componentHeight,
density = density.density.roundToInt(),
viewModel = viewModel,
),
)
)
.memoryCacheKey("page_$page")
.diskCachePolicy(CachePolicy.ENABLED)
.memoryCachePolicy(CachePolicy.ENABLED)
.build(),
.memoryCacheKey("page_$page")
.diskCachePolicy(CachePolicy.ENABLED)
.memoryCachePolicy(CachePolicy.ENABLED)
.build(),
contentDescription = "Page $page",
imageLoader = imageLoader,
modifier = Modifier
.fillMaxWidth()
.fillMaxHeight()
modifier =
Modifier
.fillMaxWidth()
.fillMaxHeight(),
)
}
Loading

0 comments on commit a2aae80

Please sign in to comment.