|
| 1 | +package com.xxmassdeveloper.mpchartexample.notimportant |
| 2 | + |
| 3 | +import android.Manifest |
| 4 | +import android.content.pm.PackageManager |
| 5 | +import android.graphics.Typeface |
| 6 | +import android.os.Bundle |
| 7 | +import android.view.View |
| 8 | +import android.widget.Toast |
| 9 | +import androidx.appcompat.app.AppCompatActivity |
| 10 | +import androidx.core.app.ActivityCompat |
| 11 | +import com.github.mikephil.charting.charts.Chart |
| 12 | +import com.google.android.material.snackbar.Snackbar |
| 13 | +import com.xxmassdeveloper.mpchartexample.R |
| 14 | +import java.text.DateFormatSymbols |
| 15 | + |
| 16 | +abstract class DemoBase : AppCompatActivity(), ActivityCompat.OnRequestPermissionsResultCallback { |
| 17 | + |
| 18 | + @JvmField |
| 19 | + protected val parties: Array<String> = arrayOf( |
| 20 | + "Party A", "Party B", "Party C", "Party D", "Party E", "Party F", "Party G", "Party H", |
| 21 | + "Party I", "Party J", "Party K", "Party L", "Party M", "Party N", "Party O", "Party P", |
| 22 | + "Party Q", "Party R", "Party S", "Party T", "Party U", "Party V", "Party W", "Party X", |
| 23 | + "Party Y", "Party Z" |
| 24 | + ) |
| 25 | + |
| 26 | + @JvmField |
| 27 | + protected var tfRegular: Typeface? = null |
| 28 | + |
| 29 | + @JvmField |
| 30 | + protected var tfLight: Typeface? = null |
| 31 | + |
| 32 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 33 | + super.onCreate(savedInstanceState) |
| 34 | + |
| 35 | + tfRegular = Typeface.createFromAsset(assets, "OpenSans-Regular.ttf") |
| 36 | + tfLight = Typeface.createFromAsset(assets, "OpenSans-Light.ttf") |
| 37 | + } |
| 38 | + |
| 39 | + override fun onBackPressed() { |
| 40 | + super.onBackPressed() |
| 41 | + overridePendingTransition(R.anim.move_left_in_activity, R.anim.move_right_out_activity) |
| 42 | + } |
| 43 | + |
| 44 | + override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) { |
| 45 | + super.onRequestPermissionsResult(requestCode, permissions, grantResults) |
| 46 | + if (requestCode == PERMISSION_STORAGE) { |
| 47 | + if (grantResults.size == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { |
| 48 | + saveToGallery() |
| 49 | + } else { |
| 50 | + Toast.makeText(applicationContext, "Saving FAILED!", Toast.LENGTH_SHORT).show() |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + protected fun requestStoragePermission(view: View?) { |
| 56 | + if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { |
| 57 | + Snackbar.make(view!!, "Write permission is required to save image to gallery", Snackbar.LENGTH_INDEFINITE) |
| 58 | + .setAction(android.R.string.ok) { |
| 59 | + ActivityCompat.requestPermissions( |
| 60 | + this@DemoBase, |
| 61 | + arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), |
| 62 | + PERMISSION_STORAGE |
| 63 | + ) |
| 64 | + } |
| 65 | + .show() |
| 66 | + } else { |
| 67 | + Toast.makeText(applicationContext, "Permission Required!", Toast.LENGTH_SHORT).show() |
| 68 | + ActivityCompat.requestPermissions(this@DemoBase, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), PERMISSION_STORAGE) |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + protected fun saveToGallery(chart: Chart<*>?, name: String) { |
| 73 | + chart?.let { |
| 74 | + if (chart.saveToGallery(name + "_" + System.currentTimeMillis(), 70)) |
| 75 | + Toast.makeText(applicationContext, "Saving SUCCESSFUL!", Toast.LENGTH_SHORT).show() |
| 76 | + else |
| 77 | + Toast.makeText(applicationContext, "Saving FAILED!", Toast.LENGTH_SHORT).show() |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + protected abstract fun saveToGallery() |
| 82 | + |
| 83 | + companion object { |
| 84 | + private const val PERMISSION_STORAGE = 0 |
| 85 | + // Jan, Feb,... Dec |
| 86 | + val months = DateFormatSymbols().months.toList().map { it.take(3) } |
| 87 | + } |
| 88 | +} |
0 commit comments