Skip to content

Update Options #32

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

Merged
merged 4 commits into from
Jun 21, 2025
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
13 changes: 8 additions & 5 deletions app/src/main/java/org/cssnr/zipline/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ class MainActivity : AppCompatActivity() {
// TODO: Cleanup the logic for handling MAIN intent...
val currentDestinationId = navController.currentDestination?.id
Log.d("onNewIntent", "currentDestinationId: $currentDestinationId")
val launcherAction = preferences.getString("launcher_action", null)
Log.d("onNewIntent", "launcherAction: $launcherAction")
//val launcherAction = preferences.getString("launcher_action", null)
//Log.d("onNewIntent", "launcherAction: $launcherAction")
val fromShortcut = intent.getStringExtra("fromShortcut")
Log.d("onNewIntent", "fromShortcut: $fromShortcut")
Log.d("onNewIntent", "nav_item_preview: ${R.id.nav_item_upload}")
Expand All @@ -178,10 +178,10 @@ class MainActivity : AppCompatActivity() {
.setPopUpTo(navController.graph.id, true)
.build()
)
} else if (currentDestinationId != R.id.nav_item_home && launcherAction != "previous") {
Log.i("onNewIntent", "HOME SETTING SET - Navigating to HomeFragment")
navController.navigate(R.id.nav_item_home)
}
//} else if (currentDestinationId != R.id.nav_item_home && launcherAction != "previous") {
// Log.i("onNewIntent", "HOME SETTING SET - Navigating to HomeFragment")
// navController.navigate(R.id.nav_item_home)
// TODO: Determine if this needs to be in the above if/else
if (fromShortcut == "upload") {
Log.d("onNewIntent", "filePickerLauncher.launch")
Expand Down Expand Up @@ -266,7 +266,10 @@ class MainActivity : AppCompatActivity() {
} else {
Toast.makeText(this, "That's a Bug!", Toast.LENGTH_LONG).show()
Log.w("onNewIntent", "UNKNOWN INTENT - action: $action")

}
//} else if ("RECENT_FILE" == action) {
// Log.d("handleIntent", "RECENT_FILE")
}

override fun onStop() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ import retrofit2.http.Header
import retrofit2.http.Multipart
import retrofit2.http.POST
import retrofit2.http.Part
import retrofit2.http.Query
import java.io.InputStream
import java.net.URLConnection

