Browse Source

Multiplatform template in IDEA / Android Studio. Refactoring

pull/36/head
Igor Demin 4 years ago
parent
commit
f9d3c56b0c
  1. 10
      examples/codeviewer/android/src/main/assets/data/EditorView.kt
  2. 1
      examples/codeviewer/android/src/main/java/org/jetbrains/codeviewer/MainActivity.kt
  3. 6
      examples/codeviewer/common/src/jvmMain/kotlin/org/jetbrains/codeviewer/platform/JvmFile.kt

10
examples/codeviewer/android/src/main/assets/data/EditorView.kt

@ -1,5 +1,5 @@
/**
* This file is example (we can open it in android application)
* This file is an example (we can open it in android application)
*/
package org.jetbrains.codeviewer.ui.editor
@ -73,7 +73,7 @@ fun EditorView(model: Editor, settings: Settings) = key(model) {
@Composable
private fun Lines(lines: Editor.Lines, settings: Settings) = with(DensityAmbient.current) {
val maxNum = remember(lines.lineNumberDigitCount) {
val maxNumber = remember(lines.lineNumberDigitCount) {
(1..lines.lineNumberDigitCount).joinToString(separator = "") { "9" }
}
@ -89,7 +89,7 @@ private fun Lines(lines: Editor.Lines, settings: Settings) = with(DensityAmbient
val line: Editor.Line? by loadable { lines.get(index) }
Box(Modifier.height(lineHeight)) {
if (line != null) {
Line(Modifier.align(Alignment.CenterStart), maxNum, line!!, settings)
Line(Modifier.align(Alignment.CenterStart), maxNumber, line!!, settings)
}
}
}
@ -108,11 +108,11 @@ private fun Lines(lines: Editor.Lines, settings: Settings) = with(DensityAmbient
// دعم اللغة العربية
// 中文支持
@Composable
private fun Line(modifier: Modifier, maxNum: String, line: Editor.Line, settings: Settings) {
private fun Line(modifier: Modifier, maxNumber: String, line: Editor.Line, settings: Settings) {
Row(modifier = modifier) {
WithoutSelection {
Box {
LineNumber(maxNum, Modifier.drawOpacity(0f), settings)
LineNumber(maxNumber, Modifier.drawOpacity(0f), settings)
LineNumber(line.number.toString(), Modifier.align(Alignment.CenterEnd), settings)
}
}

1
examples/codeviewer/android/src/main/java/org/jetbrains/codeviewer/MainActivity.kt

@ -8,7 +8,6 @@ import org.jetbrains.codeviewer.ui.MainView
import java.io.File
import java.io.FileOutputStream
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

6
examples/codeviewer/common/src/jvmMain/kotlin/org/jetbrains/codeviewer/platform/JvmFile.kt

@ -25,6 +25,7 @@ fun java.io.File.toProjectFile(): File = object : File {
get() = isDirectory && listFiles()?.size ?: 0 > 0
override suspend fun readLines(backgroundScope: CoroutineScope): TextLines {
// linePositions can be very big, so we are using IntList instead of List<Long>
val linePositions = IntList()
var size by mutableStateOf(0)
@ -52,6 +53,7 @@ fun java.io.File.toProjectFile(): File = object : File {
try {
RandomAccessFile(this@toProjectFile, "rws").use {
it.seek(position.toLong())
// NOTE: it isn't efficient, but simple
String(
it.readLine()
.toCharArray()
@ -77,7 +79,6 @@ private suspend fun java.io.File.readLinePositions(list: IntList) = withContext(
}
val averageLineLength = 200
// linePositions can be very big, so we are using IntArray instead of List<Long>
list.clear(length().toInt() / averageLineLength)
var isBeginOfLine = true
@ -107,6 +108,9 @@ private suspend fun java.io.File.readLinePositions(list: IntList) = withContext(
list.compact()
}
/**
* Compact version of List<Int> (without unboxing Int and using IntArray under the hood)
*/
private class IntList(initialCapacity: Int = 16) {
@Volatile
private var array = IntArray(initialCapacity)

Loading…
Cancel
Save