Skip to content

Commit

Permalink
sync 2024-06-25
Browse files Browse the repository at this point in the history
  • Loading branch information
Fast Reports committed Jun 25, 2024
1 parent 063c1b9 commit d42eb42
Show file tree
Hide file tree
Showing 65 changed files with 1,573 additions and 0 deletions.
47 changes: 47 additions & 0 deletions Kotlin Demos/fastreport-cloud-androidKotlin/app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
}

android {
namespace = "cloud.fastreport.androiddemo"
compileSdk = 34

defaultConfig {
applicationId = "cloud.fastreport.androiddemo"
minSdk = 31
targetSdk = 34
versionCode = 1
versionName = "1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
signingConfig = signingConfigs.getByName("debug")
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}

}

dependencies {
implementation("com.squareup.okhttp3:okhttp:4.12.0")
implementation("org.json:json:20210307")
implementation("androidx.core:core-ktx:1.13.1")
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.12.0")
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
}
21 changes: 21 additions & 0 deletions Kotlin Demos/fastreport-cloud-androidKotlin/app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# 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
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">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:requestLegacyExternalStorage="true"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/cloud_icon"
android:label="@string/app_name"
android:roundIcon="@mipmap/cloud_icon_round"
android:supportsRtl="true"
android:theme="@style/Theme.AndroidDemo"
tools:targetApi="34">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<Report ScriptLanguage="CSharp" ReportInfo.Created="06/05/2024 09:17:22" ReportInfo.Modified="06/05/2024 09:17:22" ReportInfo.CreatorVersion="2024.1.10.0">
<Dictionary>
<Parameter Name="Parameter1" DataType="System.String" AsString="">
<Parameter Name="Parameter2" DataType="System.String" AsString=""/>
</Parameter>
<Parameter Name="Parameter3" DataType="System.String" AsString=""/>
</Dictionary>
<ReportPage Name="Page1" Watermark.Font="Arial, 60pt">
<ReportTitleBand Name="ReportTitle1" Width="718.2" Height="47.25">
<TextObject Name="Text2" Left="18.9" Top="18.89" Width="264.6" Height="28.35" Text="File has been generated by Android Demo" Font="Tahoma, 10pt"/>
</ReportTitleBand>
<PageHeaderBand Name="PageHeader1" Width="718.2" Height="37.8"/>
<DataBand Name="Data1" Width="718.2" Height="75.6">
<TextObject Name="Text1" Left="151.16" Top="37.79" Width="113.4" Height="18.9" Text="[Parameter1]" Font="Tahoma, 10pt"/>
<TextObject Name="Text3" Left="18.9" Top="37.8" Width="132.3" Height="18.9" Text="Report Parameter:" Font="Tahoma, 10pt"/>
</DataBand>
<PageFooterBand Name="PageFooter1" Width="718.2" Height="18.9"/>
</ReportPage>
</Report>
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,173 @@
package cloud.fastreport.androiddemo

import android.os.Bundle
import android.os.Environment
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import okhttp3.Credentials
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MultipartBody
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import java.io.IOException
import org.json.JSONObject
import java.io.File

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

var apiKey = ""
var templateId = ""
var exportId = ""
var subId = ""
var templateFolder = ""
var exportFolder = ""

val toast = Toast.makeText(this, "Success!", Toast.LENGTH_SHORT)
val toastError = Toast.makeText(this, "Error!", Toast.LENGTH_SHORT)
val client = OkHttpClient()
val mediaType = "application/json; charset=utf-8".toMediaType()

