Skip to content

Commit efe1628

Browse files
authored
Merge pull request #275 from AppDevNext/KotlinDemoBase
Kotlin DemoBase
2 parents b090ce3 + 1083827 commit efe1628

File tree

6 files changed

+140
-173
lines changed

6 files changed

+140
-173
lines changed

MPChartExample/src/main/java/com/xxmassdeveloper/mpchartexample/CombinedChartActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ protected void onCreate(Bundle savedInstanceState) {
8787
xAxis.setValueFormatter(new IAxisValueFormatter() {
8888
@Override
8989
public String getFormattedValue(float value, AxisBase axis) {
90-
return months[(int) value % months.length];
90+
return DemoBase.Companion.getMonths().get((int) value % DemoBase.Companion.getMonths().size());
9191
}
9292
});
9393

MPChartExample/src/main/java/com/xxmassdeveloper/mpchartexample/notimportant/DemoBase.java

Lines changed: 0 additions & 100 deletions
This file was deleted.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
}

MPChartExample/src/main/java/com/xxmassdeveloper/mpchartexample/notimportant/MainActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class MainActivity : AppCompatActivity(), OnItemClickListener {
2424

2525
// initialize the utilities
2626
Utils.init(this)
27-
val adapter = MyAdapter(this, menuItems)
27+
val adapter = MenuAdapter(this, menuItems)
2828
val lv = findViewById<ListView>(R.id.listViewMain)
2929
lv.adapter = adapter
3030
lv.onItemClickListener = this
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.xxmassdeveloper.mpchartexample.notimportant
2+
3+
import android.annotation.SuppressLint
4+
import android.content.Context
5+
import android.graphics.Typeface
6+
import android.view.LayoutInflater
7+
import android.view.View
8+
import android.view.ViewGroup
9+
import android.widget.ArrayAdapter
10+
import android.widget.TextView
11+
import com.xxmassdeveloper.mpchartexample.R
12+
13+
internal class MenuAdapter(context: Context, objects: List<ContentItem<*>?>?) : ArrayAdapter<ContentItem<*>?>(context, 0, objects!!) {
14+
private val mTypeFaceLight: Typeface = Typeface.createFromAsset(context.assets, "OpenSans-Light.ttf")
15+
private val mTypeFaceRegular: Typeface = Typeface.createFromAsset(context.assets, "OpenSans-Regular.ttf")
16+
17+
@SuppressLint("InflateParams")
18+
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
19+
val c = getItem(position)
20+
21+
val holder = ViewHolder()
22+
23+
val inflatedView = if (c != null && c.isSection) {
24+
LayoutInflater.from(context).inflate(R.layout.list_item_section, null)
25+
} else {
26+
LayoutInflater.from(context).inflate(R.layout.list_item, null)
27+
}
28+
29+
holder.tvName = inflatedView.findViewById(R.id.tvName)
30+
holder.tvDesc = inflatedView.findViewById(R.id.tvDesc)
31+
32+
inflatedView.tag = holder
33+
34+
if (c != null && c.isSection)
35+
holder.tvName?.setTypeface(mTypeFaceRegular)
36+
else
37+
holder.tvName?.setTypeface(mTypeFaceLight)
38+
holder.tvDesc?.setTypeface(mTypeFaceLight)
39+
40+
holder.tvName?.text = c?.name
41+
holder.tvDesc?.text = c?.desc
42+
43+
return inflatedView
44+
}
45+
46+
private inner class ViewHolder {
47+
var tvName: TextView? = null
48+
var tvDesc: TextView? = null
49+
}
50+
}

MPChartExample/src/main/java/com/xxmassdeveloper/mpchartexample/notimportant/MyAdapter.java

Lines changed: 0 additions & 71 deletions
This file was deleted.

0 commit comments

Comments
 (0)