Skip to content

Commit d81212b

Browse files
committed
initial commit
1 parent 9fb9526 commit d81212b

File tree

16 files changed

+498
-140
lines changed

16 files changed

+498
-140
lines changed

app/src/main/assets/CoGoTooltips

66.3 MB
Binary file not shown.

app/src/main/java/com/itsaky/androidide/activities/MainActivity.kt

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717

1818
package com.itsaky.androidide.activities
1919

20+
import android.content.Context
2021
import android.content.Intent
2122
import android.os.Bundle
2223
import android.text.TextUtils
24+
import android.util.Log
2325
import android.view.View
2426
import androidx.activity.OnBackPressedCallback
2527
import androidx.activity.viewModels
@@ -31,6 +33,7 @@ import com.google.android.material.transition.MaterialSharedAxis
3133
import com.itsaky.androidide.activities.editor.EditorActivityKt
3234
import com.itsaky.androidide.app.EdgeToEdgeIDEActivity
3335
import com.itsaky.androidide.databinding.ActivityMainBinding
36+
import com.itsaky.androidide.idetooltips.DocumentationDatabase
3437
import com.itsaky.androidide.preferences.internal.GeneralPreferences
3538
import com.itsaky.androidide.projects.ProjectManagerImpl
3639
import com.itsaky.androidide.resources.R.string
@@ -46,9 +49,15 @@ import com.itsaky.androidide.viewmodel.MainViewModel.Companion.SCREEN_TEMPLATE_L
4649
import com.itsaky.androidide.viewmodel.MainViewModel.Companion.TOOLTIPS_WEB_VIEW
4750
//import io.sentry.Sentry
4851
import java.io.File
52+
import java.io.FileOutputStream
53+
import java.io.IOException
54+
import java.io.InputStream
4955

