Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Path Listener #17

Merged
merged 1 commit into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Expand Up @@ -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(
Expand All @@ -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")
}


}
Expand Down
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
Expand Up @@ -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
Expand Down
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
Expand Up @@ -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 =
Expand Down Expand Up @@ -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)
}
}

Expand Down
Loading