From b0c2fe35210bc00403bc0f147990fe835936a8f9 Mon Sep 17 00:00:00 2001 From: Shubham Singh Date: Sat, 2 Apr 2022 12:05:24 +0530 Subject: [PATCH] Migrated Barricade1 to Barricade2 --- app/.gitignore | 2 +- app/build.gradle | 140 ++++----- app/proguard-rules.pro | 27 +- .../barricade/ExampleInstrumentedTest.kt | 22 ++ .../sample/BarricadeActivityTest.java | 82 ------ .../barricade/sample/MainActivityTest.java | 60 ---- .../sample/utils/RecyclerViewMatcher.java | 69 ----- app/src/main/AndroidManifest.xml | 36 +-- .../sample/BarricadeSampleApplication.java | 12 - .../barricade/sample/MainActivity.java | 124 -------- .../sample/api/ChuckNorrisApiService.java | 19 -- .../barricade/sample/api/model/Joke.java | 24 -- .../barricade/sample/api/util/ApiUtils.java | 29 -- .../barricadeSample/B2Application.kt | 12 + .../barricadeSample/MainActivity.kt | 42 +++ .../barricadeSample/data/ApiService.kt | 26 ++ .../barricadeSample/data/JokeApi.kt | 29 ++ .../data/models/JokeResponse.kt | 8 + .../barricadeSample/states/ResponseState.kt | 8 + .../ui/screens/LandingScreen.kt | 181 ++++++++++++ .../barricadeSample/ui/theme/Color.kt | 8 + .../barricadeSample/ui/theme/Shape.kt | 11 + .../barricadeSample/ui/theme/Theme.kt | 44 +++ .../barricadeSample/ui/theme/Type.kt | 28 ++ .../viewmodels/LandingScreenVM.kt | 65 +++++ .../drawable-v24/ic_launcher_foreground.xml | 30 ++ .../res/drawable/ic_launcher_background.xml | 170 +++++++++++ app/src/main/res/drawable/ic_settings.xml | 9 - app/src/main/res/layout/activity_main.xml | 56 ---- app/src/main/res/menu/menu_main.xml | 8 - .../res/mipmap-anydpi-v26/ic_launcher.xml | 5 + .../mipmap-anydpi-v26/ic_launcher_round.xml | 5 + app/src/main/res/mipmap-hdpi/ic_launcher.png | Bin 3418 -> 0 bytes app/src/main/res/mipmap-hdpi/ic_launcher.webp | Bin 0 -> 1404 bytes .../res/mipmap-hdpi/ic_launcher_round.webp | Bin 0 -> 2898 bytes app/src/main/res/mipmap-mdpi/ic_launcher.png | Bin 2206 -> 0 bytes app/src/main/res/mipmap-mdpi/ic_launcher.webp | Bin 0 -> 982 bytes .../res/mipmap-mdpi/ic_launcher_round.webp | Bin 0 -> 1772 bytes app/src/main/res/mipmap-xhdpi/ic_launcher.png | Bin 4842 -> 0 bytes .../main/res/mipmap-xhdpi/ic_launcher.webp | Bin 0 -> 1900 bytes .../res/mipmap-xhdpi/ic_launcher_round.webp | Bin 0 -> 3918 bytes .../main/res/mipmap-xxhdpi/ic_launcher.png | Bin 7718 -> 0 bytes .../main/res/mipmap-xxhdpi/ic_launcher.webp | Bin 0 -> 2884 bytes .../res/mipmap-xxhdpi/ic_launcher_round.webp | Bin 0 -> 5914 bytes .../main/res/mipmap-xxxhdpi/ic_launcher.png | Bin 10486 -> 0 bytes .../main/res/mipmap-xxxhdpi/ic_launcher.webp | Bin 0 -> 3844 bytes .../res/mipmap-xxxhdpi/ic_launcher_round.webp | Bin 0 -> 7778 bytes app/src/main/res/values-w820dp/dimens.xml | 6 - app/src/main/res/values/colors.xml | 12 +- app/src/main/res/values/dimens.xml | 5 - app/src/main/res/values/strings.xml | 13 +- app/src/main/res/values/styles.xml | 11 - app/src/main/res/values/themes.xml | 7 + .../mutualmobile/barricade/ExampleUnitTest.kt | 16 ++ .../sample/api/ChuckNorrisApiServiceTest.java | 88 ------ barricade-annotations/.gitignore | 2 +- barricade-annotations/build.gradle | 18 +- .../barricade/IBarricadeConfig.java | 14 - .../barricade/IBarricadeConfig.kt | 12 + .../barricade/annotation/Barricade.java | 17 -- .../barricade/annotation/Barricade.kt | 11 + .../barricade/annotation/Response.java | 16 -- .../barricade/annotation/Response.kt | 12 + .../barricade/response/BarricadeResponse.java | 21 -- .../barricade/response/BarricadeResponse.kt | 7 + .../response/BarricadeResponseSet.java | 18 -- .../response/BarricadeResponseSet.kt | 10 + barricade-compiler/.gitignore | 2 +- barricade-compiler/build.gradle | 24 +- .../compiler/BarricadeProcessor.java | 87 ------ .../barricade/compiler/BarricadeProcessor.kt | 88 ++++++ .../compiler/BarricadeProcessorProvider.kt | 14 + .../barricade/compiler/CodeGenerator.java | 184 ------------ .../barricade/compiler/StringUtils.java | 36 --- .../compiler/utils/ExtensionUtils.kt | 7 + .../compiler/visitors/BarricadeVisitor.kt | 46 +++ ...ols.ksp.processing.SymbolProcessorProvider | 1 + barricade/.gitignore | 2 +- barricade/build.gradle | 98 ++++--- barricade/consumer-rules.pro | 0 barricade/proguard-rules.pro | 17 +- .../barricade/ApplicationTest.java | 13 - .../barricade/ExampleInstrumentedTest.kt | 22 ++ barricade/src/main/AndroidManifest.xml | 20 +- .../com/mutualmobile/barricade/Barricade.java | 209 -------------- .../com/mutualmobile/barricade/Barricade.kt | 156 ++++++++++ .../barricade/BarricadeConfigActivity.kt | 40 +++ .../barricade/BarricadeInterceptor.java | 39 --- .../barricade/BarricadeInterceptor.kt | 36 +++ .../barricade/activity/BarricadeActivity.java | 174 ----------- .../adapter/BarricadeEndpointsRVAdapter.java | 70 ----- .../adapter/BarricadeResponsesRVAdapter.java | 74 ----- .../ui/screens/BarricadeConfigScreen.kt | 270 ++++++++++++++++++ .../mutualmobile/barricade/ui/theme/Color.kt | 9 + .../mutualmobile/barricade/ui/theme/Shape.kt | 11 + .../mutualmobile/barricade/ui/theme/Theme.kt | 44 +++ .../mutualmobile/barricade/ui/theme/Type.kt | 28 ++ .../utils/AndroidAssetFileManager.java | 49 ---- .../utils/AndroidAssetFileManager.kt | 35 +++ ...etFileManager.java => AssetFileManager.kt} | 18 +- .../utils/BarricadeShakeListener.java | 109 ------- .../barricade/utils/ContextExtensions.kt | 8 + .../barricade/utils/TestAssetFileManager.java | 49 ---- .../src/main/res/drawable/ic_done_red.xml | 9 - .../{ic_timer_white.xml => ic_timer.xml} | 7 +- barricade/src/main/res/drawable/ic_undo.xml | 15 +- .../main/res/layout/activity_barricade.xml | 36 --- .../res/layout/dialog_edit_global_delay.xml | 17 -- .../layout_barricade_response_list_item.xml | 50 ---- barricade/src/main/res/menu/main.xml | 15 - barricade/src/main/res/values/colors.xml | 6 - barricade/src/main/res/values/strings.xml | 29 +- barricade/src/main/res/values/themes.xml | 7 + .../mutualmobile/barricade/ExampleUnitTest.kt | 16 ++ build.gradle | 40 +-- gradle.properties | 30 +- gradle/wrapper/gradle-wrapper.jar | Bin 53636 -> 59203 bytes gradle/wrapper/gradle-wrapper.properties | 6 +- gradlew | 111 ++++--- gradlew.bat | 53 ++-- settings.gradle | 24 +- 121 files changed, 1987 insertions(+), 2280 deletions(-) create mode 100644 app/src/androidTest/java/com/mutualmobile/barricade/ExampleInstrumentedTest.kt delete mode 100644 app/src/androidTest/java/com/mutualmobile/barricade/sample/BarricadeActivityTest.java delete mode 100644 app/src/androidTest/java/com/mutualmobile/barricade/sample/MainActivityTest.java delete mode 100644 app/src/androidTest/java/com/mutualmobile/barricade/sample/utils/RecyclerViewMatcher.java delete mode 100644 app/src/main/java/com/mutualmobile/barricade/sample/BarricadeSampleApplication.java delete mode 100644 app/src/main/java/com/mutualmobile/barricade/sample/MainActivity.java delete mode 100644 app/src/main/java/com/mutualmobile/barricade/sample/api/ChuckNorrisApiService.java delete mode 100644 app/src/main/java/com/mutualmobile/barricade/sample/api/model/Joke.java delete mode 100644 app/src/main/java/com/mutualmobile/barricade/sample/api/util/ApiUtils.java create mode 100644 app/src/main/java/com/mutualmobile/barricadeSample/B2Application.kt create mode 100644 app/src/main/java/com/mutualmobile/barricadeSample/MainActivity.kt create mode 100644 app/src/main/java/com/mutualmobile/barricadeSample/data/ApiService.kt create mode 100644 app/src/main/java/com/mutualmobile/barricadeSample/data/JokeApi.kt create mode 100644 app/src/main/java/com/mutualmobile/barricadeSample/data/models/JokeResponse.kt create mode 100644 app/src/main/java/com/mutualmobile/barricadeSample/states/ResponseState.kt create mode 100644 app/src/main/java/com/mutualmobile/barricadeSample/ui/screens/LandingScreen.kt create mode 100644 app/src/main/java/com/mutualmobile/barricadeSample/ui/theme/Color.kt create mode 100644 app/src/main/java/com/mutualmobile/barricadeSample/ui/theme/Shape.kt create mode 100644 app/src/main/java/com/mutualmobile/barricadeSample/ui/theme/Theme.kt create mode 100644 app/src/main/java/com/mutualmobile/barricadeSample/ui/theme/Type.kt create mode 100644 app/src/main/java/com/mutualmobile/barricadeSample/viewmodels/LandingScreenVM.kt create mode 100644 app/src/main/res/drawable-v24/ic_launcher_foreground.xml create mode 100644 app/src/main/res/drawable/ic_launcher_background.xml delete mode 100644 app/src/main/res/drawable/ic_settings.xml delete mode 100644 app/src/main/res/layout/activity_main.xml delete mode 100644 app/src/main/res/menu/menu_main.xml create mode 100644 app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml create mode 100644 app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml delete mode 100644 app/src/main/res/mipmap-hdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-hdpi/ic_launcher.webp create mode 100644 app/src/main/res/mipmap-hdpi/ic_launcher_round.webp delete mode 100644 app/src/main/res/mipmap-mdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-mdpi/ic_launcher.webp create mode 100644 app/src/main/res/mipmap-mdpi/ic_launcher_round.webp delete mode 100644 app/src/main/res/mipmap-xhdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xhdpi/ic_launcher.webp create mode 100644 app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp delete mode 100644 app/src/main/res/mipmap-xxhdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xxhdpi/ic_launcher.webp create mode 100644 app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp delete mode 100644 app/src/main/res/mipmap-xxxhdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp create mode 100644 app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp delete mode 100644 app/src/main/res/values-w820dp/dimens.xml delete mode 100644 app/src/main/res/values/dimens.xml delete mode 100644 app/src/main/res/values/styles.xml create mode 100644 app/src/main/res/values/themes.xml create mode 100644 app/src/test/java/com/mutualmobile/barricade/ExampleUnitTest.kt delete mode 100644 app/src/test/java/com/mutualmobile/barricade/sample/api/ChuckNorrisApiServiceTest.java delete mode 100644 barricade-annotations/src/main/java/com/mutualmobile/barricade/IBarricadeConfig.java create mode 100644 barricade-annotations/src/main/java/com/mutualmobile/barricade/IBarricadeConfig.kt delete mode 100644 barricade-annotations/src/main/java/com/mutualmobile/barricade/annotation/Barricade.java create mode 100644 barricade-annotations/src/main/java/com/mutualmobile/barricade/annotation/Barricade.kt delete mode 100644 barricade-annotations/src/main/java/com/mutualmobile/barricade/annotation/Response.java create mode 100644 barricade-annotations/src/main/java/com/mutualmobile/barricade/annotation/Response.kt delete mode 100644 barricade-annotations/src/main/java/com/mutualmobile/barricade/response/BarricadeResponse.java create mode 100644 barricade-annotations/src/main/java/com/mutualmobile/barricade/response/BarricadeResponse.kt delete mode 100644 barricade-annotations/src/main/java/com/mutualmobile/barricade/response/BarricadeResponseSet.java create mode 100644 barricade-annotations/src/main/java/com/mutualmobile/barricade/response/BarricadeResponseSet.kt delete mode 100644 barricade-compiler/src/main/java/com/mutualmobile/barricade/compiler/BarricadeProcessor.java create mode 100644 barricade-compiler/src/main/java/com/mutualmobile/barricade/compiler/BarricadeProcessor.kt create mode 100644 barricade-compiler/src/main/java/com/mutualmobile/barricade/compiler/BarricadeProcessorProvider.kt delete mode 100755 barricade-compiler/src/main/java/com/mutualmobile/barricade/compiler/CodeGenerator.java delete mode 100644 barricade-compiler/src/main/java/com/mutualmobile/barricade/compiler/StringUtils.java create mode 100644 barricade-compiler/src/main/java/com/mutualmobile/barricade/compiler/utils/ExtensionUtils.kt create mode 100644 barricade-compiler/src/main/java/com/mutualmobile/barricade/compiler/visitors/BarricadeVisitor.kt create mode 100644 barricade-compiler/src/main/resources/META-INF/services/com.google.devtools.ksp.processing.SymbolProcessorProvider create mode 100644 barricade/consumer-rules.pro delete mode 100644 barricade/src/androidTest/java/com/mutualmobile/barricade/ApplicationTest.java create mode 100644 barricade/src/androidTest/java/com/mutualmobile/barricade/ExampleInstrumentedTest.kt delete mode 100644 barricade/src/main/java/com/mutualmobile/barricade/Barricade.java create mode 100644 barricade/src/main/java/com/mutualmobile/barricade/Barricade.kt create mode 100644 barricade/src/main/java/com/mutualmobile/barricade/BarricadeConfigActivity.kt delete mode 100644 barricade/src/main/java/com/mutualmobile/barricade/BarricadeInterceptor.java create mode 100644 barricade/src/main/java/com/mutualmobile/barricade/BarricadeInterceptor.kt delete mode 100644 barricade/src/main/java/com/mutualmobile/barricade/activity/BarricadeActivity.java delete mode 100644 barricade/src/main/java/com/mutualmobile/barricade/adapter/BarricadeEndpointsRVAdapter.java delete mode 100644 barricade/src/main/java/com/mutualmobile/barricade/adapter/BarricadeResponsesRVAdapter.java create mode 100644 barricade/src/main/java/com/mutualmobile/barricade/ui/screens/BarricadeConfigScreen.kt create mode 100644 barricade/src/main/java/com/mutualmobile/barricade/ui/theme/Color.kt create mode 100644 barricade/src/main/java/com/mutualmobile/barricade/ui/theme/Shape.kt create mode 100644 barricade/src/main/java/com/mutualmobile/barricade/ui/theme/Theme.kt create mode 100644 barricade/src/main/java/com/mutualmobile/barricade/ui/theme/Type.kt delete mode 100644 barricade/src/main/java/com/mutualmobile/barricade/utils/AndroidAssetFileManager.java create mode 100644 barricade/src/main/java/com/mutualmobile/barricade/utils/AndroidAssetFileManager.kt rename barricade/src/main/java/com/mutualmobile/barricade/utils/{AssetFileManager.java => AssetFileManager.kt} (53%) delete mode 100644 barricade/src/main/java/com/mutualmobile/barricade/utils/BarricadeShakeListener.java create mode 100644 barricade/src/main/java/com/mutualmobile/barricade/utils/ContextExtensions.kt delete mode 100644 barricade/src/main/java/com/mutualmobile/barricade/utils/TestAssetFileManager.java delete mode 100644 barricade/src/main/res/drawable/ic_done_red.xml rename barricade/src/main/res/drawable/{ic_timer_white.xml => ic_timer.xml} (73%) delete mode 100644 barricade/src/main/res/layout/activity_barricade.xml delete mode 100644 barricade/src/main/res/layout/dialog_edit_global_delay.xml delete mode 100644 barricade/src/main/res/layout/layout_barricade_response_list_item.xml delete mode 100644 barricade/src/main/res/menu/main.xml delete mode 100644 barricade/src/main/res/values/colors.xml create mode 100644 barricade/src/main/res/values/themes.xml create mode 100644 barricade/src/test/java/com/mutualmobile/barricade/ExampleUnitTest.kt diff --git a/app/.gitignore b/app/.gitignore index 796b96d..42afabf 100644 --- a/app/.gitignore +++ b/app/.gitignore @@ -1 +1 @@ -/build +/build \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index 00c60b3..74ae30d 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,82 +1,84 @@ -apply plugin: 'com.android.application' - -buildscript { - repositories { - jcenter() - mavenCentral() - } +plugins { + id 'com.android.application' + id 'org.jetbrains.kotlin.android' + id 'com.google.devtools.ksp' version "1.6.10-1.0.4" } android { - compileSdkVersion 28 - buildToolsVersion '28.0.3' + compileSdk 32 + + defaultConfig { + applicationId "com.mutualmobile.barricade" + minSdk 21 + targetSdk 32 + versionCode 1 + versionName "1.0" - defaultConfig { - applicationId "com.mutualmobile.barricade.sample" - minSdkVersion 16 - targetSdkVersion 28 - versionCode 1 - versionName "1.0" - testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" - } - buildTypes { - release { - minifyEnabled true - proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + vectorDrawables { + useSupportLibrary true + } } - } - testOptions { - unitTests.all { - // All the usual Gradle options. - testLogging { - events "passed", "skipped", "failed", "standardOut", "standardError" - outputs.upToDateWhen { false } - showStandardStreams = true - } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + kotlin { + kotlinOptions { + freeCompilerArgs += "-Xopt-in=kotlin.RequiresOptIn" + } + } + } + kotlinOptions { + jvmTarget = '1.8' + } + buildFeatures { + compose true + } + composeOptions { + kotlinCompilerExtensionVersion compose_version + } + packagingOptions { + resources { + excludes += '/META-INF/{AL2.0,LGPL2.1}' + } + } + sourceSets { + debug { + kotlin { srcDir(file("build/generated/ksp/debug/kotlin")) } + } + release { + kotlin { srcDir(file("build/generated/ksp/release/kotlin")) } + } } - } } - - dependencies { - implementation "androidx.appcompat:appcompat:${SUPPORT_LIB_VERSION}" - implementation "androidx.cardview:cardview:${SUPPORT_LIB_VERSION}" - - implementation project(':barricade') - annotationProcessor project(':barricade-compiler') - - implementation "com.squareup.okhttp3:logging-interceptor:${OKHTTP_VERSION}" - - implementation "com.squareup.retrofit2:retrofit:${RETROFIT_VERSION}" - implementation "com.squareup.retrofit2:converter-gson:${RETROFIT_VERSION}" - testImplementation 'junit:junit:4.12' - testImplementation 'com.google.truth:truth:0.35' + implementation 'androidx.core:core-ktx:1.7.0' + implementation "androidx.compose.ui:ui:$compose_version" + implementation "androidx.compose.material:material:$compose_version" + implementation "androidx.compose.ui:ui-tooling-preview:$compose_version" + implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1' + implementation 'androidx.activity:activity-compose:1.3.1' + testImplementation 'junit:junit:4.13.2' + androidTestImplementation 'androidx.test.ext:junit:1.1.3' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' + androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version" + debugImplementation "androidx.compose.ui:ui-tooling:$compose_version" + implementation implementation(project(':barricade',)) - androidTestImplementation "androidx.test.espresso:espresso-core:${ESPRESSO_VERSION}" - androidTestImplementation "androidx.test:runner:${RUNNER_VERSION}" - androidTestImplementation "androidx.annotation:annotation:${SUPPORT_LIB_VERSION}" + implementation(project(":barricade-annotations")) + ksp(project(":barricade-compiler")) - androidTestImplementation 'com.jakewharton.espresso:okhttp3-idling-resource:1.0.0' - androidTestImplementation "com.squareup.okhttp3:okhttp:${OKHTTP_VERSION}" - androidTestImplementation('androidx.test.espresso:espresso-contrib:3.1.0-alpha4') { - // Necessary to avoid version conflicts - exclude group: 'com.android.support', module: 'appcompat' - exclude group: 'com.android.support', module: 'support-v4' - exclude group: 'com.android.support', module: 'support-annotations' - exclude module: 'recyclerview-v7' - } - implementation project(path: ':barricade-annotations') -} - - -configurations.all { - resolutionStrategy { - // cache dynamic versions for 10 minutes - cacheDynamicVersionsFor 10*60, 'seconds' - // don't cache changing modules at all - cacheChangingModulesFor 0, 'seconds' - } -} + implementation 'com.squareup.retrofit2:retrofit:2.9.0' + implementation 'com.squareup.retrofit2:converter-gson:2.9.0' + implementation("com.squareup.okhttp3:logging-interceptor:4.9.3") + implementation("io.coil-kt:coil-compose:2.0.0-rc01") +} \ No newline at end of file diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro index c4ec211..481bb43 100644 --- a/app/proguard-rules.pro +++ b/app/proguard-rules.pro @@ -1,30 +1,21 @@ # Add project specific ProGuard rules here. -# By default, the flags in this file are appended to flags specified -# in /Users/mustafa/Development/android-sdk-macosx/tools/proguard/proguard-android.txt -# You can edit the include path and order by changing the proguardFiles -# directive in build.gradle. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. # # For more details, see # http://developer.android.com/guide/developing/tools/proguard.html -# Add any project specific keep options here: - # If your project uses WebView with JS, uncomment the following # and specify the fully qualified class name to the JavaScript interface # class: #-keepclassmembers class fqcn.of.javascript.interface.for.webview { # public *; #} --dontwarn okio.** -# Platform calls Class.forName on types which do not exist on Android to determine platform. --dontnote retrofit2.Platform -# Platform used when running on Java 8 VMs. Will not be used at runtime. --dontwarn retrofit2.Platform$Java8 -# Retain generic type information for use by reflection by converters and adapters. --keepattributes Signature -# Retain declared checked exceptions for use by a Proxy instance. --keepattributes Exceptions --dontwarn okhttp3.** --dontwarn okio.** --dontwarn retrofit2.** \ No newline at end of file +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile \ No newline at end of file diff --git a/app/src/androidTest/java/com/mutualmobile/barricade/ExampleInstrumentedTest.kt b/app/src/androidTest/java/com/mutualmobile/barricade/ExampleInstrumentedTest.kt new file mode 100644 index 0000000..f24e7c1 --- /dev/null +++ b/app/src/androidTest/java/com/mutualmobile/barricade/ExampleInstrumentedTest.kt @@ -0,0 +1,22 @@ +package com.mutualmobile.barricade + +import androidx.test.ext.junit.runners.AndroidJUnit4 +import androidx.test.platform.app.InstrumentationRegistry +import org.junit.Assert.assertEquals +import org.junit.Test +import org.junit.runner.RunWith + +/** + * Instrumented test, which will execute on an Android device. + * + * See [testing documentation](http://d.android.com/tools/testing). + */ +@RunWith(AndroidJUnit4::class) +class ExampleInstrumentedTest { + @Test + fun useAppContext() { + // Context of the app under test. + val appContext = InstrumentationRegistry.getInstrumentation().targetContext + assertEquals("com.mutualmobile.barricade", appContext.packageName) + } +} diff --git a/app/src/androidTest/java/com/mutualmobile/barricade/sample/BarricadeActivityTest.java b/app/src/androidTest/java/com/mutualmobile/barricade/sample/BarricadeActivityTest.java deleted file mode 100644 index 77cd5fb..0000000 --- a/app/src/androidTest/java/com/mutualmobile/barricade/sample/BarricadeActivityTest.java +++ /dev/null @@ -1,82 +0,0 @@ -package com.mutualmobile.barricade.sample; - -import androidx.test.InstrumentationRegistry; -import androidx.test.espresso.Espresso; -import androidx.test.espresso.contrib.RecyclerViewActions; -import androidx.test.filters.LargeTest; -import androidx.test.rule.ActivityTestRule; -import androidx.test.runner.AndroidJUnit4; -import com.mutualmobile.barricade.Barricade; -import com.mutualmobile.barricade.BarricadeConfig; -import com.mutualmobile.barricade.activity.BarricadeActivity; -import com.mutualmobile.barricade.response.BarricadeResponse; -import com.mutualmobile.barricade.response.BarricadeResponseSet; -import com.mutualmobile.barricade.utils.AndroidAssetFileManager; -import java.util.Map; -import org.junit.BeforeClass; -import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; - -import static androidx.test.espresso.Espresso.onView; -import static androidx.test.espresso.action.ViewActions.click; -import static androidx.test.espresso.assertion.ViewAssertions.matches; -import static androidx.test.espresso.matcher.ViewMatchers.hasDescendant; -import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed; -import static androidx.test.espresso.matcher.ViewMatchers.withId; -import static androidx.test.espresso.matcher.ViewMatchers.withText; -import static com.mutualmobile.barricade.sample.utils.RecyclerViewMatcher.withRecyclerView; - -/** - * Contains Espresso tests for the Barricade Activity UI. We verify the response and it's corresponding response sets in the UI. Functionality of - * delay and reset is verified as well - */ - -@RunWith(AndroidJUnit4.class) @LargeTest public class BarricadeActivityTest { - - @Rule public ActivityTestRule activityTestRule = new ActivityTestRule<>(BarricadeActivity.class); - - private static Barricade barricade; - - @BeforeClass public static void setup() { - barricade = new Barricade.Builder(BarricadeConfig.getInstance(), new AndroidAssetFileManager(InstrumentationRegistry.getTargetContext())). - install(); - } - - @Test public void verifyEndpoints() { - int count = 0; - for (String endpoint : barricade.getConfig().keySet()) { - onView(withRecyclerView(com.mutualmobile.barricade.R.id.endpoint_rv).atPosition(count)).check(matches(hasDescendant(withText(endpoint)))); - count++; - } - } - - @Test public void verifyResponsesForEndpoints() { - int endpointCount = 0; - Map hashMap = barricade.getConfig(); - for (String endpoint : hashMap.keySet()) { - int responseCount = 0; - onView(withId(R.id.endpoint_rv)).perform(RecyclerViewActions.actionOnItemAtPosition(endpointCount, click())); - for (BarricadeResponse response : hashMap.get(endpoint).responses) { - onView(withRecyclerView(com.mutualmobile.barricade.R.id.endpoint_responses_rv).atPosition(responseCount)).check( - matches(hasDescendant(withText(response.responseFileName)))); - responseCount++; - } - Espresso.pressBack(); - } - } - - @Test public void verifyDelayTimeDialogShowsCorrectDelay() { - onView(withId(R.id.menu_delay)).perform(click()); - onView(withId(R.id.delay_value_edittext)).check(matches(withText(Long.toString(barricade.getDelay())))); - onView(withId(android.R.id.button1)).check(matches(withText(R.string.set))); - onView(withId(android.R.id.button2)).check(matches(withText(R.string.cancel))); - } - - @Test public void verifyResetDialogIsDisplayed() { - onView(withId(R.id.menu_reset)).perform(click()); - onView(withText(R.string.reset_message)).check(matches(isDisplayed())); - onView(withId(android.R.id.button1)).check(matches(withText(R.string.yes))); - onView(withId(android.R.id.button2)).check(matches(withText(R.string.no))); - } -} diff --git a/app/src/androidTest/java/com/mutualmobile/barricade/sample/MainActivityTest.java b/app/src/androidTest/java/com/mutualmobile/barricade/sample/MainActivityTest.java deleted file mode 100644 index ad37902..0000000 --- a/app/src/androidTest/java/com/mutualmobile/barricade/sample/MainActivityTest.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.mutualmobile.barricade.sample; - -import androidx.test.espresso.Espresso; -import androidx.test.espresso.IdlingResource; -import androidx.test.filters.LargeTest; -import androidx.test.rule.ActivityTestRule; -import androidx.test.runner.AndroidJUnit4; -import com.jakewharton.espresso.OkHttp3IdlingResource; -import com.mutualmobile.barricade.Barricade; -import com.mutualmobile.barricade.BarricadeConfig; -import com.mutualmobile.barricade.sample.api.util.ApiUtils; -import org.junit.After; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; - -import static androidx.test.espresso.Espresso.onView; -import static androidx.test.espresso.action.ViewActions.click; -import static androidx.test.espresso.assertion.ViewAssertions.matches; -import static androidx.test.espresso.matcher.ViewMatchers.withId; -import static androidx.test.espresso.matcher.ViewMatchers.withText; -import static org.hamcrest.Matchers.not; - -/** - * Contains Espresso tests for the UI. Using Barricade, we can set a response for an endpoint, exercise the API call and verify the UI behavior. - */ -@RunWith(AndroidJUnit4.class) @LargeTest public class MainActivityTest { - - @Rule public ActivityTestRule activityTestRule = new ActivityTestRule<>(MainActivity.class); - - private static final String JOKE_FROM_BARRICADE = "When Chuck Norris was a kid, he made his mom eat her vegetables"; - - @Before public void setup() { - IdlingResource resource = OkHttp3IdlingResource.create("OkHttp", ApiUtils.getOkHttpClient()); - Espresso.registerIdlingResources(resource); - } - - @After public void teardown() { - Barricade.getInstance().reset(); - } - - @Test public void verifyEnablingBarricade() { - onView(withId(R.id.barricade_switch)).perform(click()); - onView(withId(R.id.get_joke_button)).perform(click()); - onView(withId(R.id.joke_text)).check(matches(withText(JOKE_FROM_BARRICADE))); - } - - @Test public void verifyDisablingBarricade() { - onView(withId(R.id.get_joke_button)).perform(click()); - onView(withId(R.id.joke_text)).check(matches(not(withText(JOKE_FROM_BARRICADE)))); - } - - @Test public void verifyFailure() { - Barricade.getInstance().setResponse(BarricadeConfig.Endpoints.RANDOM, BarricadeConfig.Responses.Random.FAILURE); - onView(withId(R.id.barricade_switch)).perform(click()); - onView(withId(R.id.get_joke_button)).perform(click()); - onView(withId(R.id.joke_text)).check(matches(withText("Request failed : 401"))); - } -} \ No newline at end of file diff --git a/app/src/androidTest/java/com/mutualmobile/barricade/sample/utils/RecyclerViewMatcher.java b/app/src/androidTest/java/com/mutualmobile/barricade/sample/utils/RecyclerViewMatcher.java deleted file mode 100644 index 14d9598..0000000 --- a/app/src/androidTest/java/com/mutualmobile/barricade/sample/utils/RecyclerViewMatcher.java +++ /dev/null @@ -1,69 +0,0 @@ -package com.mutualmobile.barricade.sample.utils; - -import android.content.res.Resources; -import androidx.recyclerview.widget.RecyclerView; -import android.view.View; -import org.hamcrest.Description; -import org.hamcrest.Matcher; -import org.hamcrest.TypeSafeMatcher; - -/** - * Used for Matching contents of a RecyclerView item by position - */ -public class RecyclerViewMatcher { - private final int recyclerViewId; - - public RecyclerViewMatcher(int recyclerViewId) { - this.recyclerViewId = recyclerViewId; - } - - public Matcher atPosition(final int position) { - return atPositionOnView(position, -1); - } - - private Matcher atPositionOnView(final int position, final int targetViewId) { - - return new TypeSafeMatcher() { - Resources resources = null; - View childView; - - public void describeTo(Description description) { - String idDescription = Integer.toString(recyclerViewId); - if (this.resources != null) { - try { - idDescription = this.resources.getResourceName(recyclerViewId); - } catch (Resources.NotFoundException var4) { - idDescription = String.format("%s (resource name not found)", recyclerViewId); - } - } - - description.appendText("with id: " + idDescription); - } - - public boolean matchesSafely(View view) { - - this.resources = view.getResources(); - - if (childView == null) { - RecyclerView recyclerView = (RecyclerView) view.getRootView().findViewById(recyclerViewId); - if (recyclerView != null && recyclerView.getId() == recyclerViewId) { - childView = recyclerView.findViewHolderForAdapterPosition(position).itemView; - } else { - return false; - } - } - - if (targetViewId == -1) { - return view == childView; - } else { - View targetView = childView.findViewById(targetViewId); - return view == targetView; - } - } - }; - } - - public static RecyclerViewMatcher withRecyclerView(final int recyclerViewId) { - return new RecyclerViewMatcher(recyclerViewId); - } -} \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index d4af77c..9fe70ba 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,23 +1,27 @@ + package="com.mutualmobile.barricadeSample"> - + - - - - + + + + - - - - + + + + \ No newline at end of file diff --git a/app/src/main/java/com/mutualmobile/barricade/sample/BarricadeSampleApplication.java b/app/src/main/java/com/mutualmobile/barricade/sample/BarricadeSampleApplication.java deleted file mode 100644 index 39de2b4..0000000 --- a/app/src/main/java/com/mutualmobile/barricade/sample/BarricadeSampleApplication.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.mutualmobile.barricade.sample; - -import android.app.Application; -import com.mutualmobile.barricade.Barricade; -import com.mutualmobile.barricade.BarricadeConfig; - -public class BarricadeSampleApplication extends Application { - @Override public void onCreate() { - super.onCreate(); - new Barricade.Builder(this, BarricadeConfig.getInstance()).enableShakeToStart(this).install(); - } -} diff --git a/app/src/main/java/com/mutualmobile/barricade/sample/MainActivity.java b/app/src/main/java/com/mutualmobile/barricade/sample/MainActivity.java deleted file mode 100644 index a4cf392..0000000 --- a/app/src/main/java/com/mutualmobile/barricade/sample/MainActivity.java +++ /dev/null @@ -1,124 +0,0 @@ -package com.mutualmobile.barricade.sample; - -import android.os.Bundle; -import androidx.appcompat.app.AppCompatActivity; -import android.util.Log; -import android.view.Menu; -import android.view.MenuInflater; -import android.view.MenuItem; -import android.view.View; -import android.widget.CompoundButton; -import android.widget.ProgressBar; -import android.widget.Switch; -import android.widget.TextView; -import com.mutualmobile.barricade.Barricade; -import com.mutualmobile.barricade.sample.api.ChuckNorrisApiService; -import com.mutualmobile.barricade.sample.api.model.Joke; -import com.mutualmobile.barricade.sample.api.util.ApiUtils; -import java.util.List; -import retrofit2.Call; -import retrofit2.Callback; -import retrofit2.Response; - -public class MainActivity extends AppCompatActivity { - - private final String TAG = "BARRICADE_SAMPLE"; - private ChuckNorrisApiService chuckNorrisApiService; - private ProgressBar progressBar; - private TextView jokeTextView; - - @Override protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_main); - initUi(); - chuckNorrisApiService = ApiUtils.getApiService(); - } - - @Override public boolean onCreateOptionsMenu(Menu menu) { - MenuInflater inflater = getMenuInflater(); - inflater.inflate(R.menu.menu_main, menu); - return true; - } - - @Override public boolean onOptionsItemSelected(MenuItem item) { - if(item.getItemId() == R.id.config) { - Barricade.getInstance().launchConfigActivity(this); - return true; - } - return super.onOptionsItemSelected(item); - } - - private void initUi() { - Switch barricadeSwitch = (Switch) findViewById(R.id.barricade_switch); - checkChanged(barricadeSwitch.isChecked()); - barricadeSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { - @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - checkChanged(isChecked); - } - }); - - progressBar = (ProgressBar) findViewById(R.id.progress_bar); - findViewById(R.id.get_joke_button).setOnClickListener(new View.OnClickListener() { - @Override public void onClick(View view) { - onJokeButtonClicked(); - } - }); - jokeTextView = (TextView) findViewById(R.id.joke_text); - findViewById(R.id.get_joke_categories_button).setOnClickListener(new View.OnClickListener() { - @Override public void onClick(View view) { - onJokeCategoriesButtonClicked(); - } - }); - } - - private void onJokeButtonClicked() { - showProgress(true); - chuckNorrisApiService.getRandomJoke().enqueue(new Callback() { - @Override public void onResponse(Call call, Response response) { - if (response.isSuccessful()) { - jokeTextView.setText(response.body().value); - } else { - jokeTextView.setText("Request failed : " + response.code()); - } - showProgress(false); - } - - @Override public void onFailure(Call call, Throwable t) { - Log.e(TAG, "(╯°□°)╯︵ ┻━┻", t); - showProgress(false); - } - }); - } - - private void onJokeCategoriesButtonClicked() { - showProgress(true); - chuckNorrisApiService.getJokeCategories().enqueue(new Callback>() { - @Override public void onResponse(Call> call, Response> response) { - if (response.isSuccessful()) { - StringBuilder str = new StringBuilder("Categories:\n"); - for(String category: response.body()) { - str.append(category).append("\n"); - } - jokeTextView.setText(str); - } else { - jokeTextView.setText("Request failed : " + response.code()); - } - showProgress(false); - } - - @Override public void onFailure(Call> call, Throwable t) { - Log.e(TAG, "(╯°□°)╯︵ ┻━┻", t); - showProgress(false); - } - }); - } - - private void showProgress(boolean show) { - progressBar.setVisibility(show ? View.VISIBLE : View.INVISIBLE); - } - - private void checkChanged(boolean isChecked) { - Barricade.getInstance().setEnabled(isChecked); - } - -} diff --git a/app/src/main/java/com/mutualmobile/barricade/sample/api/ChuckNorrisApiService.java b/app/src/main/java/com/mutualmobile/barricade/sample/api/ChuckNorrisApiService.java deleted file mode 100644 index 2b13567..0000000 --- a/app/src/main/java/com/mutualmobile/barricade/sample/api/ChuckNorrisApiService.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.mutualmobile.barricade.sample.api; - -import com.mutualmobile.barricade.annotation.Barricade; -import com.mutualmobile.barricade.annotation.Response; -import com.mutualmobile.barricade.sample.api.model.Joke; - -import java.util.List; -import retrofit2.Call; -import retrofit2.http.GET; - -public interface ChuckNorrisApiService { - - @GET("/jokes/random") @Barricade(endpoint = "random", responses = { - @Response(fileName = "success.json",isDefault = true), - @Response(fileName = "failure.json", statusCode = 401) - }) Call getRandomJoke(); - - @GET("/jokes/categories") Call> getJokeCategories(); -} diff --git a/app/src/main/java/com/mutualmobile/barricade/sample/api/model/Joke.java b/app/src/main/java/com/mutualmobile/barricade/sample/api/model/Joke.java deleted file mode 100644 index a5d6332..0000000 --- a/app/src/main/java/com/mutualmobile/barricade/sample/api/model/Joke.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.mutualmobile.barricade.sample.api.model; - -import com.google.gson.annotations.SerializedName; - -public class Joke { - public String id; - public String value; - @SerializedName("icon_url") - public String iconUrl; - public String url; - - @Override public String toString() { - return "Joke{\n" - + "id=" - + '\'' - +id - +'\'' - +"\nJoke=" - +'\'' - +value - +'\'' - + "}\n\n"; - } -} diff --git a/app/src/main/java/com/mutualmobile/barricade/sample/api/util/ApiUtils.java b/app/src/main/java/com/mutualmobile/barricade/sample/api/util/ApiUtils.java deleted file mode 100644 index 8ffce0a..0000000 --- a/app/src/main/java/com/mutualmobile/barricade/sample/api/util/ApiUtils.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.mutualmobile.barricade.sample.api.util; - -import com.mutualmobile.barricade.BarricadeInterceptor; -import com.mutualmobile.barricade.sample.api.ChuckNorrisApiService; -import okhttp3.OkHttpClient; -import okhttp3.logging.HttpLoggingInterceptor; -import retrofit2.Retrofit; -import retrofit2.converter.gson.GsonConverterFactory; - -public class ApiUtils { - - private static OkHttpClient okHttpClient; - - public static ChuckNorrisApiService getApiService() { - HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(); - httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY); - - okHttpClient = new OkHttpClient.Builder().addInterceptor(new BarricadeInterceptor()).addInterceptor(httpLoggingInterceptor).build(); - - Retrofit retrofit = - new Retrofit.Builder().baseUrl("https://api.chucknorris.io").client(okHttpClient).addConverterFactory(GsonConverterFactory.create()).build(); - - return retrofit.create(ChuckNorrisApiService.class); - } - - public static OkHttpClient getOkHttpClient() { - return okHttpClient; - } -} diff --git a/app/src/main/java/com/mutualmobile/barricadeSample/B2Application.kt b/app/src/main/java/com/mutualmobile/barricadeSample/B2Application.kt new file mode 100644 index 0000000..d9d127b --- /dev/null +++ b/app/src/main/java/com/mutualmobile/barricadeSample/B2Application.kt @@ -0,0 +1,12 @@ +package com.mutualmobile.barricadeSample + +import android.app.Application +import com.mutualmobile.barricade.Barricade +import com.mutualmobile.barricade.BarricadeConfig + +class B2Application : Application() { + override fun onCreate() { + super.onCreate() + Barricade.Builder(this, BarricadeConfig.getInstance()).install() + } +} diff --git a/app/src/main/java/com/mutualmobile/barricadeSample/MainActivity.kt b/app/src/main/java/com/mutualmobile/barricadeSample/MainActivity.kt new file mode 100644 index 0000000..c157fe8 --- /dev/null +++ b/app/src/main/java/com/mutualmobile/barricadeSample/MainActivity.kt @@ -0,0 +1,42 @@ +package com.mutualmobile.barricadeSample + +import android.os.Bundle +import android.util.Log +import androidx.activity.ComponentActivity +import androidx.activity.compose.setContent +import androidx.activity.viewModels +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.material.MaterialTheme +import androidx.compose.material.Surface +import androidx.compose.ui.Modifier +import androidx.lifecycle.lifecycleScope +import com.mutualmobile.barricadeSample.data.JokeApi +import com.mutualmobile.barricadeSample.ui.screens.LandingScreen +import com.mutualmobile.barricadeSample.ui.theme.barricadeTheme +import com.mutualmobile.barricadeSample.viewmodels.LandingScreenVM + +private const val TAG = "MainActivity" + +class MainActivity : ComponentActivity() { + private val landingScreenVM by viewModels() + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + lifecycleScope.launchWhenResumed { + JokeApi.instance.getRandomJoke().body()?.let { nnJokeBody -> + Log.d(TAG, "onCreate: $nnJokeBody") + } + } + setContent { + barricadeTheme { + // A surface container using the 'background' color from the theme + Surface( + modifier = Modifier.fillMaxSize(), + color = MaterialTheme.colors.background + ) { + LandingScreen(landingScreenVM) + } + } + } + } +} diff --git a/app/src/main/java/com/mutualmobile/barricadeSample/data/ApiService.kt b/app/src/main/java/com/mutualmobile/barricadeSample/data/ApiService.kt new file mode 100644 index 0000000..51184ab --- /dev/null +++ b/app/src/main/java/com/mutualmobile/barricadeSample/data/ApiService.kt @@ -0,0 +1,26 @@ +package com.mutualmobile.barricadeSample.data + +import com.mutualmobile.barricade.annotation.Barricade +import com.mutualmobile.barricadeSample.data.models.JokeResponse +import retrofit2.Response +import retrofit2.http.GET + +interface ApiService { + @GET("/jokes/random") + @Barricade( + responses = [ + com.mutualmobile.barricade.annotation.Response( + fileName = "success.json", + isDefault = true + ), + com.mutualmobile.barricade.annotation.Response( + fileName = "failure.json", + statusCode = 404 + ) + ] + ) + suspend fun getRandomJoke(): Response + + @GET("/jokes/categories") + suspend fun getJokeCategories(): Response> +} diff --git a/app/src/main/java/com/mutualmobile/barricadeSample/data/JokeApi.kt b/app/src/main/java/com/mutualmobile/barricadeSample/data/JokeApi.kt new file mode 100644 index 0000000..e27d811 --- /dev/null +++ b/app/src/main/java/com/mutualmobile/barricadeSample/data/JokeApi.kt @@ -0,0 +1,29 @@ +package com.mutualmobile.barricadeSample.data + +import com.google.gson.Gson +import com.google.gson.GsonBuilder +import com.mutualmobile.barricade.BarricadeInterceptor +import okhttp3.OkHttpClient +import okhttp3.logging.HttpLoggingInterceptor +import retrofit2.Retrofit +import retrofit2.converter.gson.GsonConverterFactory +import retrofit2.create + +object JokeApi { + private val loggingInterceptor = HttpLoggingInterceptor() + .setLevel(HttpLoggingInterceptor.Level.BODY) + + private val client = OkHttpClient.Builder() + .addInterceptor(loggingInterceptor) + .addInterceptor(BarricadeInterceptor()) + .build() + + private val gson: Gson = GsonBuilder().setLenient().create() + + val instance = Retrofit.Builder() + .baseUrl("https://api.chucknorris.io") + .client(client) + .addConverterFactory(GsonConverterFactory.create(gson)) + .build() + .create() +} diff --git a/app/src/main/java/com/mutualmobile/barricadeSample/data/models/JokeResponse.kt b/app/src/main/java/com/mutualmobile/barricadeSample/data/models/JokeResponse.kt new file mode 100644 index 0000000..07ea21c --- /dev/null +++ b/app/src/main/java/com/mutualmobile/barricadeSample/data/models/JokeResponse.kt @@ -0,0 +1,8 @@ +package com.mutualmobile.barricadeSample.data.models + +data class JokeResponse( + val icon_url: String, + val id: String, + val url: String, + val value: String +) diff --git a/app/src/main/java/com/mutualmobile/barricadeSample/states/ResponseState.kt b/app/src/main/java/com/mutualmobile/barricadeSample/states/ResponseState.kt new file mode 100644 index 0000000..38731e7 --- /dev/null +++ b/app/src/main/java/com/mutualmobile/barricadeSample/states/ResponseState.kt @@ -0,0 +1,8 @@ +package com.mutualmobile.barricadeSample.states + +sealed class ResponseState { + object Empty : ResponseState() + object Loading : ResponseState() + class Success(val data: T) : ResponseState() + class Failure(val reason: String) : ResponseState() +} diff --git a/app/src/main/java/com/mutualmobile/barricadeSample/ui/screens/LandingScreen.kt b/app/src/main/java/com/mutualmobile/barricadeSample/ui/screens/LandingScreen.kt new file mode 100644 index 0000000..1bd6319 --- /dev/null +++ b/app/src/main/java/com/mutualmobile/barricadeSample/ui/screens/LandingScreen.kt @@ -0,0 +1,181 @@ +package com.mutualmobile.barricadeSample.ui.screens + +import androidx.compose.animation.AnimatedContent +import androidx.compose.animation.ExperimentalAnimationApi +import androidx.compose.animation.fadeIn +import androidx.compose.animation.fadeOut +import androidx.compose.animation.slideInVertically +import androidx.compose.animation.with +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.size +import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.material.Button +import androidx.compose.material.Card +import androidx.compose.material.Icon +import androidx.compose.material.IconButton +import androidx.compose.material.LinearProgressIndicator +import androidx.compose.material.MaterialTheme +import androidx.compose.material.Scaffold +import androidx.compose.material.Switch +import androidx.compose.material.Text +import androidx.compose.material.TopAppBar +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.filled.Settings +import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.layout.ContentScale +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.unit.dp +import coil.compose.AsyncImage +import com.mutualmobile.barricade.Barricade +import com.mutualmobile.barricadeSample.R +import com.mutualmobile.barricadeSample.states.ResponseState +import com.mutualmobile.barricadeSample.viewmodels.LandingScreenVM + +object LandingScreen { + const val HorizontalPadding = 16 + const val VerticalPadding = 4 + const val RoundedCornerPercent = 15 + const val ImageSize = 50 +} + +@OptIn(ExperimentalAnimationApi::class) +@Composable +fun LandingScreen( + landingScreenVM: LandingScreenVM +) { + val ctx = LocalContext.current + val barricade by remember { mutableStateOf(Barricade.getInstance()) } + val currentJokeState = landingScreenVM.jokeResponseState + val currentJokeCategoriesState = landingScreenVM.jokeCategoriesState + val isChecked = landingScreenVM.barricadeStatus + + Scaffold( + topBar = { + TopAppBar( + title = { Text(stringResource(id = R.string.app_name)) }, + actions = { + IconButton(onClick = { barricade.launchConfigActivity(ctx) }) { + Icon(Icons.Default.Settings, stringResource(R.string.configBtnDesc)) + } + } + ) + }, + ) { + Column( + horizontalAlignment = Alignment.CenterHorizontally + ) { + Row( + verticalAlignment = Alignment.CenterVertically, + horizontalArrangement = Arrangement.SpaceBetween, + modifier = Modifier + .fillMaxWidth() + .padding( + horizontal = LandingScreen.HorizontalPadding.dp, + vertical = LandingScreen.VerticalPadding.dp + ), + ) { + Text(stringResource(R.string.barricade)) + Switch(checked = isChecked, onCheckedChange = { switchChecked -> + landingScreenVM.setBarricadeEnabled(switchChecked) + }) + } + Button(onClick = { + landingScreenVM.fetchJoke() + }) { + Text(stringResource(R.string.getJoke)) + } + Button(onClick = { + landingScreenVM.fetchJokeCategories() + }) { + Text(stringResource(R.string.getJokeCategories)) + } + AnimatedContent( + targetState = currentJokeState, + transitionSpec = { fadeIn() + slideInVertically() with fadeOut() }, + modifier = Modifier.fillMaxWidth(), + contentAlignment = Alignment.Center + ) { jokeState -> + when (jokeState) { + is ResponseState.Empty -> {} + is ResponseState.Loading -> { + LinearProgressIndicator(modifier = Modifier.fillMaxWidth()) + } + is ResponseState.Success -> { + Row( + modifier = Modifier.padding(horizontal = LandingScreen.HorizontalPadding.dp) + ) { + Card( + shape = RoundedCornerShape(LandingScreen.RoundedCornerPercent), + modifier = Modifier.size(LandingScreen.ImageSize.dp) + ) { + AsyncImage( + model = jokeState.data.icon_url, + contentDescription = null, + modifier = Modifier.fillMaxSize(), + contentScale = ContentScale.Crop, + ) + } + Text( + jokeState.data.value, + modifier = Modifier + .padding( + horizontal = LandingScreen.HorizontalPadding.div(2).dp + ) + ) + } + } + is ResponseState.Failure -> { + Text(jokeState.reason, color = MaterialTheme.colors.error) + } + } + } + AnimatedContent( + targetState = currentJokeCategoriesState, + transitionSpec = { fadeIn() + slideInVertically() with fadeOut() }, + modifier = Modifier.fillMaxWidth(), + contentAlignment = Alignment.Center + ) { categoryState -> + when (categoryState) { + is ResponseState.Empty -> {} + is ResponseState.Loading -> { + LinearProgressIndicator(modifier = Modifier.fillMaxWidth()) + } + is ResponseState.Success -> { + LazyColumn( + modifier = Modifier + .fillMaxWidth() + .padding(horizontal = LandingScreen.HorizontalPadding.dp), + horizontalAlignment = Alignment.CenterHorizontally + ) { + categoryState.data.forEach { jokeCategory -> + item { + Text( + jokeCategory, + modifier = Modifier.padding( + horizontal = LandingScreen.HorizontalPadding.div(2).dp + ) + ) + } + } + } + } + is ResponseState.Failure -> { + Text(categoryState.reason, color = MaterialTheme.colors.error) + } + } + } + } + } +} diff --git a/app/src/main/java/com/mutualmobile/barricadeSample/ui/theme/Color.kt b/app/src/main/java/com/mutualmobile/barricadeSample/ui/theme/Color.kt new file mode 100644 index 0000000..5bdc77f --- /dev/null +++ b/app/src/main/java/com/mutualmobile/barricadeSample/ui/theme/Color.kt @@ -0,0 +1,8 @@ +package com.mutualmobile.barricadeSample.ui.theme + +import androidx.compose.ui.graphics.Color + +val Purple200 = Color(0xFFBB86FC) +val Purple500 = Color(0xFF6200EE) +val Purple700 = Color(0xFF3700B3) +val Teal200 = Color(0xFF03DAC5) diff --git a/app/src/main/java/com/mutualmobile/barricadeSample/ui/theme/Shape.kt b/app/src/main/java/com/mutualmobile/barricadeSample/ui/theme/Shape.kt new file mode 100644 index 0000000..c082aef --- /dev/null +++ b/app/src/main/java/com/mutualmobile/barricadeSample/ui/theme/Shape.kt @@ -0,0 +1,11 @@ +package com.mutualmobile.barricadeSample.ui.theme + +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.material.Shapes +import androidx.compose.ui.unit.dp + +val Shapes = Shapes( + small = RoundedCornerShape(4.dp), + medium = RoundedCornerShape(4.dp), + large = RoundedCornerShape(0.dp) +) diff --git a/app/src/main/java/com/mutualmobile/barricadeSample/ui/theme/Theme.kt b/app/src/main/java/com/mutualmobile/barricadeSample/ui/theme/Theme.kt new file mode 100644 index 0000000..dd5f0c0 --- /dev/null +++ b/app/src/main/java/com/mutualmobile/barricadeSample/ui/theme/Theme.kt @@ -0,0 +1,44 @@ +package com.mutualmobile.barricadeSample.ui.theme + +import androidx.compose.foundation.isSystemInDarkTheme +import androidx.compose.material.MaterialTheme +import androidx.compose.material.darkColors +import androidx.compose.material.lightColors +import androidx.compose.runtime.Composable + +private val DarkColorPalette = darkColors( + primary = Purple200, + primaryVariant = Purple700, + secondary = Teal200 +) + +private val LightColorPalette = lightColors( + primary = Purple500, + primaryVariant = Purple700, + secondary = Teal200 + + /* Other default colors to override + background = Color.White, + surface = Color.White, + onPrimary = Color.White, + onSecondary = Color.Black, + onBackground = Color.Black, + onSurface = Color.Black, + */ +) + +@Composable +fun barricadeTheme(darkTheme: Boolean = isSystemInDarkTheme(), content: @Composable () -> Unit) { + val colors = if (darkTheme) { + DarkColorPalette + } else { + LightColorPalette + } + + MaterialTheme( + colors = colors, + typography = Typography, + shapes = Shapes, + content = content + ) +} diff --git a/app/src/main/java/com/mutualmobile/barricadeSample/ui/theme/Type.kt b/app/src/main/java/com/mutualmobile/barricadeSample/ui/theme/Type.kt new file mode 100644 index 0000000..c92f94f --- /dev/null +++ b/app/src/main/java/com/mutualmobile/barricadeSample/ui/theme/Type.kt @@ -0,0 +1,28 @@ +package com.mutualmobile.barricadeSample.ui.theme + +import androidx.compose.material.Typography +import androidx.compose.ui.text.TextStyle +import androidx.compose.ui.text.font.FontFamily +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.unit.sp + +// Set of Material typography styles to start with +val Typography = Typography( + body1 = TextStyle( + fontFamily = FontFamily.Default, + fontWeight = FontWeight.Normal, + fontSize = 16.sp + ) + /* Other default text styles to override + button = TextStyle( + fontFamily = FontFamily.Default, + fontWeight = FontWeight.W500, + fontSize = 14.sp + ), + caption = TextStyle( + fontFamily = FontFamily.Default, + fontWeight = FontWeight.Normal, + fontSize = 12.sp + ) + */ +) diff --git a/app/src/main/java/com/mutualmobile/barricadeSample/viewmodels/LandingScreenVM.kt b/app/src/main/java/com/mutualmobile/barricadeSample/viewmodels/LandingScreenVM.kt new file mode 100644 index 0000000..f891dd4 --- /dev/null +++ b/app/src/main/java/com/mutualmobile/barricadeSample/viewmodels/LandingScreenVM.kt @@ -0,0 +1,65 @@ +package com.mutualmobile.barricadeSample.viewmodels + +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.setValue +import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import com.mutualmobile.barricade.Barricade +import com.mutualmobile.barricadeSample.data.JokeApi +import com.mutualmobile.barricadeSample.data.models.JokeResponse +import com.mutualmobile.barricadeSample.states.ResponseState +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch + +class LandingScreenVM : ViewModel() { + var barricadeStatus: Boolean by mutableStateOf(false) + private set + + var jokeResponseState: ResponseState by mutableStateOf(ResponseState.Empty) + private set + + var jokeCategoriesState: ResponseState> by mutableStateOf(ResponseState.Empty) + private set + + private val barricade = Barricade.getInstance() + + fun setBarricadeEnabled(isBarricadeEnabled: Boolean) { + barricade.isEnabled = isBarricadeEnabled + barricadeStatus = barricade.isEnabled + } + + fun fetchJoke() { + jokeCategoriesState = ResponseState.Empty + jokeResponseState = ResponseState.Loading + viewModelScope.launch(Dispatchers.IO) { + val networkResponse = JokeApi.instance.getRandomJoke() + if (networkResponse.isSuccessful) { + networkResponse.body()?.let { nnNetworkResponse -> + jokeResponseState = ResponseState.Success(data = nnNetworkResponse) + } ?: run { + jokeResponseState = ResponseState.Failure(reason = "Response body is empty") + } + } else { + jokeResponseState = ResponseState.Failure(reason = networkResponse.message()) + } + } + } + + fun fetchJokeCategories() { + jokeResponseState = ResponseState.Empty + jokeCategoriesState = ResponseState.Loading + viewModelScope.launch(Dispatchers.IO) { + val networkResponse = JokeApi.instance.getJokeCategories() + if (networkResponse.isSuccessful) { + networkResponse.body()?.let { nnNetworkResponse -> + jokeCategoriesState = ResponseState.Success(data = nnNetworkResponse) + } ?: run { + jokeCategoriesState = ResponseState.Failure(reason = "Response body is empty") + } + } else { + jokeCategoriesState = ResponseState.Failure(reason = networkResponse.message()) + } + } + } +} diff --git a/app/src/main/res/drawable-v24/ic_launcher_foreground.xml b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 0000000..2b068d1 --- /dev/null +++ b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_launcher_background.xml b/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..07d5da9 --- /dev/null +++ b/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/drawable/ic_settings.xml b/app/src/main/res/drawable/ic_settings.xml deleted file mode 100644 index ce997a7..0000000 --- a/app/src/main/res/drawable/ic_settings.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml deleted file mode 100644 index 47223ae..0000000 --- a/app/src/main/res/layout/activity_main.xml +++ /dev/null @@ -1,56 +0,0 @@ - - - - - -