delete account

This commit is contained in:
Bnyro 2022-06-25 17:55:03 +02:00
parent 62055b5c21
commit 47c65ebfe8
8 changed files with 178 additions and 19 deletions

View File

@ -0,0 +1,94 @@
package com.github.libretube.dialogs
import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.util.Log
import android.util.TypedValue
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.core.text.HtmlCompat
import androidx.fragment.app.DialogFragment
import androidx.lifecycle.lifecycleScope
import com.github.libretube.R
import com.github.libretube.obj.DeleteUserRequest
import com.github.libretube.util.RetrofitInstance
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import retrofit2.HttpException
import java.io.IOException
class DeleteAccountDialog : DialogFragment() {
private val TAG = "DeleteAccountDialog"
lateinit var username: EditText
lateinit var password: EditText
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity?.let {
val builder = MaterialAlertDialogBuilder(it)
val inflater = requireActivity().layoutInflater
val view = inflater.inflate(R.layout.dialog_login, null)
password = view.findViewById(R.id.delete_password)
view.findViewById<Button>(R.id.delete_account_confirm).setOnClickListener {
if (password.text.toString() != "") {
deleteAccount(password.text.toString())
} else {
Toast.makeText(context, R.string.empty, Toast.LENGTH_SHORT).show()
}
}
val typedValue = TypedValue()
this.requireActivity().theme.resolveAttribute(R.attr.colorPrimaryDark, typedValue, true)
val hexColor = String.format("#%06X", (0xFFFFFF and typedValue.data))
val appName = HtmlCompat.fromHtml(
"Libre<span style='color:$hexColor';>Tube</span>",
HtmlCompat.FROM_HTML_MODE_COMPACT
)
view.findViewById<TextView>(R.id.title).text = appName
builder.setView(view)
builder.create()
} ?: throw IllegalStateException("Activity cannot be null")
}
private fun deleteAccount(password: String) {
fun run() {
lifecycleScope.launchWhenCreated {
val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE)
val token = sharedPref?.getString("token", "")!!
val response = try {
RetrofitInstance.api.deleteAccount(token, DeleteUserRequest(password))
} catch (e: IOException) {
println(e)
Log.e(TAG, "IOException, you might not have internet connection")
Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show()
return@launchWhenCreated
} catch (e: HttpException) {
Log.e(TAG, "HttpException, unexpected response")
Toast.makeText(context, R.string.server_error, Toast.LENGTH_SHORT).show()
return@launchWhenCreated
} catch (e: Exception) {
Log.e(TAG, "dafaq?$e")
return@launchWhenCreated
}
Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show()
logout()
dialog?.dismiss()
}
}
run()
}
private fun logout() {
val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE)
val token = sharedPref?.getString("token", "")
if (token != "") {
with(sharedPref!!.edit()) {
putString("token", "")
apply()
}
}
}
}

View File

@ -0,0 +1,5 @@
package com.github.libretube.obj
data class DeleteUserRequest(
var password: String? = null
)

View File

