Fixed bad practices of singleton

This commit is contained in:
ivan agosto 2019-02-10 11:41:56 -06:00
parent 329813d1b3
commit d783b4887d
10 changed files with 46 additions and 52 deletions

View File

@ -24,7 +24,7 @@ class HostActivity : AppCompatActivity() {
settings = PreferenceManager.getDefaultSharedPreferences(this)
editor = settings.edit()
ManagerSingleton.context = this
button.setOnClickListener {
getKeys(hostText.text.toString())
}
@ -49,7 +49,7 @@ class HostActivity : AppCompatActivity() {
editor.putString("last_host",host)
editor.putString("hostP2play",host)
editor.apply()
ManagerSingleton.Toast(getString(R.string.finallyMsg))
ManagerSingleton.Toast(getString(R.string.finallyMsg), this)
ManagerSingleton.url=host
startApp()
}
@ -74,7 +74,7 @@ class HostActivity : AppCompatActivity() {
}
else{
runOnUiThread {
ManagerSingleton.Toast(getString(R.string.errorMsg))
ManagerSingleton.Toast(getString(R.string.errorMsg), this)
button.isEnabled = true
}
}
@ -83,7 +83,7 @@ class HostActivity : AppCompatActivity() {
private fun startApp(){
runOnUiThread {
val intent = Intent(ManagerSingleton.context,MainActivity::class.java)
val intent = Intent(this,MainActivity::class.java)
startActivity(intent)
this.finish()
}

View File

@ -22,7 +22,6 @@ class LoginActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
setTitle(R.string.action_login)
ManagerSingleton.context = this
_db = Database(this)
settings = PreferenceManager.getDefaultSharedPreferences(this)
@ -56,13 +55,13 @@ class LoginActivity : AppCompatActivity() {
}
"0" -> {
runOnUiThread {
ManagerSingleton.Toast(getString(R.string.loginError_msg))
ManagerSingleton.Toast(getString(R.string.loginError_msg), this)
}
}
"-1" -> {
runOnUiThread {
loginBtn.isEnabled = true
ManagerSingleton.Toast(getString(R.string.loginFailed_msg))
ManagerSingleton.Toast(getString(R.string.loginFailed_msg), this)
}
}
}
@ -76,13 +75,13 @@ class LoginActivity : AppCompatActivity() {
_db.newUser(user)
ManagerSingleton.user = user
runOnUiThread {
ManagerSingleton.Toast(getString(R.string.loginSuccess_msg))
ManagerSingleton.Toast(getString(R.string.loginSuccess_msg), this)
finish()
}
}
else{
runOnUiThread {
ManagerSingleton.Toast(getString(R.string.loginError_msg))
ManagerSingleton.Toast(getString(R.string.loginError_msg), this)
}
}
}

View File

@ -53,12 +53,6 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
drawer_layout.addDrawerListener(toggle)
toggle.syncState()
// Search bar
// Context for ManagerSingleton
ManagerSingleton.context = this
nav_view.setNavigationItemSelectedListener(this)
viewManager = LinearLayoutManager(this)
@ -114,13 +108,13 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
try {
if(this.pagination == 0){
(viewAdapter as VideosAdapter)?.clearData()
(viewAdapter as VideosAdapter).clearData()
recyclerView.scrollToPosition(0)
}
(viewAdapter as VideosAdapter)?.addData(videos)
(viewAdapter as VideosAdapter).addData(videos)
}catch (err: Exception){
err.printStackTrace()
ManagerSingleton.Toast(getString(R.string.errorMsg))
ManagerSingleton.Toast(getString(R.string.errorMsg), this)
}
this.swipeContainer.isRefreshing = false
@ -146,7 +140,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
private fun getSubscriptionVideos(){
if(ManagerSingleton.user.status != 1){
ManagerSingleton.Toast("Inicia session primero")
ManagerSingleton.Toast("Inicia session primero", this)
startActivity(Intent(this, LoginActivity::class.java))
return
}
@ -316,7 +310,6 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
override fun onResume() {
super.onResume()
ManagerSingleton.context = this
setSideData()
}
@ -351,7 +344,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
ManagerSingleton.logout()
this.refresh()
ManagerSingleton.Toast(getString(R.string.logout_msg))
ManagerSingleton.Toast(getString(R.string.logout_msg), this)
}
@ -377,7 +370,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
private fun searchVideos(){
swipeContainer.isRefreshing = true
section = "search"
setTitle(this.searchVal)
this.title = this.searchVal
AsyncTask.execute {
val videos = client.search(this.searchVal, this.pagination)
runOnUiThread {

View File

@ -5,16 +5,15 @@ import org.libre.agosto.p2play.models.TokenModel
import org.libre.agosto.p2play.models.UserModel
object ManagerSingleton {
var context: Context?= null
var url: String?= null
var user: UserModel = UserModel()
var token: TokenModel = TokenModel()
var nfsw: Boolean = false
// var keys:
var videos_count: Int = 0
fun Toast(text: String?) {
if(this.context == null) { return }
android.widget.Toast.makeText(this.context, text, android.widget.Toast.LENGTH_SHORT).show()
fun Toast(text: String?, context: Context) {
if(context == null) { return }
android.widget.Toast.makeText(context, text, android.widget.Toast.LENGTH_SHORT).show()
}
fun logout(){

View File

@ -20,7 +20,6 @@ class RegisterActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_register)
setTitle(R.string.registerActionBtn)
ManagerSingleton.context = this
settings = PreferenceManager.getDefaultSharedPreferences(this)
client_id = settings.getString("client_id", "")
@ -43,11 +42,11 @@ class RegisterActivity : AppCompatActivity() {
runOnUiThread {
when (res) {
1 -> {
ManagerSingleton.Toast(getString(R.string.registerSuccess_msg))
ManagerSingleton.Toast(getString(R.string.registerSuccess_msg), this)
finish()
}
0 -> ManagerSingleton.Toast(getString(R.string.registerFailed_msg))
-1 -> ManagerSingleton.Toast(getString(R.string.registerError_msg))
0 -> ManagerSingleton.Toast(getString(R.string.registerFailed_msg), this)
-1 -> ManagerSingleton.Toast(getString(R.string.registerError_msg), this)
}
registerBtn.isEnabled = true
}

View File

@ -35,7 +35,6 @@ class ReproductorActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_reproductor)
ManagerSingleton.context = this
videoView.webChromeClient = WebClient()
videoView.settings.javaScriptEnabled = true
@ -86,7 +85,7 @@ class ReproductorActivity : AppCompatActivity() {
val res = this._actions.subscribe(ManagerSingleton.token.token, account)
if (res == 1) {
runOnUiThread {
ManagerSingleton.Toast(getString(R.string.subscribeMsg))
ManagerSingleton.Toast(getString(R.string.subscribeMsg), this)
this.changeSubscribeBtn(true)
}
}
@ -101,7 +100,7 @@ class ReproductorActivity : AppCompatActivity() {
val res = this._actions.unSubscribe(ManagerSingleton.token.token, account)
if (res == 1) {
runOnUiThread {
ManagerSingleton.Toast(getString(R.string.unSubscribeMsg))
ManagerSingleton.Toast(getString(R.string.unSubscribeMsg), this)
this.changeSubscribeBtn(false)
}
}
@ -115,7 +114,7 @@ class ReproductorActivity : AppCompatActivity() {
val res = this._actions.rate(ManagerSingleton.token.token, this.video.id, rate)
if (res == 1) {
runOnUiThread {
ManagerSingleton.Toast(getString(R.string.rateMsg))
ManagerSingleton.Toast(getString(R.string.rateMsg), this)
if(rate=="like"){
likeLayout.setBackgroundColor(ContextCompat.getColor(this, R.color.colorLike))
dislikeLayout.background = null
@ -204,7 +203,7 @@ class ReproductorActivity : AppCompatActivity() {
private fun makeComment() {
if(commentaryText.text.toString() == ""){
ManagerSingleton.Toast(getString(R.string.emptyCommentaryMsg))
ManagerSingleton.Toast(getString(R.string.emptyCommentaryMsg), this)
return
}
val text = commentaryText.text.toString()
@ -212,12 +211,12 @@ class ReproductorActivity : AppCompatActivity() {
val res = this.client.makeCommentary(ManagerSingleton.token.token, this.video.id, text)
runOnUiThread {
if (res) {
ManagerSingleton.Toast(getString(R.string.makedCommentaryMsg))
ManagerSingleton.Toast(getString(R.string.makedCommentaryMsg), this)
commentaryText.text.clear()
this.getComments()
}
else {
ManagerSingleton.Toast(getString(R.string.errorCommentaryMsg))
ManagerSingleton.Toast(getString(R.string.errorCommentaryMsg), this)
}
}
}
@ -276,7 +275,7 @@ class ReproductorActivity : AppCompatActivity() {
this.mOriginalSystemUiVisibility = this@ReproductorActivity.window.decorView.systemUiVisibility
this.mOriginalOrientation = this@ReproductorActivity.requestedOrientation
this.mCustomViewCallback = paramCustomViewCallback
(this@ReproductorActivity.window.decorView as FrameLayout).addView(this.mCustomView, FrameLayout.LayoutParams(0, 0))
(this@ReproductorActivity.window.decorView as FrameLayout).addView(this.mCustomView, FrameLayout.LayoutParams(-1, -1))
this@ReproductorActivity.window.decorView.systemUiVisibility = 3846
}
catch (err: Exception){

View File

@ -32,13 +32,12 @@ class SettingsActivity : AppCompatPreferenceActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
ManagerSingleton.context = this
setupActionBar()
}
override fun onBackPressed() {
super.onBackPressed()
ManagerSingleton.Toast(getString(R.string.pref_message_exit))
ManagerSingleton.Toast(getString(R.string.pref_message_exit), this)
}
/**

View File

@ -24,9 +24,9 @@ class SplashActivity : AppCompatActivity() {
settings = PreferenceManager.getDefaultSharedPreferences(this)
ManagerSingleton.context = this
ManagerSingleton.nfsw = settings.getBoolean("show_nfsw", false)
ManagerSingleton.videos_count = settings.getInt("videos_count", 15)
val host = settings.getString("hostP2play","")
val lastHost = settings.getString("last_host","")
@ -93,7 +93,7 @@ class SplashActivity : AppCompatActivity() {
private fun startApp() {
runOnUiThread {
val intent = Intent(ManagerSingleton.context, MainActivity::class.java)
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
this.finish()
}
@ -101,7 +101,7 @@ class SplashActivity : AppCompatActivity() {
private fun startHostActivity() {
runOnUiThread {
val intent = Intent(ManagerSingleton.context, HostActivity::class.java)
val intent = Intent(this, HostActivity::class.java)
startActivity(intent)
this.finish()
}

View File

@ -1,6 +1,7 @@
package org.libre.agosto.p2play.adapters
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.drawable.Drawable
import android.os.AsyncTask
@ -33,12 +34,14 @@ class CommentariesAdapter(private val myDataset: ArrayList<CommentaryModel>) :
val userImg: ImageView
val username: TextView
val commentary: TextView
val context: Context
init {
// Define click listener for the ViewHolder's View
username = view.findViewById(R.id.userTxt)
commentary = view.findViewById(R.id.userCommentary)
userImg = view.findViewById(R.id.userCommentImg)
context = view.context
}
}

View File

@ -1,6 +1,7 @@
package org.libre.agosto.p2play.adapters
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.drawable.Drawable
import android.os.AsyncTask
@ -25,6 +26,7 @@ import java.util.concurrent.TimeUnit
class VideosAdapter(private val myDataset: ArrayList<VideoModel>) :
RecyclerView.Adapter<VideosAdapter.ViewHolder>() {
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder.
@ -34,6 +36,7 @@ class VideosAdapter(private val myDataset: ArrayList<VideoModel>) :
val userImg: ImageView
val tittle: TextView
val description: TextView
val context: Context
init {
// Define click listener for the ViewHolder's View
@ -41,10 +44,10 @@ class VideosAdapter(private val myDataset: ArrayList<VideoModel>) :
description = view.findViewById(R.id.descriptionTxt)
thumb = view.findViewById(R.id.thumb)
userImg = view.findViewById(R.id.userImg)
context = view.context
}
}
// Create new views (invoked by the layout manager)
override fun onCreateViewHolder(parent: ViewGroup,
viewType: Int): VideosAdapter.ViewHolder {
@ -62,25 +65,25 @@ class VideosAdapter(private val myDataset: ArrayList<VideoModel>) :
holder.tittle.text = myDataset[position].name
Picasso.get().load("https://"+ManagerSingleton.url+myDataset[position].thumbUrl).into(holder.thumb)
holder.thumb.setOnClickListener {
val intent = Intent(ManagerSingleton.context, ReproductorActivity::class.java)
val intent = Intent(holder.context, ReproductorActivity::class.java)
intent.putExtra("video", myDataset[position] as Serializable)
ManagerSingleton.context!!.startActivity(intent)
holder.context.startActivity(intent)
}
if(myDataset[position].userImageUrl!="")
Picasso.get().load("https://"+ManagerSingleton.url+myDataset[position].userImageUrl).into(holder.userImg)
else
Picasso.get().load(R.drawable.default_avatar).into(holder.userImg)
val viewsText = ManagerSingleton.context!!.getString(R.string.view_text)
var timeText = ManagerSingleton.context!!.getString(R.string.timeSec_text)
val viewsText = holder.context.getString(R.string.view_text)
var timeText = holder.context.getString(R.string.timeSec_text)
var timeString = myDataset[position].duration.toString()
val seconds = myDataset[position].duration.toInt();
if(seconds > 60 && seconds < (60 * 60)){
timeText = ManagerSingleton.context!!.getString(R.string.timeMin_text)
timeText = holder.context.getString(R.string.timeMin_text)
timeString = (seconds / 60).toString() + ":" + (seconds % 60).toString()
}
else if(seconds > (60 * 60)){
timeText = ManagerSingleton.context!!.getString(R.string.timeHrs_text)
timeText = holder.context.getString(R.string.timeHrs_text)
timeString = (seconds / 60 / 60).toString() + ":" + (seconds / 60 % 60).toString()
}