Skip to content

Commit 84b710d

Browse files
authored
Merge pull request #528 from AppDevNext/KotlinFileUtils
Kotlin file utils
2 parents dcca685 + 6d2dab2 commit 84b710d

File tree

5 files changed

+81
-374
lines changed

5 files changed

+81
-374
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.github.mikephil.charting.utils
2+
3+
import android.content.res.AssetManager
4+
import android.util.Log
5+
import com.github.mikephil.charting.data.BarEntry
6+
import com.github.mikephil.charting.data.Entry
7+
import java.io.BufferedReader
8+
import java.io.IOException
9+
import java.io.InputStreamReader
10+
import java.nio.charset.StandardCharsets
11+
12+
/**
13+
* Utilities class for interacting with the assets and the devices storage to load and save DataSet objects from and to .txt files.
14+
*/
15+
private const val LOG = "Chart-FileUtils"
16+
17+
/**
18+
* Loads an array of Entries from a textfile from the assets folder.
19+
*
20+
* @param path the name of the file in the assets folder (+ path if needed)
21+
*/
22+
fun AssetManager.loadEntriesFromAssets(path: String): MutableList<Entry> {
23+
val entries: MutableList<Entry> = ArrayList()
24+
25+
try {
26+
BufferedReader(
27+
InputStreamReader(this.open(path), StandardCharsets.UTF_8)
28+
).use { reader ->
29+
var line = reader.readLine()
30+
while (line != null) {
31+
// process line
32+
val split: Array<String?> = line.split("#".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
33+
34+
if (split.size <= 2) {
35+
entries.add(Entry(split[1]!!.toFloat(), split[0]!!.toFloat()))
36+
} else {
37+
val vals = FloatArray(split.size - 1)
38+
39+
for (i in vals.indices) {
40+
vals[i] = split[i]!!.toFloat()
41+
}
42+
43+
entries.add(BarEntry(split[split.size - 1]!!.toInt().toFloat(), vals))
44+
}
45+
line = reader.readLine()
46+
}
47+
}
48+
} catch (e: IOException) {
49+
Log.e(LOG, e.toString())
50+
}
51+
52+
return entries
53+
}
54+
55+
fun AssetManager.loadBarEntriesFromAssets(path: String): MutableList<BarEntry> {
56+
val entries: MutableList<BarEntry> = ArrayList()
57+
58+
try {
59+
BufferedReader(InputStreamReader(this.open(path), StandardCharsets.UTF_8)).use { reader ->
60+
var line = reader.readLine()
61+
while (line != null) {
62+
val split: Array<String?> = line.split("#".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
63+
entries.add(BarEntry(split[1]!!.toFloat(), split[0]!!.toFloat()))
64+
line = reader.readLine()
65+
}
66+
}
67+
} catch (e: IOException) {
68+
Log.e(LOG, e.toString())
69+
}
70+
71+
return entries
72+
}

MPChartLib/src/main/java/com/github/mikephil/charting/utils/FileUtils.java

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

0 commit comments

Comments
 (0)