migrate more tokens to prefhelper

This commit is contained in:
Bnyro 2022-06-26 11:08:10 +02:00
parent 96ca250e48
commit d1418650d1
13 changed files with 32 additions and 67 deletions

View File

@ -1,7 +1,6 @@
package com.github.libretube.adapters
import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.text.format.DateUtils
import android.util.Log
@ -82,7 +81,7 @@ class PlaylistAdapter(
val delete = holder.v.findViewById<ImageView>(R.id.delete_playlist)
delete.visibility = View.VISIBLE
delete.setOnClickListener {
val token = PreferenceHelper.getString(holder.v.context, "token", "")!!
val token = PreferenceHelper.getToken(holder.v.context)
removeFromPlaylist(token, position)
}
}

View File

@ -13,6 +13,7 @@ import com.github.libretube.MainActivity
import com.github.libretube.R
import com.github.libretube.obj.Subscribe
import com.github.libretube.obj.Subscription
import com.github.libretube.util.PreferenceHelper
import com.github.libretube.util.RetrofitInstance
import com.squareup.picasso.Picasso
import kotlinx.coroutines.CoroutineScope
@ -70,10 +71,9 @@ class SubscriptionChannelAdapter(private val subscriptions: MutableList<Subscrip
fun run() {
CoroutineScope(Dispatchers.IO).launch {
val response = try {
val sharedPref = context
.getSharedPreferences("token", Context.MODE_PRIVATE)
val token = PreferenceHelper.getToken(context)
RetrofitInstance.api.subscribe(
sharedPref?.getString("token", "")!!,
token,
Subscribe(channelId)
)
} catch (e: IOException) {
@ -93,10 +93,9 @@ class SubscriptionChannelAdapter(private val subscriptions: MutableList<Subscrip
fun run() {
CoroutineScope(Dispatchers.IO).launch {
val response = try {
val sharedPref =
context.getSharedPreferences("token", Context.MODE_PRIVATE)
val token = PreferenceHelper.getToken(context)
RetrofitInstance.api.unsubscribe(
sharedPref?.getString("token", "")!!,
token,
Subscribe(channelId)
)
} catch (e: IOException) {

View File

@ -1,7 +1,6 @@
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

View File

@ -1,7 +1,6 @@
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

View File

@ -1,7 +1,6 @@
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

View File

@ -40,11 +40,7 @@ class LoginDialog : DialogFragment() {
view.findViewById<TextView>(R.id.user).text.toString() + " (" + user + ")"
view.findViewById<Button>(R.id.logout).setOnClickListener {
Toast.makeText(context, R.string.loggedout, Toast.LENGTH_SHORT).show()
val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE)
with(sharedPref!!.edit()) {
putString("token", "")
apply()
}
PreferenceHelper.setToken(requireContext(), "")
dialog?.dismiss()
}
} else {
@ -105,17 +101,8 @@ class LoginDialog : DialogFragment() {
Toast.makeText(context, response.error, Toast.LENGTH_SHORT).show()
} else if (response.token != null) {
Toast.makeText(context, R.string.loggedIn, Toast.LENGTH_SHORT).show()
val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE)
with(sharedPref!!.edit()) {
putString("token", response.token)
apply()
}
val sharedPref2 =
context?.getSharedPreferences("username", Context.MODE_PRIVATE)
with(sharedPref2!!.edit()) {
putString("username", login.username)
apply()
}
PreferenceHelper.setToken(requireContext(), response.token!!)
PreferenceHelper.setUsername(requireContext(), login.username!!)
dialog?.dismiss()
}
}
@ -138,24 +125,15 @@ class LoginDialog : DialogFragment() {
Toast.makeText(context, R.string.server_error, Toast.LENGTH_SHORT).show()
return@launchWhenCreated
} catch (e: Exception) {
Log.e(TAG, "dafaq?" + e.toString())
Log.e(TAG, "dafaq?$e")
return@launchWhenCreated
}
if (response.error != null) {
Toast.makeText(context, response.error, Toast.LENGTH_SHORT).show()
} else if (response.token != null) {
Toast.makeText(context, R.string.registered, Toast.LENGTH_SHORT).show()
val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE)
with(sharedPref!!.edit()) {
putString("token", response.token)
apply()
}
val sharedPref2 =
context?.getSharedPreferences("username", Context.MODE_PRIVATE)
with(sharedPref2!!.edit()) {
putString("username", login.username)
apply()
}
PreferenceHelper.setToken(requireContext(), response.token!!)
PreferenceHelper.setUsername(requireContext(), login.username!!)
dialog?.dismiss()
}
}

View File

@ -18,6 +18,7 @@ import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.github.libretube.R
import com.github.libretube.adapters.ChannelAdapter
import com.github.libretube.obj.Subscribe
import com.github.libretube.util.PreferenceHelper
import com.github.libretube.util.RetrofitInstance
import com.github.libretube.util.formatShort
import com.google.android.material.button.MaterialButton
@ -63,9 +64,8 @@ class ChannelFragment : Fragment() {
val refreshChannel = {
refreshLayout?.isRefreshing = true
fetchChannel(view)
val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE)
val subButton = view.findViewById<MaterialButton>(R.id.channel_subscribe)
if (sharedPref?.getString("token", "") != "") {
if (PreferenceHelper.getToken(requireContext()) != "") {
isSubscribed(subButton)
}
}
@ -95,10 +95,10 @@ class ChannelFragment : Fragment() {
fun run() {
lifecycleScope.launchWhenCreated {
val response = try {
val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE)
val token = PreferenceHelper.getToken(requireContext())
RetrofitInstance.api.isSubscribed(
channel_id!!,
sharedPref?.getString("token", "")!!
token
)
} catch (e: IOException) {
println(e)
@ -135,9 +135,9 @@ class ChannelFragment : Fragment() {
fun run() {
lifecycleScope.launchWhenCreated {
val response = try {
val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE)
val token = PreferenceHelper.getToken(requireContext())
RetrofitInstance.api.subscribe(
sharedPref?.getString("token", "")!!,
token,
Subscribe(channel_id)
)
} catch (e: IOException) {
@ -158,9 +158,9 @@ class ChannelFragment : Fragment() {
fun run() {
lifecycleScope.launchWhenCreated {
val response = try {
val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE)
val token = PreferenceHelper.getToken(requireContext())
RetrofitInstance.api.unsubscribe(
sharedPref?.getString("token", "")!!,
token,
Subscribe(channel_id)
)
} catch (e: IOException) {

View File

@ -1,6 +1,5 @@
package com.github.libretube.fragments
import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater

View File

@ -34,7 +34,6 @@ import androidx.core.os.bundleOf
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.preference.PreferenceManager
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
@ -426,7 +425,7 @@ class PlayerFragment : Fragment() {
thumbnailUrl = response.thumbnailUrl!!
// check whether related streams and autoplay are enabled
autoplay = PreferenceHelper.getBoolean(requireContext(),"autoplay", false)
autoplay = PreferenceHelper.getBoolean(requireContext(), "autoplay", false)
relatedStreamsEnabled = PreferenceHelper.getBoolean(requireContext(), "related_streams_toggle", true)
// save related streams for autoplay
relatedStreams = response.relatedStreams
@ -679,8 +678,8 @@ class PlayerFragment : Fragment() {
activity.findViewById<MotionLayout>(R.id.mainMotionLayout).transitionToEnd()
view.findViewById<MotionLayout>(R.id.playerMotionLayout).transitionToEnd()
}
val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE)
if (sharedPref?.getString("token", "") != "") {
val token = PreferenceHelper.getToken(requireContext())
if (token != "") {
val channelId = response.uploaderUrl?.replace("/channel/", "")
val subButton = view.findViewById<MaterialButton>(R.id.player_subscribe)
isSubscribed(subButton, channelId!!)
@ -940,10 +939,10 @@ class PlayerFragment : Fragment() {
fun run() {
lifecycleScope.launchWhenCreated {
val response = try {
val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE)
val token = PreferenceHelper.getToken(requireContext())
RetrofitInstance.api.isSubscribed(
channel_id,
sharedPref?.getString("token", "")!!
token
)
} catch (e: IOException) {
println(e)
@ -980,9 +979,9 @@ class PlayerFragment : Fragment() {
fun run() {
lifecycleScope.launchWhenCreated {
val response = try {
val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE)
val token = PreferenceHelper.getToken(requireContext())
RetrofitInstance.api.subscribe(
sharedPref?.getString("token", "")!!,
token,
Subscribe(channel_id)
)
} catch (e: IOException) {
@ -1003,9 +1002,9 @@ class PlayerFragment : Fragment() {
fun run() {
lifecycleScope.launchWhenCreated {
val response = try {
val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE)
val token = PreferenceHelper.getToken(requireContext())
RetrofitInstance.api.unsubscribe(
sharedPref?.getString("token", "")!!,
token,
Subscribe(channel_id)
)
} catch (e: IOException) {

View File

@ -1,6 +1,5 @@
package com.github.libretube.fragments
import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater

View File

@ -1,6 +1,5 @@
package com.github.libretube.fragments
import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
@ -15,7 +14,6 @@ import android.widget.Toast
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.preference.PreferenceManager
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

View File

@ -39,9 +39,7 @@ class AdvancedSettings : PreferenceFragmentCompat() {
PreferenceHelper.clearPreferences(requireContext())
// clear login token
val sharedPrefToken =
context?.getSharedPreferences("token", Context.MODE_PRIVATE)
sharedPrefToken?.edit()?.clear()?.commit()
PreferenceHelper.setToken(requireContext(), "")
requireMainActivityRestart = true
activity?.recreate()

View File

@ -309,11 +309,10 @@ class InstanceSettings : PreferenceFragmentCompat() {
fun run() {
lifecycleScope.launchWhenCreated {
val response = try {
val sharedPref =
context?.getSharedPreferences("token", Context.MODE_PRIVATE)
val token = PreferenceHelper.getToken(requireContext())
RetrofitInstance.api.importSubscriptions(
false,
sharedPref?.getString("token", "")!!,
token,
channels
)
} catch (e: IOException) {