@@ -31,10 +31,14 @@ import com.github.droidworksstudio.common.showLongToast
31
31
import com.github.droidworksstudio.launcher.BuildConfig
32
32
import com.github.droidworksstudio.launcher.R
33
33
import com.github.droidworksstudio.launcher.accessibility.ActionService
34
+ import com.github.droidworksstudio.launcher.data.dao.AppInfoDAO
35
+ import com.github.droidworksstudio.launcher.data.entities.AppInfo
34
36
import com.github.droidworksstudio.launcher.helper.weather.WeatherResponse
35
37
import com.github.droidworksstudio.launcher.utils.Constants
36
38
import com.github.droidworksstudio.launcher.utils.WeatherApiService
37
39
import com.google.gson.Gson
40
+ import com.google.gson.reflect.TypeToken
41
+ import kotlinx.coroutines.flow.first
38
42
import retrofit2.Retrofit
39
43
import retrofit2.converter.gson.GsonConverterFactory
40
44
import java.net.UnknownHostException
@@ -316,7 +320,7 @@ class AppHelper @Inject constructor() {
316
320
fun storeFile (activity : Activity ) {
317
321
// Generate a unique filename with a timestamp
318
322
val timeStamp = SimpleDateFormat (" yyyyMMdd_HHmmss" , Locale .getDefault()).format(Date ())
319
- val fileName = " backup_ $timeStamp .json"
323
+ val fileName = " Settings_backup_ $timeStamp .json"
320
324
321
325
val intent = Intent (Intent .ACTION_CREATE_DOCUMENT ).apply {
322
326
addCategory(Intent .CATEGORY_OPENABLE )
@@ -334,6 +338,67 @@ class AppHelper @Inject constructor() {
334
338
activity.startActivityForResult(intent, Constants .BACKUP_READ , null )
335
339
}
336
340
341
+ fun storeFileApps (activity : Activity ) {
342
+ // Generate a unique filename with a timestamp
343
+ val timeStamp = SimpleDateFormat (" yyyyMMdd_HHmmss" , Locale .getDefault()).format(Date ())
344
+ val fileName = " Apps_backup_$timeStamp .json"
345
+
346
+ val intent = Intent (Intent .ACTION_CREATE_DOCUMENT ).apply {
347
+ addCategory(Intent .CATEGORY_OPENABLE )
348
+ type = " application/json"
349
+ putExtra(Intent .EXTRA_TITLE , fileName)
350
+ }
351
+ activity.startActivityForResult(intent, Constants .BACKUP_WRITE_APPS , null )
352
+ }
353
+
354
+ fun loadFileApps (activity : Activity ) {
355
+ val intent = Intent (Intent .ACTION_OPEN_DOCUMENT ).apply {
356
+ addCategory(Intent .CATEGORY_OPENABLE )
357
+ type = " application/json"
358
+ }
359
+ activity.startActivityForResult(intent, Constants .BACKUP_READ_APPS , null )
360
+ }
361
+
362
+ suspend fun backupAppInfo (context : Context , dao : AppInfoDAO , uri : Uri ) {
363
+ try {
364
+ dao.getAllAppsFlow()
365
+ .first() // Get the first emission from the Flow
366
+ .let { allApps ->
367
+ val gson = Gson ()
368
+ val jsonString = gson.toJson(allApps)
369
+
370
+ // Write JSON to the selected URI
371
+ context.contentResolver.openOutputStream(uri)?.use { outputStream ->
372
+ outputStream.write(jsonString.toByteArray())
373
+ } ? : throw Exception (" Failed to open output stream" )
374
+ }
375
+ } catch (e: Exception ) {
376
+ e.printStackTrace()
377
+ }
378
+ }
379
+
380
+ suspend fun restoreAppInfo (context : Context , dao : AppInfoDAO , uri : Uri ) {
381
+ try {
382
+ // Open an InputStream from the selected Uri
383
+ context.contentResolver.openInputStream(uri)?.use { inputStream ->
384
+ // Read the content from the InputStream
385
+ val jsonString = inputStream.bufferedReader().use { it.readText() }
386
+
387
+ // Convert JSON to List<AppInfo>
388
+ val gson = Gson ()
389
+ val type = object : TypeToken <List <AppInfo >>() {}.type
390
+ val appInfoList: List <AppInfo > = gson.fromJson(jsonString, type)
391
+
392
+ // Reinsert data into the database
393
+ dao.restoreAll(appInfoList)
394
+ } ? : throw Exception (" Failed to open input stream from URI" )
395
+
396
+ } catch (e: Exception ) {
397
+ e.printStackTrace()
398
+ }
399
+ }
400
+
401
+
337
402
fun getActionType (actionType : Constants .Swipe ): NavOptions {
338
403
return when (actionType) {
339
404
Constants .Swipe .DoubleTap -> {
0 commit comments