Skip to content

STTP example (not working) #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,22 @@ android {

buildTypes {
release {
minifyEnabled false
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
debug {
debuggable true
//minifyEnabled true
proguardFiles 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17

}
kotlinOptions {
jvmTarget = '11'
jvmTarget = '17'
}
buildFeatures {
compose true
Expand All @@ -39,6 +45,7 @@ android {
dependencies {

implementation project(":core")
implementation libs.scala3.library.x
implementation libs.androidx.core.ktx
implementation libs.androidx.lifecycle.runtime.ktx
implementation libs.androidx.activity.compose
Expand Down
25 changes: 4 additions & 21 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -1,21 +1,4 @@
# Add project specific ProGuard rules here.
# 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

# 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 *;
#}

# 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
-dontwarn java.net.http.HttpClient$Builder
-dontwarn java.net.http.HttpClient$Redirect
-dontwarn java.net.http.HttpClient
-dontwarn java.net.http.HttpRequest
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
android:allowBackup="true"
Expand Down
85 changes: 70 additions & 15 deletions app/src/main/java/com/example/scalacoremodule/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,100 @@ import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
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.padding
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.example.core.Description
import com.example.core.Foo
import com.example.scalacoremodule.ui.theme.ScalaCoreModuleTheme
import java.util.Optional
import java.util.concurrent.Executors

class MainActivity : ComponentActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
ScalaCoreModuleTheme {
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
Greeting(
name = "Android with Scala: ${Foo.bar()}",
modifier = Modifier.padding(innerPadding)
)
}
Form()
}
}
}
}

@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
Text(
text = "Hello $name!",
modifier = modifier
)
fun Form() {
var org by remember { mutableStateOf("indoorvivants") }
var repo by remember { mutableStateOf("sn-bindgen") }
var error by remember { mutableStateOf("") }
var proj: Optional<Description> by remember { mutableStateOf(Optional.empty()) }
val exc = Executors.newSingleThreadExecutor()


Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Column(
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(text = error)
Text("Organisation")
OutlinedTextField(value = org, onValueChange = { org = it })
Text("Repository")
OutlinedTextField(value = repo, onValueChange = { repo = it })
Button(onClick = {
exc.execute({
try {
Foo.getProjectInfo(org, repo).fold({ println(it) }, {
proj = Optional.of(it)
})
} catch (e: Exception) {
println(e.message)
println(e.printStackTrace())
}
})
}) { Text("Look up on Scaladex") }
ProjectDescription(proj)


}
}
}

@Composable
fun ProjectDescription(desc: Optional<Description>) {
if (desc.isEmpty) Column { }
else {
var proj = desc.get()
Column {
Text(proj.description())
Text("Stars: " + proj.stars().toString())
}
}
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
ScalaCoreModuleTheme {
Greeting("Android")
fun Submit(onClick: () -> Unit) {
Button(onClick = onClick) {
Text("Look up")
}
}
9 changes: 9 additions & 0 deletions app/src/main/java/java/net/http/HttpTimeoutException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package java.net.http;

import java.io.IOException;

public class HttpTimeoutException extends IOException {
HttpTimeoutException(String message) {
super(message);
}
}
7 changes: 7 additions & 0 deletions core/.scalafmt.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version = "3.9.2"
runner.dialect = scala3
rewrite.scala3.insertEndMarkerMinLines = 10
rewrite.scala3.removeOptionalBraces = true
rewrite.scala3.convertToNewSyntax = true
align.preset = more

6 changes: 5 additions & 1 deletion core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ plugins {
id 'scala'
}


dependencies {
implementation 'org.scala-lang:scala3-library_3:3.6.3'
implementation libs.scala3.library.x
implementation 'com.softwaremill.sttp.client4:okhttp-backend_3:dev'
implementation 'com.softwaremill.sttp.client4:upickle_3:dev'
implementation 'com.lihaoyi:upickle_3:4.1.0'
testImplementation 'org.scalatest:scalatest_3:3.2.19'
testImplementation 'junit:junit:4.13.1'
}
Expand Down
25 changes: 4 additions & 21 deletions core/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -1,21 +1,4 @@
# Add project specific ProGuard rules here.
# 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

# 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 *;
#}

# 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
-dontwarn java.net.http.HttpClient$Builder
-dontwarn java.net.http.HttpClient$Redirect
-dontwarn java.net.http.HttpClient
-dontwarn java.net.http.HttpRequest
29 changes: 28 additions & 1 deletion core/src/main/scala/com/example/core/Foo.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
package com.example.core

import sttp.client4.upicklejson.default.asJson

import scala.util.Try

case class Description(
description: String,
stars: Int,
topic: List[String] = List.empty
) derives upickle.default.ReadWriter

object Foo:
import sttp.client4.okhttp.OkHttpSyncBackend

val backend = OkHttpSyncBackend()
import sttp.client4.*
import sttp.client4.upicklejson.*

val x = Seq(Option(25), Option(-30), Option(100)).find(_.isDefined).flatten

val bar: Int = (1 to 11).toList.sum

val bar: Int = 42
def getProjectInfo(org: String, repo: String): Either[String, Description] =
basicRequest
.get(uri"https://index.scala-lang.org/api/v1/projects/$org/$repo")
.response(asJson[Description])
.send(backend)
.body
.left
.map(_.toString)
end Foo
3 changes: 3 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ activityCompose = "1.10.0"
composeBom = "2024.04.01"
appcompat = "1.7.0"
material = "1.12.0"
#scala3Library_x = "3.7.0-RC1-bin-SNAPSHOT"
scala3Library_x = "3.6.3"

[libraries]
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
Expand All @@ -28,6 +30,7 @@ androidx-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit
androidx-material3 = { group = "androidx.compose.material3", name = "material3" }
androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" }
material = { group = "com.google.android.material", name = "material", version.ref = "material" }
scala3-library_x = { module = "org.scala-lang:scala3-library_3", version.ref = "scala3Library_x" }

[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
Expand Down
2 changes: 2 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ pluginManagement {
}
mavenCentral()
gradlePluginPortal()

}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
mavenLocal()
}
}

Expand Down