Skip to content

Commit

Permalink
Add fast scroll
Browse files Browse the repository at this point in the history
  • Loading branch information
ismartcoding committed May 18, 2024
1 parent ddc1174 commit 21eb950
Show file tree
Hide file tree
Showing 146 changed files with 2,033 additions and 1,484 deletions.
5 changes: 5 additions & 0 deletions app/src/main/assets/editor/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
function loadEditor(data) {
editor = ace.edit('editor')
editor.renderer.setAnimatedScroll(true)
editor.setOptions({
autoScrollEditorIntoView: true,
copyWithEmptySelection: true,
vScrollBarAlwaysVisible: true,
})

if (data.isDarkTheme) {
editor.setTheme('ace/theme/monokai')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import io.ktor.client.plugins.*
import io.ktor.client.plugins.cookies.*
import io.ktor.client.plugins.logging.*
import io.ktor.client.plugins.websocket.WebSockets
import io.ktor.client.request.headers
import io.ktor.http.headers
import okhttp3.OkHttpClient
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody
Expand Down Expand Up @@ -40,6 +42,9 @@ object HttpClientManager {
install(HttpTimeout) {
requestTimeoutMillis = HttpApiTimeout.BROWSER_SECONDS * 1000L
}
headers {
set("accept", "*/*")
}
}

fun httpClient() =
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/ismartcoding/plain/data/DImage.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.ismartcoding.plain.data

import com.ismartcoding.plain.data.IData
import kotlinx.datetime.Instant
import java.io.Serializable


data class DImage(
override var id: String,
val title: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.ismartcoding.plain.data

data class DownloadResult(val path : String, val success : Boolean, val message : String = "")
14 changes: 13 additions & 1 deletion app/src/main/java/com/ismartcoding/plain/db/DChat.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.ismartcoding.lib.helpers.JsonHelper.jsonDecode
import com.ismartcoding.lib.helpers.JsonHelper.jsonEncode
import com.ismartcoding.lib.helpers.StringHelper
import com.ismartcoding.plain.R
import com.ismartcoding.plain.data.IData
import com.ismartcoding.plain.features.locale.LocaleHelper.getString
import kotlinx.datetime.*
import kotlinx.serialization.Serializable
Expand All @@ -22,9 +23,11 @@ fun DMessageContent.toJSONString(): String {
DMessageType.TEXT.value -> {
valueJSON = jsonEncode(value as DMessageText)
}

DMessageType.IMAGES.value -> {
valueJSON = jsonEncode(value as DMessageImages)
}

DMessageType.FILES.value -> {
valueJSON = jsonEncode(value as DMessageFiles)
}
Expand All @@ -46,7 +49,14 @@ enum class DMessageType(val value: String) {
class DMessageText(val text: String)

@Serializable
class DMessageFile(val id: String = StringHelper.shortUUID(), val uri: String, val size: Long, val duration: Long = 0)
class DMessageFile(
override var id: String = StringHelper.shortUUID(),
val uri: String,
val size: Long,
val duration: Long = 0,
val width: Int = 0,
val height: Int = 0,
) : IData

@Serializable
class DMessageImages(val items: List<DMessageFile>)
Expand Down Expand Up @@ -82,9 +92,11 @@ data class DChat(
DMessageType.TEXT.value -> {
message.value = jsonDecode<DMessageText>(valueJson)
}

DMessageType.IMAGES.value -> {
message.value = jsonDecode<DMessageImages>(valueJson)
}

DMessageType.FILES.value -> {
message.value = jsonDecode<DMessageFiles>(valueJson)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.ismartcoding.plain.enums

enum class RotationType(val value: Int) {
ROTATION_0(0),
ROTATION_90(90),
ROTATION_180(180),
ROTATION_270(270);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.ismartcoding.lib.extensions.getFilenameExtension
import com.ismartcoding.lib.extensions.isOk
import com.ismartcoding.lib.helpers.CryptoHelper
import com.ismartcoding.lib.html2md.MDConverter
import com.ismartcoding.lib.logcat.LogCat
import com.ismartcoding.lib.readability4j.Readability4J
import com.ismartcoding.lib.rss.DateParser
import com.ismartcoding.lib.rss.model.RssItem
Expand Down Expand Up @@ -63,12 +64,7 @@ fun RssItem.toDFeedEntry(
suspend fun DFeedEntry.fetchContentAsync(): ApiResult {
try {
val httpClient = HttpClientManager.browserClient()
val response =
httpClient.get(url) {
headers {
set("accept", "*/*")
}
}
val response = httpClient.get(url)

if (response.isOk()) {
val input = response.body<String>()
Expand All @@ -89,19 +85,24 @@ suspend fun DFeedEntry.fetchContentAsync(): ApiResult {
}

if (image.isNotEmpty() && !image.startsWith("/")) {
val r = httpClient.get(image)
if (r.isOk()) {
val dir = MainApp.instance.getExternalFilesDir(Environment.DIRECTORY_PICTURES)!!.path + "/feeds/${feedId}"
File(dir).mkdirs()
var path = "$dir/main-${CryptoHelper.sha1(image.toByteArray())}"
val extension = image.getFilenameExtension()
if (extension.isNotEmpty()) {
path += ".$extension"
try {
val r = httpClient.get(image)
if (r.isOk()) {
val dir = MainApp.instance.getExternalFilesDir(Environment.DIRECTORY_PICTURES)!!.path + "/feeds/${feedId}"
File(dir).mkdirs()
var path = "$dir/main-${CryptoHelper.sha1(image.toByteArray())}"
val extension = image.getFilenameExtension()
if (extension.isNotEmpty()) {
path += ".$extension"
}
val file = File(path)
file.createNewFile()
r.bodyAsChannel().copyAndClose(file.writeChannel())
image = path
}
val file = File(path)
file.createNewFile()
r.bodyAsChannel().copyAndClose(file.writeChannel())
image = path
} catch (ex: Exception) {
LogCat.e(ex.toString())
ex.printStackTrace()
}
}
content = MDConverter().convert(mobilizedHtml)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ object FeedEntryHelper {
val queryGroups = SearchHelper.parse(query)
queryGroups.forEach {
if (it.name == "text") {
where.addLikes(listOf("description", "content"), listOf(it.value, it.value))
where.addLikes(listOf("title", "description", "content"), listOf(it.value, it.value, it.value))
} else if (it.name == "feed_id") {
where.add("feed_id=?", it.value)
} else if (it.name == "today" && it.value == "true") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ data class DFile(
val size: Long,
val isDir: Boolean,
val children: Int,
val mediaStoreId: String = ""
val mediaId: String = ""
) : IData {
override var id: String
get() = path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ object AppLogHelper {
fun export(context: Context) {
coMain {
DialogHelper.showLoading()
val zipFile = File(context.filesDir.absolutePath + "/logs.zip")
val zipFile = File(context.cacheDir.absolutePath + "/logs.zip")
val folder = DiskLogFormatStrategy.getLogFolder(context)
withIO {
ZipUtil.pack(File(folder), zipFile)
Expand Down
60 changes: 60 additions & 0 deletions app/src/main/java/com/ismartcoding/plain/helpers/DownloadHelper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.ismartcoding.plain.helpers

import com.ismartcoding.lib.extensions.getFilenameExtension
import com.ismartcoding.lib.extensions.isOk
import com.ismartcoding.lib.extensions.scanFileByConnection
import com.ismartcoding.lib.helpers.CryptoHelper
import com.ismartcoding.lib.logcat.LogCat
import com.ismartcoding.plain.MainApp
import com.ismartcoding.plain.api.HttpClientManager
import com.ismartcoding.plain.data.DownloadResult
import io.ktor.client.request.get
import io.ktor.client.statement.bodyAsChannel
import io.ktor.util.cio.writeChannel
import io.ktor.utils.io.copyAndClose
import java.io.File

object DownloadHelper {
suspend fun downloadAsync(url: String, dir: String): DownloadResult {
val httpClient = HttpClientManager.browserClient()
return try {
val r = httpClient.get(url)
if (r.isOk()) {
File(dir).mkdirs()
var path = "$dir/${CryptoHelper.sha1(url.toByteArray())}"
val extension = url.getFilenameExtension()
if (extension.isNotEmpty()) {
path += ".$extension"
}
val file = File(path)
file.createNewFile()
r.bodyAsChannel().copyAndClose(file.writeChannel())
MainApp.instance.scanFileByConnection(file, null)
DownloadResult(path, true)
} else {
DownloadResult("", false, r.toString())
}
} catch (ex: Exception) {
LogCat.e(ex.toString())
ex.printStackTrace()
DownloadResult("", false, ex.toString())
}
}

suspend fun downloadToTempAsync(url: String, tempFile: File): DownloadResult {
val httpClient = HttpClientManager.browserClient()
return try {
val r = httpClient.get(url)
if (r.isOk()) {
r.bodyAsChannel().copyAndClose(tempFile.writeChannel())
DownloadResult(tempFile.absolutePath, true)
} else {
DownloadResult("", false, r.toString())
}
} catch (ex: Exception) {
LogCat.e(ex.toString())
ex.printStackTrace()
DownloadResult("", false, ex.toString())
}
}
}
25 changes: 13 additions & 12 deletions app/src/main/java/com/ismartcoding/plain/helpers/FileHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ object FileHelper {
context: Context,
name: String,
): File {
return File("${context.cacheDir}/$name").apply {
return File("${context.cacheDir.absolutePath}/$name").apply {
writeBytes(context.assets.open(name).readBytes())
}
}
Expand Down Expand Up @@ -115,9 +115,13 @@ object FileHelper {
}

fun copyFileToDownloads(path: String): String {
return copyFileToPublicDir(path, Environment.DIRECTORY_DOWNLOADS)
}

fun copyFileToPublicDir(path: String, dirName: String, newName: String = ""): String {
try {
val fileName = path.getFilenameFromPath()
val file = createDownloadFile(fileName)
val fileName = newName.ifEmpty { path.getFilenameFromPath() }
val file = createPublicFile(fileName, dirName)
File(path).copyTo(file)
MainApp.instance.scanFileByConnection(file, null)
return file.absolutePath
Expand All @@ -131,7 +135,7 @@ object FileHelper {
fun copyFileToDownloads(context: Context, uri: Uri): String {
try {
val fileName = uri.getFileName(context)
val file = createDownloadFile(fileName)
val file = createPublicFile(fileName, Environment.DIRECTORY_DOWNLOADS)
val outputStream = FileOutputStream(file)
if (uri.scheme == "content") {
val inputStream = context.contentResolver.openInputStream(uri)
Expand Down Expand Up @@ -160,18 +164,15 @@ object FileHelper {
return ""
}

private fun createDownloadFile(fileName: String): File {
val downloadsDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
val plainAppDirectory = File(downloadsDirectory, "PlainApp")
if (!plainAppDirectory.exists()) {
plainAppDirectory.mkdirs()
private fun createPublicFile(fileName: String, dirName: String): File {
val dir = PathHelper.getPlainPublicDir(dirName)
if (!dir.exists()) {
dir.mkdirs()
}

var file = File(plainAppDirectory, fileName)
var file = File(dir, fileName)
if (file.exists()) {
file = file.newFile()
}

return file
}
}
23 changes: 21 additions & 2 deletions app/src/main/java/com/ismartcoding/plain/helpers/ImageHelper.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.ismartcoding.plain.helpers

import android.graphics.BitmapFactory
import androidx.compose.ui.unit.IntSize
import androidx.exifinterface.media.ExifInterface
import com.ismartcoding.lib.extensions.getFilenameExtension
import com.ismartcoding.lib.logcat.LogCat
Expand Down Expand Up @@ -37,7 +39,7 @@ object ImageHelper {
return ImageType.SVG
} else if (extension == "png") {
return ImageType.PNG
}else if (extension == "jpg" || extension == "jpeg") {
} else if (extension == "jpg" || extension == "jpeg") {
return ImageType.JPG
}

Expand Down Expand Up @@ -97,7 +99,7 @@ object ImageHelper {
return ImageType.UNKNOWN
}

fun getImageRotation(path: String): Int {
fun getRotation(path: String): Int {
if (path.endsWith(".svg", true)) {
return 0
}
Expand All @@ -116,4 +118,21 @@ object ImageHelper {
}
return 0
}

fun getIntrinsicSize(path: String, rotation: Int): IntSize {
val size = if (path.endsWith(".svg", true)) {
SvgHelper.getSize(path)
} else {
val options = BitmapFactory.Options()
options.inJustDecodeBounds = true
BitmapFactory.decodeFile(path, options)
IntSize(options.outWidth, options.outHeight)
}

if (rotation == 90 || rotation == 270) {
return IntSize(size.height, size.width)
}

return size
}
}
11 changes: 11 additions & 0 deletions app/src/main/java/com/ismartcoding/plain/helpers/PathHelper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.ismartcoding.plain.helpers

import android.os.Environment
import java.io.File

object PathHelper {
fun getPlainPublicDir(dirName: String): File {
val dir = Environment.getExternalStoragePublicDirectory(dirName)
return File(dir, "PlainApp")
}
}
Loading

0 comments on commit 21eb950

Please sign in to comment.