Skip to content

Commit

Permalink
Return storage path in listener added.
Browse files Browse the repository at this point in the history
sagar authored and sagar committed Jun 5, 2024
1 parent 09c1b24 commit 59438dd
Showing 5 changed files with 59 additions and 4 deletions.
34 changes: 34 additions & 0 deletions app/src/main/java/com/ss/smartstorage/Context.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.ss.smartstorage

import android.content.Context
import android.os.Handler
import android.os.Looper
import android.text.TextUtils
import android.widget.Toast


fun Context.toast(id: Int, length: Int = Toast.LENGTH_SHORT) {
toast(getString(id), length)
}

fun Context.toast(msg: String, length: Int = Toast.LENGTH_SHORT) {
try {
if (isOnMainThread()) {
if (!TextUtils.isEmpty(msg)) {
Toast.makeText(applicationContext, msg, length).show()
}
} else {
if (!TextUtils.isEmpty(msg)) {
Handler(Looper.getMainLooper()).post {
if (!TextUtils.isEmpty(msg)) {
Toast.makeText(applicationContext, msg, length).show()
}
}
}
}
} catch (_: Exception) {}
}

fun isOnMainThread() = Looper.myLooper() == Looper.getMainLooper()


15 changes: 12 additions & 3 deletions app/src/main/java/com/ss/smartstorage/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -2,24 +2,25 @@ package com.ss.smartstorage

import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.ui.Modifier
import com.ss.smart_storage.OutputListener
import com.ss.smart_storage.SmartStorage
import com.ss.smartstorage.ui.theme.SmartStorageTheme

class MainActivity : ComponentActivity() {
class MainActivity : ComponentActivity(), OutputListener {

@SuppressLint("NewApi")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val smartStorage = SmartStorage(this)


smartStorage.registerListener(this)
setContent {
SmartStorageTheme {
Surface(
@@ -41,7 +42,15 @@ class MainActivity : ComponentActivity() {
}
}

override fun onSuccess(result: String?) {
result?.let { toast(it) }
Log.e("MainAct", "onSuccess result: $result")
}

override fun onFail(error: String?) {
error?.let { toast(it) }
Log.e("MainAct", "onFail error: $error")
}


}
2 changes: 1 addition & 1 deletion app/src/main/java/com/ss/smartstorage/ui/theme/Theme.kt
Original file line number Diff line number Diff line change
@@ -39,7 +39,7 @@ private val LightColorScheme = lightColorScheme(

@Composable
fun SmartStorageTheme(
darkTheme: Boolean = isSystemInDarkTheme(),
darkTheme: Boolean = false,
// Dynamic color is available on Android 12+
dynamicColor: Boolean = true,
content: @Composable () -> Unit
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.ss.smart_storage

interface OutputListener {
fun onSuccess(result: String?)
fun onFail(error: String?)
}
Original file line number Diff line number Diff line change
@@ -27,7 +27,11 @@ class SmartStorage(private val activity: ComponentActivity) {

private var baseDocumentTreeUri: Uri? = null
private lateinit var fileDetails: FileDetails
private var listener: OutputListener? = null

fun registerListener(listener: OutputListener) {
this.listener = listener
}

@SuppressLint("NewApi")
private val permissionManager =
@@ -130,8 +134,10 @@ class SmartStorage(private val activity: ComponentActivity) {
FileOutputStream(file).use { stream ->
stream.write(fileDetails.fileData)
}
listener?.onSuccess(file.path)
} catch (e: Exception) {
e.printStackTrace()
listener?.onFail(e.message)
}
}

0 comments on commit 59438dd

Please sign in to comment.