Skip to content

Commit

Permalink
Convert sample to compose
Browse files Browse the repository at this point in the history
Various improvements.

1. Compose to be up to date with android standards.
2. Kotlin since Java is no longer the programming language for Android.
3. Coroutines instead of threads for better multithreaded management.
4. Flows instead of LiveData
5. Document the code somewhat.
  • Loading branch information
Doomsdayrs committed Jul 22, 2024
1 parent 8c61e0f commit 550a818
Show file tree
Hide file tree
Showing 19 changed files with 511 additions and 384 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ buildscript {
}
dependencies {
classpath(libs.gradle)
classpath(libs.kotlin.gradle.plugin)

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
22 changes: 21 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ test = "1.6.1"
tesseract4android = "4.7.0"
coreKtx = "1.13.1"
kotlin = "2.0.0"
kotlinGradlePlugin = "1.9.0"
lifecycleRuntimeKtx = "2.8.3"
activityCompose = "1.9.0"
composeBom = "2024.06.00"
window = "1.3.0"
adaptiveAndroid = "1.0.0-beta04"

[libraries]
androidx-annotation = { module = "androidx.annotation:annotation", version.ref = "annotation" }
Expand All @@ -25,8 +31,10 @@ androidx-espresso-core = { module = "androidx.test.espresso:espresso-core", vers
androidx-junit = { module = "androidx.test.ext:junit", version.ref = "androidJUnit" }
androidx-lifecycle-livedata = { module = "androidx.lifecycle:lifecycle-livedata", version.ref = "lifecycleLivedata" }
androidx-lifecycle-viewmodel = { module = "androidx.lifecycle:lifecycle-viewmodel", version.ref = "lifecycleLivedata" }
androidx-lifecycle-viewmodel-compose = { module = "androidx.lifecycle:lifecycle-viewmodel-compose" }
androidx-rules = { module = "androidx.test:rules", version.ref = "test" }
androidx-runner = { module = "androidx.test:runner", version.ref = "test" }
androidx-window = { module = "androidx.window:window", version.ref = "window" }
gradle = { module = "com.android.tools.build:gradle", version.ref = "gradle" }
junit = { module = "junit:junit", version.ref = "junit" }
material = { module = "com.google.android.material:material", version.ref = "material" }
Expand All @@ -38,6 +46,18 @@ tesseract4android-jitpack-openmp = { group = "cz.adaptech.tesseract4android", na
tesseract4android-local = { group = "cz.adaptech", name = "tesseract4android", version.ref = "tesseract4android" }
tesseract4android-local-openmp = { group = "cz.adaptech", name = "tesseract4android-openmp", version.ref = "tesseract4android" }
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
kotlin-gradle-plugin = { group = "org.jetbrains.kotlin", name = "kotlin-gradle-plugin", version.ref = "kotlinGradlePlugin" }
androidx-lifecycle-runtime-ktx = { group = "androidx.lifecycle", name = "lifecycle-runtime-ktx", version.ref = "lifecycleRuntimeKtx" }
androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "activityCompose" }
androidx-compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "composeBom" }
androidx-ui = { group = "androidx.compose.ui", name = "ui" }
androidx-ui-graphics = { group = "androidx.compose.ui", name = "ui-graphics" }
androidx-ui-tooling = { group = "androidx.compose.ui", name = "ui-tooling" }
androidx-ui-tooling-preview = { group = "androidx.compose.ui", name = "ui-tooling-preview" }
androidx-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest" }
androidx-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit4" }
androidx-material3 = { group = "androidx.compose.material3", name = "material3" }
androidx-adaptive-android = { group = "androidx.compose.material3.adaptive", name = "adaptive-android", version.ref = "adaptiveAndroid" }

[plugins]
jetbrains-kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
jetbrains-kotlin-android = { id = "org.jetbrains.kotlin.android" }
34 changes: 29 additions & 5 deletions sample/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ android {
versionName = "1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary = true
}
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
Expand All @@ -32,10 +34,19 @@ android {
}
buildFeatures {
viewBinding = true
compose = true
}
kotlinOptions {
jvmTarget = "17"
}
composeOptions {
kotlinCompilerExtensionVersion = "1.5.1"
}
packaging {
resources {
excludes += "/META-INF/{AL2.0,LGPL2.1}"
}
}
}

// In case you are using dependency on local library (the project(":tesseract4android") below),
Expand Down Expand Up @@ -64,13 +75,26 @@ dependencies {
// Which flavor to use is determined by missingDimensionStrategy parameter above.
//implementation(project(":tesseract4android"))

implementation(libs.androidx.appcompat)
implementation(libs.material)
implementation(libs.androidx.constraintlayout)
implementation(libs.androidx.lifecycle.livedata)
implementation(libs.androidx.lifecycle.viewmodel)
implementation(libs.androidx.core.ktx)
implementation(libs.androidx.activity.compose)
implementation(platform(libs.androidx.compose.bom))
implementation(libs.androidx.ui)
implementation(libs.androidx.ui.graphics)
implementation(libs.androidx.ui.tooling.preview)
implementation(libs.androidx.material3)
implementation(libs.androidx.window)

implementation(libs.androidx.lifecycle.runtime.ktx)
implementation(libs.androidx.lifecycle.viewmodel)
implementation(libs.androidx.lifecycle.viewmodel.compose)
implementation(libs.androidx.adaptive.android)

testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
androidTestImplementation(platform(libs.androidx.compose.bom))
androidTestImplementation(libs.androidx.ui.test.junit4)
debugImplementation(libs.androidx.ui.tooling)
debugImplementation(libs.androidx.ui.test.manifest)
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package cz.adaptech.tesseract4android.sample

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import cz.adaptech.tesseract4android.sample.ui.main.MainFragment
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.ui.Modifier
import cz.adaptech.tesseract4android.sample.ui.main.MainView
import cz.adaptech.tesseract4android.sample.ui.theme.Tesseract4AndroidTheme

class MainActivity : AppCompatActivity() {
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if (savedInstanceState == null) {
supportFragmentManager.beginTransaction()
.replace(R.id.container, MainFragment.newInstance())
.commitNow()
enableEdgeToEdge()
setContent {
MainView()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package cz.adaptech.tesseract4android.sample


/**
* Represents the various states that the OCR can be in.
*
* @since 2024/07/22
* @author Clocks
*/
sealed interface OCRState {
/**
* OCR is loading up.
*/
data object Loading : OCRState

/**
* OCR is prepared.
*
* @param version Version of tesseract
* @param flavour Build flavour of tesseract
*/
data class StartUp(val version: String, val flavour: String) : OCRState

/**
* OCR has been stopped.
*/
data object Stopped : OCRState

/**
* OCR is being stopped.
*/
data object Stopping : OCRState

/**
* OCR is starting up.
*/
data object Processing : OCRState

/**
* OCR is currently in process.
*
* @param progress 0-100 progress indication.
*/
data class Progress(val progress: Int) : OCRState

/**
* OCR has completed its task.
*
* @param time How many seconds it took to process the image.
*/
data class Finished(val time: Float) : OCRState
}

This file was deleted.

Loading

0 comments on commit 550a818

Please sign in to comment.