@ -2,6 +2,7 @@ package com.github.libretube.util
import com.github.libretube.obj.Channel import com.github.libretube.obj.Channel
import com.github.libretube.obj.CommentsPage import com.github.libretube.obj.CommentsPage
import com.github.libretube.obj.DeleteUserRequest
import com.github.libretube.obj.Instances import com.github.libretube.obj.Instances
import com.github.libretube.obj.Login import com.github.libretube.obj.Login
import com.github.libretube.obj.Message import com.github.libretube.obj.Message
@ -86,6 +87,12 @@ interface PipedApi {
@POST("register") @POST("register")
suspend fun register(@Body login: Login): Token suspend fun register(@Body login: Login): Token
@POST("user/delete")
suspend fun deleteAccount(
@Header("Authorization") token: String,
@Body password: DeleteUserRequest
): Message
@GET("feed") @GET("feed")
suspend fun getFeed(@Query("authToken") token: String?): List<StreamItem> suspend fun getFeed(@Query("authToken") token: String?): List<StreamItem>

View File

@ -11,9 +11,10 @@
android:id="@+id/back_imageButton" android:id="@+id/back_imageButton"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginLeft="6dp" android:layout_marginLeft="20dp"
android:layout_marginVertical="10dp" android:layout_marginVertical="10dp"
android:backgroundTint="@android:color/transparent" android:backgroundTint="@android:color/transparent"
android:background="?android:selectableItemBackgroundBorderless"
android:src="@drawable/ic_arrow_back" /> android:src="@drawable/ic_arrow_back" />
<TextView <TextView
@ -21,7 +22,7 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/settings" android:text="@string/settings"
android:layout_marginLeft="14dp" android:layout_marginLeft="20dp"
android:textSize="20sp" android:textSize="20sp"
android:layout_gravity="center" /> android:layout_gravity="center" />

View File

@ -0,0 +1,46 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:gravity="center"
android:layout_margin="10dp"
android:textSize="20sp" />
<com.google.android.material.textfield.TextInputLayout style="@style/CustomDialogTextInputLayout">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/delete_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/password"
android:inputType="text"
android:padding="12dp" />
</com.google.android.material.textfield.TextInputLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="right">
<Button
android:id="@+id/cancel_button"
android:text="@string/cancel"
style="@style/CustomDialogButton" />
<Button
android:id="@+id/delete_account_confirm"
android:text="@string/deleteAccount"
style="@style/CustomDialogButton" />
</LinearLayout>
</LinearLayout>

View File

@ -26,8 +26,6 @@
android:textSize="27sp" android:textSize="27sp"
android:textStyle="bold" /> android:textStyle="bold" />
<View style="@style/HorizontalLine" />
<LinearLayout <LinearLayout
android:id="@+id/website" android:id="@+id/website"
style="@style/AboutItem"> style="@style/AboutItem">
@ -45,8 +43,6 @@
</LinearLayout> </LinearLayout>
<View style="@style/HorizontalLine" />
<LinearLayout <LinearLayout
android:id="@+id/contributing" android:id="@+id/contributing"
style="@style/AboutItem"> style="@style/AboutItem">
@ -64,8 +60,6 @@
</LinearLayout> </LinearLayout>
<View style="@style/HorizontalLine" />
<LinearLayout <LinearLayout
android:id="@+id/authors" android:id="@+id/authors"
style="@style/AboutItem"> style="@style/AboutItem">
@ -83,8 +77,6 @@
</LinearLayout> </LinearLayout>
<View style="@style/HorizontalLine" />
<LinearLayout <LinearLayout
android:id="@+id/piped" android:id="@+id/piped"
style="@style/AboutItem"> style="@style/AboutItem">
@ -102,8 +94,6 @@
</LinearLayout> </LinearLayout>
<View style="@style/HorizontalLine" />
<LinearLayout <LinearLayout
android:id="@+id/donate" android:id="@+id/donate"
style="@style/AboutItem"> style="@style/AboutItem">
@ -121,8 +111,6 @@
</LinearLayout> </LinearLayout>
<View style="@style/HorizontalLine" />
<LinearLayout <LinearLayout
android:id="@+id/license" android:id="@+id/license"
style="@style/AboutItem"> style="@style/AboutItem">

View File

@ -43,7 +43,7 @@
<string name="unknown_error">Network error.</string> <string name="unknown_error">Network error.</string>
<string name="error">Something went wrong.</string> <string name="error">Something went wrong.</string>
<string name="empty">You have to enter a username and password.</string> <string name="empty">You have to enter a username and password.</string>
<string name="notgmail">This is for a LibreTube account.</string> <string name="notgmail">This is for a Piped account.</string>
<string name="defres">Default video resolution</string> <string name="defres">Default video resolution</string>
<string name="grid">Grid columns</string> <string name="grid">Grid columns</string>
<string name="emptyList">Nothing here.</string> <string name="emptyList">Nothing here.</string>
@ -203,4 +203,8 @@
<string name="clonePlaylist">Clone playlist</string> <string name="clonePlaylist">Clone playlist</string>
<string name="reset">Restore defaults</string> <string name="reset">Restore defaults</string>
<string name="reset_message">Are you sure? This will log you out and reset all your settings!</string> <string name="reset_message">Are you sure? This will log you out and reset all your settings!</string>
<string name="deleteAccount">Delete account</string>
<string name="deleteAccount_summary">Delete your Piped account</string>
<string name="account">Account</string>
<string name="restore">Restore</string>
</resources> </resources>

View File

@ -13,15 +13,19 @@
app:title="@string/instances" /> app:title="@string/instances" />
<Preference <Preference
android:icon="@drawable/ic_add_instance"
app:key="customInstance" app:key="customInstance"
app:title="@string/customInstance"
app:summary="@string/customInstance_summary" app:summary="@string/customInstance_summary"
android:icon="@drawable/ic_add_instance" /> app:title="@string/customInstance" />
<Preference <Preference
android:icon="@drawable/ic_trash"
app:key="clearCustomInstances" app:key="clearCustomInstances"
app:title="@string/clear_customInstances" app:title="@string/clear_customInstances" />
android:icon="@drawable/ic_trash" />
</PreferenceCategory>
<PreferenceCategory app:title="@string/account">
<Preference <Preference
android:icon="@drawable/ic_login_filled" android:icon="@drawable/ic_login_filled"
@ -29,6 +33,16 @@
app:key="login_register" app:key="login_register"
app:title="@string/login_register" /> app:title="@string/login_register" />
<Preference
android:icon="@drawable/ic_reset"
android:summary="@string/deleteAccount_summary"
app:key="delete_account"
app:title="@string/deleteAccount" />
</PreferenceCategory>
<PreferenceCategory app:title="@string/restore">
<Preference <Preference
android:icon="@drawable/ic_upload" android:icon="@drawable/ic_upload"
android:summary="@string/import_from_yt_summary" android:summary="@string/import_from_yt_summary"