class ZiplineApi(private val context: Context, url: String? = null) {
class ServerApi(private val context: Context, url: String? = null) {

val api: ApiService
private var ziplineUrl: String
Expand Down Expand Up @@ -97,7 +98,7 @@ class ZiplineApi(private val context: Context, url: String? = null) {
return response
}

suspend fun upload(fileName: String, inputStream: InputStream): Response<FileResponse> {
suspend fun upload(fileName: String, inputStream: InputStream): Response<UploadedFiles> {
Log.d("Api[upload]", "fileName: $fileName")
val fileNameFormat = preferences.getString("file_name_format", null) ?: "random"
Log.d("Api[upload]", "fileNameFormat: $fileNameFormat")
Expand Down Expand Up @@ -126,6 +127,19 @@ class ZiplineApi(private val context: Context, url: String? = null) {
return response
}

suspend fun recent(take: String = "3"): Response<List<FileResponse>> {
Log.d("Api[stats]", "stats")
val response = api.getRecent(take)
if (response.code() == 401) {
val token = reAuthenticate(api, ziplineUrl)
Log.d("Api[upload]", "reAuthenticate: token: $token")
if (token != null) {
return api.getRecent()
}
}
return response
}

private suspend fun reAuthenticate(api: ApiService, ziplineUrl: String): String? {
return try {
val cookies = CookieManager.getInstance().getCookie(ziplineUrl)
Expand Down Expand Up @@ -196,12 +210,17 @@ class ZiplineApi(private val context: Context, url: String? = null) {
@GET("user/stats")
suspend fun getStats(): Response<StatsResponse>

@GET("user/recent")
suspend fun getRecent(
@Query("take") take: String = "3"
): Response<List<FileResponse>>

@Multipart
@POST("upload")
suspend fun postUpload(
@Header("x-zipline-format") format: String,
@Part file: MultipartBody.Part,
): Response<FileResponse>
): Response<UploadedFiles>

@POST("user/urls")
suspend fun postShort(
Expand All @@ -221,12 +240,12 @@ class ZiplineApi(private val context: Context, url: String? = null) {
)

@JsonClass(generateAdapter = true)
data class FileResponse(
val files: List<UploadedFile>,
data class UploadedFiles(
val files: List<UploadResponse>,
)

@JsonClass(generateAdapter = true)
data class UploadedFile(
data class UploadResponse(
val id: String,
val type: String,
val url: String,
Expand Down Expand Up @@ -266,6 +285,24 @@ class ZiplineApi(private val context: Context, url: String? = null) {
@Json(name = "urlViews") val urlViews: Int,
)

@JsonClass(generateAdapter = true)
data class FileResponse(
@Json(name = "createdAt") val createdAt: String,
@Json(name = "updatedAt") val updatedAt: String,
@Json(name = "deletesAt") val deletesAt: String?,
@Json(name = "favorite") val favorite: Boolean,
@Json(name = "id") val id: String,
@Json(name = "originalName") val originalName: String?,
@Json(name = "name") val name: String,
@Json(name = "size") val size: Int,
@Json(name = "type") val type: String,
@Json(name = "views") val views: Int,
@Json(name = "maxViews") val maxViews: Int?,
@Json(name = "folderId") val folderId: String?,
@Json(name = "thumbnail") val thumbnail: String?,
@Json(name = "password") val password: String?,
@Json(name = "url") val url: String
)

inner class SimpleCookieJar : CookieJar {
private val cookieStore = mutableMapOf<String, List<Cookie>>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ class SettingsFragment : PreferenceFragmentCompat() {
val fileNameFormat = findPreference<ListPreference>("file_name_format")
fileNameFormat?.summaryProvider = ListPreference.SimpleSummaryProvider.getInstance()

// Launcher Icon Action
val launcherAction = findPreference<ListPreference>("launcher_action")
launcherAction?.summaryProvider = ListPreference.SimpleSummaryProvider.getInstance()
//// Launcher Icon Action
//val launcherAction = findPreference<ListPreference>("launcher_action")
//launcherAction?.summaryProvider = ListPreference.SimpleSummaryProvider.getInstance()

// Widget Settings
findPreference<Preference>("open_widget_settings")?.setOnPreferenceClickListener {
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/org/cssnr/zipline/ui/setup/SetupFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import com.google.firebase.ktx.Firebase
import kotlinx.coroutines.launch
import org.cssnr.zipline.MainActivity
import org.cssnr.zipline.R
import org.cssnr.zipline.api.ZiplineApi
import org.cssnr.zipline.api.ServerApi
import org.cssnr.zipline.databinding.FragmentSetupBinding

class SetupFragment : Fragment() {
Expand Down Expand Up @@ -118,7 +118,7 @@ class SetupFragment : Fragment() {

Log.d("loginButton", "lifecycleScope.launch")
lifecycleScope.launch {
val api = ZiplineApi(ctx, host)
val api = ServerApi(ctx, host)
val token = api.login(host, user, pass)
Log.d("loginButton", "token: $token")
if (token.isNullOrEmpty()) {
Expand Down
86 changes: 5 additions & 81 deletions app/src/main/java/org/cssnr/zipline/ui/upload/ShortFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.cssnr.zipline.R
import org.cssnr.zipline.api.ZiplineApi
import org.cssnr.zipline.api.ServerApi
import org.cssnr.zipline.copyToClipboard
import org.cssnr.zipline.databinding.FragmentShortBinding

Expand Down Expand Up @@ -55,13 +55,6 @@ class ShortFragment : Fragment() {

navController = findNavController()

//val callback = object : OnBackPressedCallback(true) {
// override fun handleOnBackPressed() {
// requireActivity().finish()
// }
//}
//requireActivity().onBackPressedDispatcher.addCallback(viewLifecycleOwner, callback)

val url = requireArguments().getString("url")
Log.d("Short[onViewCreated]", "url: $url")

Expand Down Expand Up @@ -127,9 +120,11 @@ class ShortFragment : Fragment() {
Log.d("processShort", "Vanity: $vanityName")

val savedUrl = preferences.getString("ziplineUrl", null)
val authToken = preferences.getString("ziplineToken", null)
Log.d("processShort", "savedUrl: $savedUrl")
val authToken = preferences.getString("ziplineToken", null)
Log.d("processShort", "authToken: $authToken")
val shareUrl = preferences.getBoolean("share_after_short", true)
Log.d("processShort", "shareUrl: $shareUrl")

if (savedUrl == null || authToken == null) {
Log.e("processShort", "ziplineUrl || ziplineToken is null")
Expand All @@ -143,7 +138,7 @@ class ShortFragment : Fragment() {
return
}

val api = ZiplineApi(requireContext())
val api = ServerApi(requireContext())
lifecycleScope.launch {
val response = api.shorten(longUrl, vanityName)
Log.d("processShort", "response: $response")
Expand All @@ -152,8 +147,6 @@ class ShortFragment : Fragment() {
if (shortResponse != null) {
Log.d("processShort", "shortResponse.url: ${shortResponse.url}")
copyToClipboard(requireContext(), shortResponse.url)
val shareUrl = preferences.getBoolean("share_after_short", true)
Log.d("processShort", "shareUrl: $shareUrl")
if (shareUrl) {
val shareIntent = Intent(Intent.ACTION_SEND).apply {
type = "text/plain"
Expand All @@ -179,73 +172,4 @@ class ShortFragment : Fragment() {
}
}
}

// // TODO: This is the DjangoFiles processShort, Old Zipline processShort was used above...
// private fun processShort(url: String, vanity: String?) {
// Log.d("processShort", "url: $url")
// Log.d("processShort", "vanity: $vanity")
// //val preferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE)
// val savedUrl = preferences.getString("ziplineUrl", null)
// Log.d("processShort", "savedUrl: $savedUrl")
// val authToken = preferences.getString("ziplineToken", null)
// Log.d("processShort", "authToken: $authToken")
// if (savedUrl == null || authToken == null) {
// // TODO: Show settings dialog here...
// Log.w("processShort", "Missing OR savedUrl/authToken")
// Toast.makeText(requireContext(), getString(R.string.tst_no_url), Toast.LENGTH_SHORT)
// .show()
// return
// }
// val api = ZiplineApi(requireContext())
// Log.d("processShort", "api: $api")
// Toast.makeText(requireContext(), "Creating Short URL...", Toast.LENGTH_SHORT).show()
// lifecycleScope.launch {
// try {
// val response = api.shorten(url, vanity, savedUrl)
// Log.d("processShort", "response: $response")
// if (response.isSuccessful) {
// val shortResponse = response.body()
// Log.d("processShort", "shortResponse: $shortResponse")
// withContext(Dispatchers.Main) {
// if (shortResponse != null) {
// copyToClipboard(requireContext(), shortResponse.url)
// val shareUrl = sharedPreferences.getBoolean("share_after_short", true)
// Log.d("processShort", "shareUrl: $shareUrl")
// if (shareUrl) {
// val shareIntent = Intent(Intent.ACTION_SEND).apply {
// type = "text/plain"
// putExtra(Intent.EXTRA_TEXT, shortResponse.url)
// }
// startActivity(Intent.createChooser(shareIntent, null))
// }
// navController.navigate(
// R.id.nav_item_home,
// bundleOf("url" to "${savedUrl}/shorts/#shorts-table_wrapper"),
// NavOptions.Builder()
// .setPopUpTo(R.id.nav_graph, inclusive = true)
// .build()
// )
// } else {
// Log.w("processShort", "shortResponse is null")
// val msg = "Unknown Response!"
// Toast.makeText(requireContext(), msg, Toast.LENGTH_LONG).show()
// }
// }
// } else {
// val msg = "Error: ${response.code()}: ${response.message()}"
// Log.w("processShort", "Error: $msg")
// withContext(Dispatchers.Main) {
// Toast.makeText(requireContext(), msg, Toast.LENGTH_LONG).show()
// }
// }
// } catch (e: Exception) {
// e.printStackTrace()
// val msg = e.message ?: "Unknown Error!"
// Log.i("processShort", "msg: $msg")
// withContext(Dispatchers.Main) {
// Toast.makeText(requireContext(), msg, Toast.LENGTH_LONG).show()
// }
// }
// }
// }
}
23 changes: 7 additions & 16 deletions app/src/main/java/org/cssnr/zipline/ui/upload/TextFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.cssnr.zipline.R
import org.cssnr.zipline.api.ZiplineApi
import org.cssnr.zipline.api.ServerApi
import org.cssnr.zipline.copyToClipboard
import org.cssnr.zipline.databinding.FragmentTextBinding

Expand Down Expand Up @@ -59,30 +59,18 @@ class TextFragment : Fragment() {

if (extraText.isEmpty()) {
// TODO: Better Handle this Error
Log.e("Text[onViewCreated]", "extraText is null")
Log.w("Text[onViewCreated]", "extraText is null")
Toast.makeText(requireContext(), "No Text to Process!", Toast.LENGTH_LONG).show()
//return
}

//val preferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
//val savedUrl = preferences.getString("ziplineUrl", null)
//Log.d("Text[onViewCreated]", "savedUrl: $savedUrl")
//val authToken = preferences.getString("ziplineToken", null)
//Log.d("Text[onViewCreated]", "authToken: $authToken")
//if (savedUrl == null) {
// // TODO: Better Handle this Error
// Log.e("Text[onViewCreated]", "savedUrl is null")
// Toast.makeText(requireContext(), "No Server Setup!", Toast.LENGTH_LONG).show()
// return
//}

binding.textContent.setText(extraText)

binding.shareButton.setOnClickListener {
Log.d("shareButton", "setOnClickListener")
val shareIntent = Intent(Intent.ACTION_SEND).apply {
type = "text/plain"
putExtra(Intent.EXTRA_TEXT, extraText)
putExtra(Intent.EXTRA_TEXT, binding.textContent.text)
}
startActivity(Intent.createChooser(shareIntent, null))
}
Expand Down Expand Up @@ -120,6 +108,9 @@ class TextFragment : Fragment() {
Log.d("processUpload", "savedUrl: $savedUrl")
val authToken = preferences.getString("ziplineToken", null)
Log.d("processUpload", "authToken: $authToken")
val shareUrl = preferences.getBoolean("share_after_upload", true)
Log.d("processShort", "shareUrl: $shareUrl")

if (savedUrl == null || authToken == null) {
// TODO: Show settings dialog here...
Log.w("processUpload", "Missing OR savedUrl/authToken/fileName")
Expand All @@ -128,7 +119,7 @@ class TextFragment : Fragment() {
return
}
val inputStream = textContent.byteInputStream()
val api = ZiplineApi(requireContext())
val api = ServerApi(requireContext())
Log.d("processUpload", "api: $api")
Toast.makeText(requireContext(), getString(R.string.tst_uploading_file), Toast.LENGTH_SHORT)
.show()
Expand Down
Loading
Loading