Skip to content

Commit

Permalink
Simplify example
Browse files Browse the repository at this point in the history
  • Loading branch information
blundell committed Jan 11, 2022
1 parent a6a8910 commit b09819b
Show file tree
Hide file tree
Showing 23 changed files with 45 additions and 223 deletions.
2 changes: 1 addition & 1 deletion mono-build-logic/android-k-plugins/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ plugins {

dependencies {
api(project(":android-plugins"))
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.30")
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.31")
implementation("com.android.tools.build:gradle:7.0.4")
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import com.blundell.systemPropertyIsInAppPurchaseTest

plugins {
id("android-module")
id("com.android.application")
Expand All @@ -15,11 +13,7 @@ android {
}
getByName("debug") {
isDebuggable = true
if (systemPropertyIsInAppPurchaseTest()) {
println(" IAP TEST - DEBUG BUILD WITH PROD PACKAGE NAME -")
} else {
applicationIdSuffix = ".debug"
}
applicationIdSuffix = ".debug"
}
}
}

This file was deleted.

This file was deleted.

2 changes: 1 addition & 1 deletion mono-build-logic/android-plugins/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ plugins {
}

dependencies {
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.30")
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.31")
implementation("com.android.tools.build:gradle:7.0.4")
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ afterEvaluate { project ->
unitTests.returnDefaultValues = true
}
composeOptions {
kotlinCompilerExtensionVersion "1.1.0-rc02"
kotlinCompilerExtensionVersion compose_version
}
packagingOptions {
resources {
Expand Down
6 changes: 2 additions & 4 deletions mono-build-logic/tut-app-1-plugins/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
plugins {
// id("groovy-gradle-plugin") // This enables src/main/groovy
`kotlin-dsl` // This enables src/main/kotlin
// id("java-gradle-plugin")
}

dependencies {
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.30") // 1.4.10
implementation("com.android.tools.build:gradle:7.0.4") //4.1.0
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.31")
implementation("com.android.tools.build:gradle:7.0.4")
implementation(project(":android-k-plugins"))
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import com.blundell.SemVersion

plugins {
id("app-android-module")
}

android {
defaultConfig {
applicationId = "com.blundell.tut1"

SemVersion(0, 1, 0, 0).apply {
versionCode = asCode()
versionName = asName()
}
versionCode = 1
versionName = "1.0.0"
}
}
6 changes: 2 additions & 4 deletions mono-build-logic/tut-app-2-plugins/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
plugins {
// id("groovy-gradle-plugin") // This enables src/main/groovy
`kotlin-dsl` // This enables src/main/kotlin
// id("java-gradle-plugin")
}

dependencies {
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.30") // 1.4.10
implementation("com.android.tools.build:gradle:7.0.4") //4.1.0
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.31")
implementation("com.android.tools.build:gradle:7.0.4")
implementation(project(":android-k-plugins"))
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import com.blundell.SemVersion

plugins {
id("app-android-module")
}

android {
defaultConfig {
applicationId = "com.blundell.tut2"

SemVersion(0, 1, 3, 0).apply {
versionCode = asCode()
versionName = asName()
}
versionCode = 13
versionName = "1.3.0"
}
}
4 changes: 0 additions & 4 deletions mono-libraries/http/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
This module handles talking through HTTP.
It also makes some assumptions about mobile connections:
- we'll want to retry failed connections a few times before reporting an error
- we expect timeouts to be longer than the default on mobile (TODO)
- we want errors to be reported as kotlin.Result and not exceptions
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class OkHttpExecutor(
} catch (e: InterruptedIOException) {
logg.e("Interrupted exception.")
return Result.failure(NetworkException(Timeout(e)))
} catch (e: Exception) {
logg.e("Catch all exception.")
return Result.failure(NetworkException(Timeout(e)))
}
}

Expand Down
4 changes: 0 additions & 4 deletions tut-app-1/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ plugins {
dependencies {
val implementation by configurations
val debugImplementation by configurations
val testImplementation by configurations

implementation(project(":http"))
implementation(project(":logging"))

val composeVersion = rootProject.extra["compose_version"]
Expand All @@ -23,6 +21,4 @@ dependencies {
implementation("androidx.activity:activity-compose:1.4.0")

debugImplementation("androidx.compose.ui:ui-tooling:$composeVersion")

testImplementation("junit:junit:latest.release")
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.blundell.tut1
import android.app.Application
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import modularisation.blundell.library.http.api.HttpBootstrapper
import modularisation.blundell.library.logging.api.LoggBootstrapper

class AppViewModelFactory(
Expand All @@ -12,11 +11,9 @@ class AppViewModelFactory(

override fun <T : ViewModel?> create(modelClass: Class<T>): T {
val logg = LoggBootstrapper.getLogger(BuildConfig.DEBUG, "TUT1")
val httpNetworker = HttpBootstrapper.getHttpNetworker(logg)
@Suppress("UNCHECKED_CAST")
return MainViewModel(
logg,
httpNetworker,
) as T
}
}
26 changes: 4 additions & 22 deletions tut-app-1/app/src/main/java/com/blundell/tut1/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,15 @@ class MainActivity : ComponentActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
logg.d("onCreate")
val model: MainViewModel by viewModels {
AppViewModelFactory(this.application)
}
model.viewState.observe(this) { state ->
when (state) {
is ViewState.Complete -> logg.d("State complete.")
is ViewState.Error -> logg.d("State error.")
is ViewState.Loading -> logg.d("State loading.")
ViewState.Idle -> logg.d("State idle.")
}
}
model.onStart()
setContent {
Tut1Theme {
val state by (viewModel() as MainViewModel).viewState.observeAsState(ViewState.Idle)
when (state) {
is ViewState.Idle -> {
Surface(color = MaterialTheme.colors.background) {
Text(text = "Welcome!")
}
}
is ViewState.Complete -> {
val stateComplete = state as ViewState.Complete
Surface(color = MaterialTheme.colors.background) {
Expand All @@ -53,15 +41,6 @@ class MainActivity : ComponentActivity() {
)
}
}
is ViewState.Error -> {
val stateError = state as ViewState.Error
Surface(color = MaterialTheme.colors.background) {
Text(
style = MaterialTheme.typography.h3,
text = stateError.errorMessage,
)
}
}
is ViewState.Loading -> {
val stateLoading = state as ViewState.Loading
Surface(color = MaterialTheme.colors.background) {
Expand All @@ -72,6 +51,9 @@ class MainActivity : ComponentActivity() {
)
}
}
else -> {
// TUT do nothing
}
}
}
}
Expand Down
33 changes: 6 additions & 27 deletions tut-app-1/app/src/main/java/com/blundell/tut1/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,53 +6,32 @@ import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import modularisation.blundell.library.http.api.HttpNetworker
import modularisation.blundell.library.logging.api.Logg

class MainViewModel(
private val logg: Logg,
private val httpNetworker: HttpNetworker,
) : ViewModel() {

val viewState = MutableLiveData<ViewState>(ViewState.Idle)

fun onStart() {
logg.d("VM onStart()")
viewModelScope.launch(Dispatchers.Main) {
viewState.value = ViewState.Loading("Doing some http background stuff.")
viewState.value = ViewState.Loading("Doing some background world creating stuff.")
}
viewModelScope.launch(Dispatchers.IO) {
val content = httpNetworker
.getContent("https://your.westlancs.gov.uk/?uprn=100010662672")
if (content.isSuccess) {
val contentHtml = content.getOrThrow()
try {
logg.d("Doing stuff")
delay(1000)
viewModelScope.launch(Dispatchers.Main) {
viewState.value = ViewState.Complete(
"Completed stuff",
)
}
} catch (ex: Exception) {
logg.e("Failed to do stuff", IllegalStateException("Error in [$contentHtml]."))
viewModelScope.launch(Dispatchers.Main) {
viewState.value = ViewState.Error("Oh no! A reading error occurred, sorry about that.")
}
}
} else {
logg.e("Failed to load.", content.exceptionOrNull()!!)
viewModelScope.launch(Dispatchers.Main) {
viewState.value = ViewState.Error("Oh no! A network error occurred, sorry about that.")
}
logg.d("Doing background stuff.")
delay(2000)
viewModelScope.launch(Dispatchers.Main) {
viewState.value = ViewState.Complete("Hello World!")
}
logg.d("Background stuff finished.")
}
}
}

sealed class ViewState {
data class Complete(val message: String) : ViewState()
data class Loading(val loadingMessage: String) : ViewState()
data class Error(val errorMessage: String) : ViewState()
object Idle : ViewState()
}
16 changes: 0 additions & 16 deletions tut-app-1/app/src/test/java/com/blundell/tut1/ExampleUnitTest.kt

This file was deleted.

This file was deleted.

Loading

0 comments on commit b09819b

Please sign in to comment.