Skip to content

Commit

Permalink
Add simple sample app (#38)
Browse files Browse the repository at this point in the history
Fixes #15
  • Loading branch information
mattmook authored Feb 22, 2021
1 parent 057c21d commit 2d4d5b3
Show file tree
Hide file tree
Showing 54 changed files with 1,480 additions and 3 deletions.
3 changes: 3 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ buildscript {
}
dependencies {
classpath("com.android.tools.build:gradle:${Versions.androidGradlePlugin}")
classpath("com.google.dagger:hilt-android-gradle-plugin:${Versions.Google.dagger}")
classpath("androidx.navigation:navigation-safe-args-gradle-plugin:${Versions.AndroidX.navigation}")
classpath("com.squareup.sqldelight:gradle-plugin:${Versions.sqlDelight}")
}
}

Expand Down
14 changes: 13 additions & 1 deletion buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ object Versions {
const val coroutines = "1.4.2"
const val diskLruCache = "2.0.2"
const val ehcache = "3.9.1"
const val groupie = "2.9.0"
const val ktor = "1.5.1"
const val orbitMvi = "3.0.1"
const val slf4j = "1.7.30"
const val sqlDelight = "1.4.4"
const val systemRules = "1.19.0"

const val junit4 = "4.13.2"
Expand All @@ -40,10 +44,16 @@ object Versions {
const val retrofit = "2.9.0"
const val robolectric = "4.5.1"

object AndroidX {
const val desugar = "1.1.1"

object AndroidX {
const val annotation = "1.1.0"
const val appCompat = "1.2.0"
const val constraintLayout = "2.1.0-alpha2"
const val fragment = "1.3.0"
const val hilt = "1.0.0-alpha02"
const val lifecycle = "2.3.0"
const val navigation = "2.3.3"
const val multidex = "2.0.1"
const val securityCrypto = "1.1.0-alpha03"

Expand All @@ -53,6 +63,8 @@ object Versions {
}

object Google {
const val dagger = "2.31.1-alpha"
const val material = "1.3.0-rc01"
const val tink = "1.5.0"
}

Expand Down
113 changes: 113 additions & 0 deletions samples/androidApp/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright 2021 Appmattus Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

plugins {
id("com.android.application")
kotlin("android")
id("kotlin-parcelize")
id("com.squareup.sqldelight")
kotlin("kapt")
id("androidx.navigation.safeargs.kotlin")
kotlin("plugin.serialization")
}

apply(plugin = "dagger.hilt.android.plugin")

dependencies {

implementation(project(":layercache"))
implementation(project(":layercache-android"))
implementation(project(":layercache-android-encryption"))

implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:${Versions.coroutines}")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:${Versions.coroutines}")

// Architecture
implementation("androidx.fragment:fragment-ktx:${Versions.AndroidX.fragment}")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:${Versions.AndroidX.lifecycle}")
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:${Versions.AndroidX.lifecycle}")
implementation("androidx.lifecycle:lifecycle-common-java8:${Versions.AndroidX.lifecycle}")
implementation("androidx.navigation:navigation-fragment-ktx:${Versions.AndroidX.navigation}")
implementation("androidx.navigation:navigation-ui-ktx:${Versions.AndroidX.navigation}")
implementation("org.orbit-mvi:orbit-viewmodel:${Versions.orbitMvi}")

// UI
implementation("com.google.android.material:material:${Versions.Google.material}")
implementation("androidx.appcompat:appcompat:${Versions.AndroidX.appCompat}")
implementation("androidx.constraintlayout:constraintlayout:${Versions.AndroidX.constraintLayout}")
implementation("com.xwray:groupie:${Versions.groupie}")
implementation("com.xwray:groupie-viewbinding:${Versions.groupie}")

implementation("io.ktor:ktor-client-core:${Versions.ktor}")
implementation("io.ktor:ktor-client-serialization:${Versions.ktor}")
implementation("io.ktor:ktor-client-serialization-jvm:${Versions.ktor}")
implementation("io.ktor:ktor-client-android:${Versions.ktor}")

// Database
implementation("com.squareup.sqldelight:runtime:${Versions.sqlDelight}")
implementation("com.squareup.sqldelight:android-driver:${Versions.sqlDelight}")

// Dependency Injection
implementation("androidx.hilt:hilt-lifecycle-viewmodel:${Versions.AndroidX.hilt}")
kapt("androidx.hilt:hilt-compiler:${Versions.AndroidX.hilt}")
implementation("com.google.dagger:hilt-android:${Versions.Google.dagger}")
kapt("com.google.dagger:hilt-android-compiler:${Versions.Google.dagger}")

coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:${Versions.desugar}")
}

android {
compileSdkVersion(30)
defaultConfig {
applicationId = "com.appmattus.layercache.samples"
minSdkVersion(21)
targetSdkVersion(30)
versionCode = 1
versionName = "1.0"
vectorDrawables.useSupportLibrary = true
}
buildTypes {
getByName("release") {
isMinifyEnabled = false
}
}

compileOptions {
isCoreLibraryDesugaringEnabled = true

sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}

kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8.toString()
}

buildFeatures {
buildConfig = true
viewBinding = true
}

sourceSets.all {
java.srcDir("src/$name/kotlin")
}
}

sqldelight {
database("AppDatabase") {
packageName = "com.appmattus.layercache.samples.data.database"
}
}
10 changes: 10 additions & 0 deletions samples/androidApp/src/debug/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<application>
<activity
android:name="com.appmattus.layercache.samples.test.HiltTestActivity"
android:exported="false" />
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.appmattus.layercache.samples.test

import androidx.appcompat.app.AppCompatActivity
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class HiltTestActivity : AppCompatActivity()
27 changes: 27 additions & 0 deletions samples/androidApp/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.appmattus.layercache.samples">

<uses-permission android:name="android.permission.INTERNET" />

<application
android:name="com.appmattus.layercache.samples.SamplesApplication"
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:networkSecurityConfig="@xml/network_security_config"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:usesCleartextTraffic="false"
tools:ignore="UnusedAttribute">
<activity android:name="com.appmattus.layercache.samples.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2021 Appmattus Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.appmattus.layercache.samples

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.findNavController
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {

private val navController by lazy { findNavController(R.id.nav_host_fragment) }

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)
}

override fun onSupportNavigateUp() = navController.navigateUp()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2021 Appmattus Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.appmattus.layercache.samples

import android.app.Application
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp
class SamplesApplication : Application()
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2021 Appmattus Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.appmattus.layercache.samples

import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.appmattus.layercache.samples.databinding.RecyclerViewFragmentBinding
import com.appmattus.layercache.samples.ui.component.SingleLineTextHeaderItem
import com.appmattus.layercache.samples.ui.component.SingleLineTextItem
import com.appmattus.layercache.samples.ui.viewBinding
import com.xwray.groupie.GroupAdapter
import com.xwray.groupie.GroupieViewHolder
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class SamplesFragment : Fragment(R.layout.recycler_view_fragment) {

private val binding by viewBinding<RecyclerViewFragmentBinding>()

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

binding.recyclerView.apply {
layoutManager = LinearLayoutManager(requireContext(), RecyclerView.VERTICAL, false)
adapter = GroupAdapter<GroupieViewHolder>().apply {
add(SingleLineTextHeaderItem("Samples"))
add(SingleLineTextItem("Encrypted shared preferences") {
findNavController().navigate(R.id.action_samplesFragment_to_sharedPrefsFragment)
})
add(SingleLineTextItem("SqlDelight") {
findNavController().navigate(R.id.action_samplesFragment_to_sqlDelightFragment)
})
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.appmattus.layercache.samples.data

import com.appmattus.layercache.Cache

/**
* Class used to wrap a cache just so we can figure out from composing two caches where data originated from
*/
class LastRetrievedWrapper {
// Stores the name of the cache data was returned from
var lastRetrieved: String? = null

fun reset() {
lastRetrieved = null
}

// A simple Cache wrapper to update lastRetrieved when the cache returns a value from its get function
fun <K : Any, V : Any> Cache<K, V>.wrap(name: String): Cache<K, V> {
val delegate = this
return object : Cache<K, V> {
override suspend fun get(key: K): V? = delegate.get(key)?.also {
if (lastRetrieved == null) {
lastRetrieved = name
}
}

override suspend fun set(key: K, value: V) = delegate.set(key, value)
override suspend fun evict(key: K) = delegate.evict(key)
override suspend fun evictAll() = delegate.evictAll()
}
}
}
Loading

0 comments on commit 2d4d5b3

Please sign in to comment.