Skip to content

Commit f49c89d

Browse files
author
crypticminds
committed
[Initial] Example for coldstorage
0 parents  commit f49c89d

33 files changed

Lines changed: 1539 additions & 0 deletions

app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

app/build.gradle

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
apply plugin: 'com.android.application'
2+
3+
apply plugin: 'kotlin-android'
4+
5+
apply plugin: 'kotlin-android-extensions'
6+
7+
apply plugin: "kotlin-kapt"
8+
9+
android {
10+
compileSdkVersion 29
11+
buildToolsVersion "29.0.2"
12+
defaultConfig {
13+
applicationId "com.arcane.coldstorageexamples"
14+
minSdkVersion 15
15+
targetSdkVersion 29
16+
versionCode 1
17+
versionName "1.0"
18+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
19+
}
20+
buildTypes {
21+
release {
22+
minifyEnabled false
23+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
24+
}
25+
}
26+
}
27+
28+
dependencies {
29+
implementation fileTree(dir: 'libs', include: ['*.jar'])
30+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
31+
implementation 'androidx.appcompat:appcompat:1.1.0'
32+
implementation 'androidx.core:core-ktx:1.1.0'
33+
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
34+
testImplementation 'junit:junit:4.12'
35+
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
36+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
37+
38+
implementation "com.github.crypticminds.ColdStorage:coldstoragecache:2.0.1"
39+
kapt "com.github.crypticminds.ColdStorage:coldstoragecompiler:2.0.1"
40+
implementation "com.github.crypticminds.ColdStorage:coldstorageannotation:2.0.1"
41+
}

app/proguard-rules.pro

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.arcane.coldstorageexamples
2+
3+
import androidx.test.platform.app.InstrumentationRegistry
4+
import androidx.test.ext.junit.runners.AndroidJUnit4
5+
6+
import org.junit.Test
7+
import org.junit.runner.RunWith
8+
9+
import org.junit.Assert.*
10+
11+
/**
12+
* Instrumented test, which will execute on an Android device.
13+
*
14+
* See [testing documentation](http://d.android.com/tools/testing).
15+
*/
16+
@RunWith(AndroidJUnit4::class)
17+
class ExampleInstrumentedTest {
18+
@Test
19+
fun useAppContext() {
20+
// Context of the app under test.
21+
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22+
assertEquals("com.arcane.coldstorageexamples", appContext.packageName)
23+
}
24+
}