val uploadButton: Button = findViewById(R.id.uploadButton)
uploadButton.setOnClickListener {
val editTextInputApiKey: EditText = findViewById(R.id.editTextInputApiKey)
apiKey = editTextInputApiKey.text.toString()
val editTextInputSubId: EditText = findViewById(R.id.editTextInputSubId)
subId = editTextInputSubId.text.toString()

GlobalScope.launch {
try {
val getTemplateRoot = Request.Builder()
.url("https://fastreport.cloud/api/rp/v1/Templates/Root?subscriptionId=${subId}")
.addHeader("Authorization", Credentials.basic("apikey", apiKey))
.build()
try {
client.newCall(getTemplateRoot).execute().use { response ->
templateFolder = JSONObject(response.body!!.string()).getString("id")
}
} catch (e: IOException) {
println("Error: $e")
}

val fileName = "Template.frx"
val fileContent = assets.open(fileName).bufferedReader().use { it.readText() }
val requestBody = MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart(
"FileContent",
fileName,
fileContent.toRequestBody(mediaType)
)
.build()
val request = Request.Builder()
.url("https://fastreport.cloud/api/rp/v2/Templates/Folder/${templateFolder}/File")
.addHeader("Authorization", Credentials.basic("apikey", apiKey))
.addHeader("Accept", "application/json")
.post(requestBody)
.build()
client.newCall(request).execute().use { response ->
if (!response.isSuccessful) {
throw IOException("Error: ${response.code} ${response.message}")
}
templateId = JSONObject(response.body!!.string()).getString("id")
}
toast.show()
} catch (e: Exception) {
e.printStackTrace()
toastError.show()
}
}
}

val exportButton: Button = findViewById(R.id.exportButton)
exportButton.setOnClickListener {
GlobalScope.launch {
try {
val getExportRoot = Request.Builder()
.url("https://fastreport.cloud/api/rp/v1/Exports/Root?subscriptionId=${subId}")
.addHeader("Authorization", Credentials.basic("apikey", apiKey))
.build()
try {
client.newCall(getExportRoot).execute().use { response ->
exportFolder = JSONObject(response.body!!.string()).getString("id")
}
} catch (e: IOException) {
println("Error: $e")
}

val editParameterKey: EditText = findViewById(R.id.editParameterKey)
val parameterKey = editParameterKey.text.toString()
val editParameterValue: EditText = findViewById(R.id.editParameterValue)
val parameterValue = editParameterValue.text.toString()

val jsonRequest = JSONObject().apply {
put("\$t", "ExportTemplateVM")
put("fileName", "Exported file")
put("format", "Pdf")
put("folderId", exportFolder)
put("reportParameters", JSONObject().put(parameterKey, parameterValue))
}
val body: RequestBody = jsonRequest.toString().toRequestBody(mediaType)
val request = Request.Builder()
.url("https://fastreport.cloud/api/rp/v1/Templates/File/${templateId}/Export")
.addHeader("Authorization", Credentials.basic("apikey", apiKey))
.post(body)
.build()
client.newCall(request).execute().use { response ->
if (!response.isSuccessful) {
toastError.show()
}
exportId = JSONObject(response.body!!.string()).getString("id")
}
toast.show()
} catch (e: Exception) {
e.printStackTrace()
toastError.show()
}
}
}

val downloadButton: Button = findViewById(R.id.downloadButton)
downloadButton.setOnClickListener {
GlobalScope.launch {
try {
val downloadPdf = Request.Builder()
.url("https://fastreport.cloud/download/e/$exportId?preview=false")
.addHeader("Authorization", Credentials.basic("apikey", apiKey))
.build()
try {
client.newCall(downloadPdf).execute().use { response ->
if (!response.isSuccessful) {
throw IOException("Error: ${response.code} ${response.message}")
}

val responseBody = response.body
val inputStream = responseBody?.byteStream()
val downloadsPath =
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
val file1 = File(downloadsPath, "Exported file.pdf")
file1.outputStream().use { output ->
inputStream?.copyTo(output)
}
}
toast.show()
} catch (e: IOException) {
toastError.show()
}
} catch (e: Exception) {
e.printStackTrace()
toastError.show()
}
}
}
}
}

Loading

0 comments on commit d42eb42

Please sign in to comment.