5056
class MainActivity : EdgeToEdgeIDEActivity() {
5157

58+
private val DATABASENAME = "documentation"
59+
private val TAG = "MainActivity"
60+
5261
private val viewModel by viewModels<MainViewModel>()
5362
private var _binding: ActivityMainBinding? = null
5463

@@ -88,7 +97,9 @@ class MainActivity : EdgeToEdgeIDEActivity() {
8897

8998
override fun onCreate(savedInstanceState: Bundle?) {
9099
super.onCreate(savedInstanceState)
91-
100+
101+
transferDatabaseFromAssets(this, DATABASENAME)
102+
92103
openLastProject()
93104

94105
viewModel.currentScreen.observe(this) { screen ->
@@ -113,6 +124,54 @@ class MainActivity : EdgeToEdgeIDEActivity() {
113124
instance = this
114125
}
115126

127+
/**
128+
* Transfers a database from the assets folder to the device's internal storage.
129+
*
130+
* @param context The application context.
131+
* @param databaseName The name of the database file in the assets folder (e.g., "mydatabase.db").
132+
* @return true if the database was transferred successfully, false otherwise.
133+
*/
134+
fun transferDatabaseFromAssets(context: Context, databaseName: String): Boolean {
135+
val dbPath = context.getDatabasePath(databaseName)
136+
137+
// Check if the database already exists in internal storage.
138+
if (dbPath.exists()) {
139+
Log.d(TAG, "Database already exists at ${dbPath.absolutePath}")
140+
return true // Or false, depending on your desired behavior if the file exists
141+
}
142+
143+
// Ensure the directory exists.
144+
val dbDir = File(dbPath.parent!!) // Use non-null assertion as getDatabasePath's parent is never null
145+
if (!dbDir.exists()) {
146+
if (!dbDir.mkdirs()) {
147+
Log.e(TAG, "Failed to create database directory: ${dbDir.absolutePath}")
148+
return false
149+
}
150+
Log.d(TAG, "Database directory created at ${dbDir.absolutePath}")
151+
}
152+
153+
// Copy the database file from assets to internal storage.
154+
try {
155+
val inputStream: InputStream = context.assets.open("database/$databaseName") // Corrected path
156+
val outputStream = FileOutputStream(dbPath)
157+
val buffer = ByteArray(8192) // Use a reasonable buffer size
158+
var length: Int
159+
while (inputStream.read(buffer).also { length = it } > 0) {
160+
outputStream.write(buffer, 0, length)
161+
}
162+
outputStream.flush()
163+
outputStream.close()
164+
inputStream.close()
165+
Log.d(TAG, "Database copied successfully to ${dbPath.absolutePath}")
166+
return true
167+
} catch (e: IOException) {
168+
Log.e(TAG, "Failed to copy database: ${e.message}")
169+
e.printStackTrace() // Print the stack trace to help with debugging
170+
return false
171+
}
172+
}
173+
174+
116175
override fun onApplySystemBarInsets(insets: Insets) {
117176
binding.fragmentContainersParent.setPadding(
118177
insets.left, 0, insets.right, insets.bottom

app/src/main/java/com/itsaky/androidide/activities/editor/EditorHandlerActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ open class EditorHandlerActivity : ProjectHandlerActivity(), IEditorHandler {
696696
editorActivityScope.launch {
697697
val files = editorViewModel.getOpenedFiles()
698698
val dupliCount = mutableMapOf<String, Int>()
699-
val names = MutableIntObjectMap<Pair<String, @DrawableRes Int>>()
699+
val names = MutableIntObjectMap<Pair<String, @receiver:DrawableRes Int>>()
700700
val nameBuilder = UniqueNameBuilder<File>("", File.separator)
701701

702702
files.forEach {

app/src/main/java/com/itsaky/androidide/fragments/IDETooltipWebViewFragment.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package com.itsaky.androidide.fragments
1919

20+
import android.annotation.SuppressLint
2021
import android.os.Bundle
2122
import android.util.Log
2223
import android.view.LayoutInflater
@@ -27,6 +28,7 @@ import android.webkit.WebResourceRequest
2728
import android.webkit.WebView
2829
import android.webkit.WebViewClient
2930
import androidx.activity.OnBackPressedCallback
31+
import androidx.core.view.isVisible
3032
import androidx.fragment.app.Fragment
3133
import com.itsaky.androidide.R
3234
import java.net.URL
@@ -36,6 +38,7 @@ class IDETooltipWebviewFragment : Fragment() {
3638
private lateinit var webView: WebView
3739
private lateinit var website : String
3840

41+
@SuppressLint("SetJavaScriptEnabled")
3942
override fun onCreateView(
4043
inflater: LayoutInflater,
4144
container: ViewGroup?,
@@ -110,9 +113,12 @@ class IDETooltipWebviewFragment : Fragment() {
110113
override fun onDestroyView() {
111114
super.onDestroyView()
112115
// Clean up the WebView in Fragment
113-
webView.clearHistory()
114-
webView.loadUrl("about:blank")
116+
if(webView.isVisible) {
117+
webView.clearHistory()
118+
webView.loadUrl("about:blank")
119+
}
115120
webView.destroy()
121+
116122
}
117123

118124
companion object {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.itsaky.androidide.idetooltips
2+
3+
import androidx.room.Dao
4+
import androidx.room.Delete
5+
import androidx.room.Insert
6+
import androidx.room.OnConflictStrategy
7+
import androidx.room.Query
8+
import kotlinx.coroutines.flow.Flow
9+
10+
// Define the Data Access Objects (DAOs) (same as before)
11+
@Dao
12+
interface ContentDao {
13+
@Query("SELECT * FROM Content")
14+
fun getAll(): Flow<List<Content>> // Use Flow for reactive updates
15+
16+
@Query("SELECT * FROM Content LIMIT 1")
17+
fun getOne(): List<Content> // Use Flow for reactive updates
18+
19+
@Query("SELECT * FROM Content WHERE path = :path")
20+
fun getContent(path: String): Content? // Use Flow for reactive updates
21+
22+
@Query("SELECT * FROM Content WHERE path = :path AND languageID = :languageId")
23+
suspend fun getContentByPathAndLanguage(path: String, languageId: Int): Content?
24+
25+
@Insert(onConflict = OnConflictStrategy.IGNORE)
26+
suspend fun insert(content: Content)
27+
28+
@Delete
29+
suspend fun delete(content: Content)
30+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.itsaky.androidide.idetooltips
2+
3+
import androidx.room.ColumnInfo
4+
import androidx.room.Entity
5+
import androidx.room.ForeignKey
6+
7+
// Define the data entities (same as before)
8+
@Entity(
9+
tableName = "Content",
10+
primaryKeys = ["path", "languageID"],
11+
foreignKeys = [
12+
ForeignKey(entity = Language::class, parentColumns = ["id"], childColumns = ["languageID"]),
13+
ForeignKey(entity = ContentType::class, parentColumns = ["id"], childColumns = ["contentTypeID"])
14+
]
15+
)
16+
data class Content(
17+
@ColumnInfo(name = "path") val path: String,
18+
@ColumnInfo(name = "languageID") val languageID: Int,
19+
@ColumnInfo(name = "content") val content: ByteArray,
20+
@ColumnInfo(name = "contentTypeID") val contentTypeID: Int
21+
) {
22+
override fun equals(other: Any?): Boolean {
23+
if (this === other) return true
24+
if (javaClass != other?.javaClass) return false
25+
26+
other as Content
27+
28+
if (languageID != other.languageID) return false
29+
if (contentTypeID != other.contentTypeID) return false
30+
if (path != other.path) return false
31+
if (!content.contentEquals(other.content)) return false
32+
33+
return true
34+
}
35+
36+
override fun hashCode(): Int {
37+
var result = languageID
38+
result = 31 * result + contentTypeID
39+
result = 31 * result + path.hashCode()
40+
result = 31 * result + content.contentHashCode()
41+
return result
42+
}
43+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.itsaky.androidide.idetooltips
2+
3+
import androidx.room.Dao
4+
import androidx.room.Insert
5+
import androidx.room.OnConflictStrategy
6+
import androidx.room.Query
7+
import kotlinx.coroutines.flow.Flow
8+
9+
@Dao
10+
interface ContentTypeDao {
11+
@Query("SELECT * FROM ContentTypes")
12+
fun getAll(): Flow<List<ContentType>>
13+
14+
@Query("SELECT * FROM ContentTypes WHERE id = :id")
15+
fun getContentTypeById(id: Int): ContentType?
16+
17+
@Insert(onConflict = OnConflictStrategy.IGNORE)
18+
fun insert(contentType: ContentType): Long
19+
20+
@Query("SELECT * FROM ContentTypes WHERE `value` = :value")
21+
fun getContentTypeByValue(value: String): ContentType?
22+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.itsaky.androidide.idetooltips
2+
3+
import androidx.room.ColumnInfo
4+
import androidx.room.Entity
5+
import androidx.room.PrimaryKey
6+
7+
@Entity(tableName = "ContentTypes")
8+
data class ContentType(
9+
@PrimaryKey(autoGenerate = true) val id: Int? = 0,
10+
@ColumnInfo(name = "value") val value: String,
11+
@ColumnInfo(name = "compression") val compression: String
12+
)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.itsaky.androidide.idetooltips
2+
3+
import android.content.Context
4+
import androidx.room.Database
5+
import androidx.room.Room
6+
import androidx.room.RoomDatabase
7+
8+
// Define the Room Database (using KSP for Room)
9+
@Database(entities = [Content::class, Language::class, ContentType::class], version = 1, exportSchema = false)
10+
abstract class DocumentationDatabase : RoomDatabase() {
11+
abstract fun contentDao(): ContentDao
12+
abstract fun languageDao():LanguageDao
13+
abstract fun contentTypeDao(): ContentTypeDao
14+
15+
companion object {
16+
@Volatile
17+
private var INSTANCE: DocumentationDatabase? = null
18+
19+
fun getDatabase(context: Context): DocumentationDatabase {
20+
return INSTANCE ?: synchronized(this) {
21+
val instance = Room.databaseBuilder(
22+
context.applicationContext,
23+
DocumentationDatabase::class.java,
24+
"documentation"
25+
).build()
26+
INSTANCE = instance
27+
instance
28+
}
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)