Skip to content

Commit

Permalink
added chart sharing
Browse files Browse the repository at this point in the history
Added a button to share the chart wherever the user wants.
  • Loading branch information
NikitaSutulov committed May 24, 2024
1 parent ae4d3c2 commit ad0921a
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.nickytoolchick.agraph
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.action.ViewActions.replaceText
import androidx.test.espresso.action.ViewActions.swipeUp
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.intent.Intents
import androidx.test.espresso.intent.matcher.IntentMatchers.hasComponent
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.nickytoolchick.agraph.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>

</application>

</manifest>
21 changes: 20 additions & 1 deletion app/src/main/java/com/nickytoolchick/agraph/fileio/FileWriter.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.nickytoolchick.agraph.fileio

import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.Canvas
import android.media.MediaScannerConnection
import android.net.Uri
import android.os.Environment
import android.widget.Toast
import androidx.core.content.FileProvider
import com.nickytoolchick.agraph.data.ChartOptions
import com.nickytoolchick.agraph.data.Constants
import com.nickytoolchick.agraph.data.DatasetOptions
Expand All @@ -32,7 +35,7 @@ class FileWriter {
return Json.encodeToString(chartOptions) + Constants.SPLITTER + Json.encodeToString(datasetOptions)
}

fun exportChartAsPng(chartRenderer: ChartRenderer) {
fun exportChartAsPng(chartRenderer: ChartRenderer): File? {
val bitmap = Bitmap.createBitmap(chartRenderer.width, chartRenderer.height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
chartRenderer.draw(canvas)
Expand All @@ -51,14 +54,30 @@ class FileWriter {
null
)
Toast.makeText(chartRenderer.context, "Chart exported to gallery", Toast.LENGTH_SHORT).show()
return file
} catch (e: IOException) {
e.printStackTrace()
Toast.makeText(chartRenderer.context, "Export failed", Toast.LENGTH_SHORT).show()
return null
}
}

private fun generateImageFileName(): String {
val formatter = DateTimeFormatter.ofPattern("yyyy_MM_dd_HH_mm_ss_SSS")
return "chart${LocalDateTime.now().format(formatter)}.png"
}

fun shareChart(ctx: Context, chartRenderer: ChartRenderer) {
val file = exportChartAsPng(chartRenderer)
file?.let {
val uri: Uri = FileProvider.getUriForFile(ctx, "com.nickytoolchick.agraph.fileprovider", it)
val shareIntent: Intent = Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_STREAM, uri)
type = "image/png"
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
ctx.startActivity(Intent.createChooser(shareIntent, "Share chart via"))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class ChartRenderActivity : AppCompatActivity() {
lateinit var mainActivityIntent: Intent
private lateinit var binding: ActivityChartRenderBinding

private val fileWriter = FileWriter()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityChartRenderBinding.inflate(layoutInflater)
Expand All @@ -30,9 +32,12 @@ class ChartRenderActivity : AppCompatActivity() {
setContentView(binding.root)

binding.exportButton.setOnClickListener {
val fileWriter = FileWriter()
fileWriter.exportChartAsPng(binding.chartRenderer)
}

binding.shareButton.setOnClickListener {
fileWriter.shareChart(this, binding.chartRenderer)
}
}

private fun initChartRenderer() {
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/res/layout/activity_chart_options.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@

<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_height="match_parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@+id/submitChartOptionsButton"
android:layout_margin="15dp">
android:layout_margin="10dp"
android:paddingBottom="60dp">

<LinearLayout
android:id="@+id/inputChartOptionsLayout"
Expand Down
29 changes: 24 additions & 5 deletions app/src/main/res/layout/activity_chart_render.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,34 @@

</FrameLayout>

<Button
android:id="@+id/exportButton"
android:layout_width="wrap_content"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/chartRendererLayout"
android:text="Export as PNG"/>
app:layout_constraintTop_toBottomOf="@+id/chartRendererLayout">

<Button
android:id="@+id/exportButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="10dp"
android:text="Export as PNG"/>

<Button
android:id="@+id/shareButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="10dp"
android:text="Share image"/>

</LinearLayout>




</androidx.constraintlayout.widget.ConstraintLayout>
4 changes: 4 additions & 0 deletions app/src/main/res/xml/file_paths.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="." />
</paths>

0 comments on commit ad0921a

Please sign in to comment.