Skip to content

Commit 8de8976

Browse files
- update: import and export handlers moved into the composables
1 parent be3696a commit 8de8976

File tree

6 files changed

+64
-117
lines changed

6 files changed

+64
-117
lines changed

.idea/vcs.xml

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/main/java/com/afkanerd/deku/DefaultSMS/MainActivity.kt

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ import androidx.window.layout.WindowLayoutInfo
5151
import androidx.window.layout.WindowMetricsCalculator
5252
import com.afkanerd.deku.DefaultSMS.AdaptersViewModels.ConversationsViewModel
5353
import com.afkanerd.deku.DefaultSMS.AdaptersViewModels.SearchViewModel
54-
import com.afkanerd.deku.DefaultSMS.Models.ExportImportHandlers
5554
import com.afkanerd.deku.DefaultSMS.ui.ComposeNewMessage
5655
import com.afkanerd.deku.DefaultSMS.ui.ContactDetails
5756
import com.afkanerd.deku.DefaultSMS.ui.Conversations
@@ -135,60 +134,6 @@ class MainActivity : AppCompatActivity(){
135134
checkIsDefault()
136135
}
137136

138-
139-
override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) {
140-
super.onActivityResult(requestCode, resultCode, resultData)
141-
if(resultCode == RESULT_OK) {
142-
resultData?.let {
143-
val uri: Uri? = resultData.data
144-
// Perform operations on the document using its URI.
145-
146-
uri?.let {
147-
CoroutineScope(Dispatchers.Default).launch {
148-
if (requestCode == ExportImportHandlers.exportRequestCode) {
149-
with(contentResolver.openFileDescriptor(uri, "w")) {
150-
this?.fileDescriptor.let { fd ->
151-
val fileOutputStream = FileOutputStream(fd);
152-
fileOutputStream.write(conversationViewModel
153-
.getAllExport(applicationContext).encodeToByteArray());
154-
// Let the document provider know you're done by closing the stream.
155-
fileOutputStream.close();
156-
}
157-
this?.close();
158-
159-
runOnUiThread {
160-
Toast.makeText(applicationContext,
161-
getString(R.string.conversations_exported_complete),
162-
Toast.LENGTH_LONG).show();
163-
}
164-
}
165-
}
166-
else if(requestCode == ExportImportHandlers.importRequestCode) {
167-
val stringBuilder = StringBuilder()
168-
contentResolver.openInputStream(uri)?.use { inputStream ->
169-
BufferedReader(InputStreamReader(inputStream)).use { reader ->
170-
var line: String? = reader.readLine()
171-
while (line != null) {
172-
stringBuilder.append(line)
173-
line = reader.readLine()
174-
}
175-
}
176-
}
177-
conversationViewModel.importDetails = stringBuilder.toString()
178-
// conversationViewModel.importAll(applicationContext,
179-
// stringBuilder.toString())
180-
// runOnUiThread {
181-
// Toast.makeText(applicationContext,
182-
// getString(R.string.conversations_exported_complete),
183-
// Toast.LENGTH_LONG).show();
184-
// }
185-
}
186-
}
187-
}
188-
}
189-
}
190-
}
191-
192137
fun onLayoutInfoChanged(newLayoutInfo: WindowLayoutInfo) {
193138
conversationViewModel.newLayoutInfo = newLayoutInfo
194139
setContent {

app/src/main/java/com/afkanerd/deku/DefaultSMS/Models/ExportImportHandlers.kt

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

app/src/main/java/com/afkanerd/deku/DefaultSMS/ui/ThreadsConversationMain.kt

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@ package com.afkanerd.deku.DefaultSMS.ui
22

33
import android.app.Activity.RESULT_OK
44
import android.content.Context
5+
import android.content.ContextWrapper
56
import android.content.Intent
7+
import android.net.Uri
68
import android.provider.BlockedNumberContract
79
import android.provider.ContactsContract
810
import android.provider.Telephony
911
import android.text.InputType
1012
import android.util.Log
1113
import android.widget.Toast
1214
import androidx.activity.compose.BackHandler
15+
import androidx.activity.compose.rememberLauncherForActivityResult
1316
import androidx.activity.result.contract.ActivityResultContracts
1417
import androidx.appcompat.app.AppCompatActivity
1518
import androidx.compose.animation.expandIn
@@ -132,7 +135,6 @@ import com.afkanerd.deku.DefaultSMS.Models.Contacts
132135
import com.afkanerd.deku.DefaultSMS.Models.Conversations.Conversation
133136
import com.afkanerd.deku.DefaultSMS.Models.Conversations.ThreadedConversations
134137
import com.afkanerd.deku.DefaultSMS.Models.Conversations.ThreadedConversationsHandler
135-
import com.afkanerd.deku.DefaultSMS.Models.ExportImportHandlers
136138
import com.afkanerd.deku.DefaultSMS.Models.Notifications
137139
import com.afkanerd.deku.DefaultSMS.Models.SIMHandler
138140
import com.afkanerd.deku.DefaultSMS.Models.ThreadsCount
@@ -149,6 +151,9 @@ import kotlinx.coroutines.CoroutineScope
149151
import kotlinx.coroutines.Dispatchers
150152
import kotlinx.coroutines.coroutineScope
151153
import kotlinx.coroutines.launch
154+
import java.io.BufferedReader
155+
import java.io.FileOutputStream
156+
import java.io.InputStreamReader
152157
import kotlin.math.exp
153158

154159
enum class InboxType(val value: Int) {
@@ -414,11 +419,61 @@ fun ModalDrawerSheetLayout(
414419
@Composable
415420
private fun MainDropDownMenu(
416421
expanded: Boolean = false,
417-
importCallback: (() -> Unit)? = null,
422+
conversationViewModel: ConversationsViewModel = ConversationsViewModel(),
418423
dismissCallback: ((Boolean) -> Unit)? = null,
419424
) {
420425
val context = LocalContext.current
421426

427+
val exportLauncher = rememberLauncherForActivityResult(
428+
ActivityResultContracts.CreateDocument("application/json")) { uri ->
429+
println(uri)
430+
uri?.let {
431+
CoroutineScope(Dispatchers.IO).launch {
432+
with(context.contentResolver.openFileDescriptor(uri, "w")) {
433+
this?.fileDescriptor.let { fd ->
434+
val fileOutputStream = FileOutputStream(fd);
435+
fileOutputStream.write(conversationViewModel
436+
.getAllExport(context).encodeToByteArray());
437+
// Let the document provider know you're done by closing the stream.
438+
fileOutputStream.close();
439+
}
440+
this?.close();
441+
442+
CoroutineScope(Dispatchers.Main).launch {
443+
Toast.makeText(context,
444+
context.getString(R.string.conversations_exported_complete),
445+
Toast.LENGTH_LONG).show();
446+
}
447+
}
448+
}
449+
}
450+
}
451+
452+
val importLauncher = rememberLauncherForActivityResult(
453+
ActivityResultContracts.GetContent()) { uri ->
454+
println(uri)
455+
uri?.let {
456+
CoroutineScope(Dispatchers.IO).launch {
457+
val stringBuilder = StringBuilder()
458+
context.contentResolver.openInputStream(uri)?.use { inputStream ->
459+
BufferedReader(InputStreamReader(inputStream)).use { reader ->
460+
var line: String? = reader.readLine()
461+
while (line != null) {
462+
stringBuilder.append(line)
463+
line = reader.readLine()
464+
}
465+
}
466+
}
467+
conversationViewModel.importDetails = stringBuilder.toString()
468+
CoroutineScope(Dispatchers.Main).launch {
469+
Toast.makeText(context,
470+
context.getString(R.string.conversations_import_complete),
471+
Toast.LENGTH_LONG).show();
472+
}
473+
}
474+
}
475+
}
476+
422477
Box(modifier = Modifier
423478
.fillMaxWidth()
424479
.wrapContentSize(Alignment.TopEnd)
@@ -470,7 +525,8 @@ private fun MainDropDownMenu(
470525
},
471526
onClick = {
472527
dismissCallback?.let { it(false) }
473-
ExportImportHandlers.exportInbox(context)
528+
val filename = "Deku_SMS_All_Backup" + System.currentTimeMillis() + ".json";
529+
exportLauncher.launch(filename)
474530
}
475531
)
476532

@@ -484,8 +540,7 @@ private fun MainDropDownMenu(
484540
},
485541
onClick = {
486542
dismissCallback?.let { it(false) }
487-
// ExportImportHandlers.importInbox(context)
488-
importCallback?.invoke()
543+
importLauncher.launch("application/json")
489544
}
490545
)
491546

@@ -509,6 +564,8 @@ private fun MainDropDownMenu(
509564
}
510565
}
511566

567+
568+
512569
@OptIn(ExperimentalMaterial3Api::class, ExperimentalMaterialApi::class,
513570
ExperimentalFoundationApi::class
514571
)
@@ -630,9 +687,6 @@ fun ThreadConversationLayout(
630687

631688
MainDropDownMenu(
632689
expanded=rememberMenuExpanded,
633-
importCallback = {
634-
ExportImportHandlers.importInbox(context)
635-
}
636690
) {
637691
rememberMenuExpanded = it
638692
}

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@
260260

261261
<string name="configs_load_natives" translatable="false">LOAD NATIVES</string>
262262
<string name="conversations_exported_complete">Export Complete!</string>
263+
<string name="conversations_import_complete">Import Complete!</string>
263264
<string name="gateway_client_project_listing_project_name_title">Project name</string>
264265
<string name="gateway_client_project_listing_no_projects_added">No Projects added</string>
265266
<!-- TODO: Remove or change this placeholder text -->

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ hkdf = "2.0.0"
2424
javaWebsocket = "1.5.5"
2525
junit = "4.13.2"
2626
junitVersion = "1.2.1"
27-
kotlin = "2.1.0"
27+
kotlin = "2.0.0"
2828
kotlinxCoroutinesTest = "1.9.0"
2929
kotlinxSerializationJson = "1.6.3"
3030
ktx = "1.15.0"

0 commit comments

Comments
 (0)