app/src/main/AndroidManifest.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.arcane.coldstorageexamples">
4+
5+
<uses-permission android:name="android.permission.INTERNET" />
6+
7+
<application
8+
android:allowBackup="true"
9+
android:icon="@mipmap/ic_launcher"
10+
android:label="@string/app_name"
11+
android:roundIcon="@mipmap/ic_launcher_round"
12+
android:supportsRtl="true"
13+
android:theme="@style/AppTheme">
14+
<activity android:name=".MainActivity">
15+
<intent-filter>
16+
<action android:name="android.intent.action.MAIN" />
17+
18+
<category android:name="android.intent.category.LAUNCHER" />
19+
</intent-filter>
20+
</activity>
21+
</application>
22+
23+
</manifest>
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package com.arcane.coldstorageexamples
2+
3+
import android.os.Bundle
4+
import android.widget.Button
5+
import android.widget.TextView
6+
import androidx.appcompat.app.AppCompatActivity
7+
import com.arcane.coldstoragecache.callback.OnOperationSuccessfulCallback
8+
import com.arcane.coldstorageexamples.remotecall.MakeRemoteCall
9+
import com.arcane.generated.GeneratedCacheLayer
10+
11+
class MainActivity : AppCompatActivity(), OnOperationSuccessfulCallback<String?> {
12+
13+
/**
14+
* The make remote call object.
15+
*
16+
* @see MakeRemoteCall for details.
17+
*/
18+
private val makeremoteCall = MakeRemoteCall()
19+
20+
private lateinit var button: Button
21+
22+
private lateinit var firstRemoteCall: TextView
23+
24+
private lateinit var secondRemoteCall: TextView
25+
26+
private val valueArray = arrayListOf("a", "b", "c")
27+
28+
private var counter = 1
29+
30+
override fun onCreate(savedInstanceState: Bundle?) {
31+
super.onCreate(savedInstanceState)
32+
setContentView(R.layout.activity_main)
33+
button = findViewById(R.id.button)
34+
firstRemoteCall = findViewById(R.id.remotecall1)
35+
secondRemoteCall = findViewById(R.id.remotecall2)
36+
37+
38+
/**
39+
* On button click random values from the array will be used to make the
40+
* remote calls.
41+
* The counter variable is used to control the two calls are made alternatively
42+
* on button clicks.
43+
*/
44+
button.setOnClickListener {
45+
if (counter % 2 == 0) {
46+
GeneratedCacheLayer.makeRemoteCallToServiceA(
47+
valueArray.random(),
48+
makeremoteCall,
49+
this
50+
)
51+
} else {
52+
GeneratedCacheLayer.makeRemoteCallToServiceB(
53+
valueArray.random(),
54+
valueArray.random(),
55+
"constantvalue", makeremoteCall, this
56+
)
57+
}
58+
counter += 1
59+
}
60+
}
61+
62+
/**
63+
* Populate the first text view with the response.
64+
*/
65+
private fun populateFirstTextView(s: String) {
66+
runOnUiThread {
67+
firstRemoteCall.text = s
68+
}
69+
70+
}
71+
72+
/**
73+
* Populate the second text view with the response.
74+
*/
75+
private fun populateSecondTextView(s: String) {
76+
runOnUiThread {
77+
secondRemoteCall.text = s
78+
}
79+
}
80+
81+
/**
82+
* The callback implementation via which the result is
83+
* returned by the generated cache layer.
84+
*/
85+
override fun onSuccess(output: String?, operation: String) {
86+
when (operation) {
87+
"CallToServiceA" -> populateFirstTextView(output!!)
88+
else ->
89+
populateSecondTextView(output!!)
90+
91+
}
92+
}
93+
94+
95+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.arcane.coldstorageexamples.remotecall
2+
3+
import com.arcane.coldstorageannotation.Refrigerate
4+
import java.net.URL
5+
6+
class MakeRemoteCall {
7+
8+
/**
9+
* A method that makes a call to the "https://httpbin.org/get"
10+
* endpoint. This endpoint simply returns the arguments that were passed to it.
11+
* For the caching logic the parameter passed to the endpoint will act as
12+
* the key of the cache.
13+
*
14+
* Since there is only one parameter we don't need to specify the the keys
15+
* field in the annotation.
16+
*/
17+
@Refrigerate(operation = "CallToServiceA")
18+
fun makeRemoteCallToServiceA(value: String): String {
19+
val url = "https://httpbin.org/get?param1=$value"
20+
val textResponse = URL(url).readText()
21+
return textResponse
22+
}
23+
24+
25+
/**
26+
* We will use the same endpoint but this time we will pass 3 parameters.
27+
* However , out cache key should only be the first 2.
28+
* We can configure the "keys" field in the annotation by specifying
29+
* the variable names that should act as the key of the cache.
30+
* In this case "parameter1" and "parameter2"
31+
*/
32+
@Refrigerate(
33+
operation = "CallToServiceB", timeToLive = 10000,
34+
keys = ["parameter1", "parameter2"]
35+
)
36+
fun makeRemoteCallToServiceB(parameter1: String, parameter2: String, parameter3: String): String {
37+
val url = "https://httpbin.org/get?param1=$parameter1&param2=$parameter2&param3=$parameter3"
38+
val textResponse = URL(url).readText()
39+
return textResponse
40+
}
41+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:aapt="http://schemas.android.com/aapt"
3+
android:width="108dp"
4+
android:height="108dp"
5+
android:viewportWidth="108"
6+
android:viewportHeight="108">
7+
<path
8+
android:fillType="evenOdd"
9+
android:pathData="M32,64C32,64 38.39,52.99 44.13,50.95C51.37,48.37 70.14,49.57 70.14,49.57L108.26,87.69L108,109.01L75.97,107.97L32,64Z"
10+
android:strokeWidth="1"
11+
android:strokeColor="#00000000">
12+
<aapt:attr name="android:fillColor">
13+
<gradient
14+
android:endX="78.5885"
15+
android:endY="90.9159"
16+
android:startX="48.7653"
17+
android:startY="61.0927"
18+
android:type="linear">
19+
<item
20+
android:color="#44000000"
21+
android:offset="0.0" />
22+
<item
23+
android:color="#00000000"
24+
android:offset="1.0" />
25+
</gradient>
26+
</aapt:attr>
27+
</path>
28+
<path
29+
android:fillColor="#FFFFFF"
30+
android:fillType="nonZero"
31+
android:pathData="M66.94,46.02L66.94,46.02C72.44,50.07 76,56.61 76,64L32,64C32,56.61 35.56,50.11 40.98,46.06L36.18,41.19C35.45,40.45 35.45,39.3 36.18,38.56C36.91,37.81 38.05,37.81 38.78,38.56L44.25,44.05C47.18,42.57 50.48,41.71 54,41.71C57.48,41.71 60.78,42.57 63.68,44.05L69.11,38.56C69.84,37.81 70.98,37.81 71.71,38.56C72.44,39.3 72.44,40.45 71.71,41.19L66.94,46.02ZM62.94,56.92C64.08,56.92 65,56.01 65,54.88C65,53.76 64.08,52.85 62.94,52.85C61.8,52.85 60.88,53.76 60.88,54.88C60.88,56.01 61.8,56.92 62.94,56.92ZM45.06,56.92C46.2,56.92 47.13,56.01 47.13,54.88C47.13,53.76 46.2,52.85 45.06,52.85C43.92,52.85 43,53.76 43,54.88C43,56.01 43.92,56.92 45.06,56.92Z"
32+
android:strokeWidth="1"
33+
android:strokeColor="#00000000" />
34+
</vector>

0 commit comments

Comments
 (0)