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 package org.jetbrains.codeviewer.ui.editor
@ -73,7 +73,7 @@ fun EditorView(model: Editor, settings: Settings) = key(model) {
@Composable @Composable
private fun Lines(lines: Editor.Lines, settings: Settings) = with(DensityAmbient.current) { 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" } (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) } val line: Editor.Line? by loadable { lines.get(index) }
Box(Modifier.height(lineHeight)) { Box(Modifier.height(lineHeight)) {
if (line != null) { 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 @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) { Row(modifier = modifier) {
WithoutSelection { WithoutSelection {
Box { Box {
LineNumber(maxNum, Modifier.drawOpacity(0f), settings) LineNumber(maxNumber, Modifier.drawOpacity(0f), settings)
LineNumber(line.number.toString(), Modifier.align(Alignment.CenterEnd), 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.File
import java.io.FileOutputStream import java.io.FileOutputStream
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) 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 get() = isDirectory && listFiles()?.size ?: 0 > 0
override suspend fun readLines(backgroundScope: CoroutineScope): TextLines { override suspend fun readLines(backgroundScope: CoroutineScope): TextLines {
// linePositions can be very big, so we are using IntList instead of List<Long>
val linePositions = IntList() val linePositions = IntList()
var size by mutableStateOf(0) var size by mutableStateOf(0)
@ -52,6 +53,7 @@ fun java.io.File.toProjectFile(): File = object : File {
try { try {
RandomAccessFile(this@toProjectFile, "rws").use { RandomAccessFile(this@toProjectFile, "rws").use {
it.seek(position.toLong()) it.seek(position.toLong())
// NOTE: it isn't efficient, but simple
String( String(
it.readLine() it.readLine()
.toCharArray() .toCharArray()
@ -77,7 +79,6 @@ private suspend fun java.io.File.readLinePositions(list: IntList) = withContext(
} }
val averageLineLength = 200 val averageLineLength = 200
// linePositions can be very big, so we are using IntArray instead of List<Long>
list.clear(length().toInt() / averageLineLength) list.clear(length().toInt() / averageLineLength)
var isBeginOfLine = true var isBeginOfLine = true
@ -107,6 +108,9 @@ private suspend fun java.io.File.readLinePositions(list: IntList) = withContext(
list.compact() list.compact()
} }
/**
* Compact version of List<Int> (without unboxing Int and using IntArray under the hood)
*/
private class IntList(initialCapacity: Int = 16) { private class IntList(initialCapacity: Int = 16) {
@Volatile @Volatile
private var array = IntArray(initialCapacity) private var array = IntArray(initialCapacity)

Loading…
Cancel
Save