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

View File

@@ -22,7 +22,6 @@ class LoginActivity : AppCompatActivity() {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login) setContentView(R.layout.activity_login)
setTitle(R.string.action_login) setTitle(R.string.action_login)
ManagerSingleton.context = this
_db = Database(this) _db = Database(this)
settings = PreferenceManager.getDefaultSharedPreferences(this) settings = PreferenceManager.getDefaultSharedPreferences(this)
@@ -56,13 +55,13 @@ class LoginActivity : AppCompatActivity() {
} }
"0" -> { "0" -> {
runOnUiThread { runOnUiThread {
ManagerSingleton.Toast(getString(R.string.loginError_msg)) ManagerSingleton.Toast(getString(R.string.loginError_msg), this)
} }
} }
"-1" -> { "-1" -> {
runOnUiThread { runOnUiThread {
loginBtn.isEnabled = true 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) _db.newUser(user)
ManagerSingleton.user = user ManagerSingleton.user = user
runOnUiThread { runOnUiThread {
ManagerSingleton.Toast(getString(R.string.loginSuccess_msg)) ManagerSingleton.Toast(getString(R.string.loginSuccess_msg), this)
finish() finish()
} }
} }
else{ else{
runOnUiThread { 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) drawer_layout.addDrawerListener(toggle)
toggle.syncState() toggle.syncState()
// Search bar
// Context for ManagerSingleton
ManagerSingleton.context = this
nav_view.setNavigationItemSelectedListener(this) nav_view.setNavigationItemSelectedListener(this)
viewManager = LinearLayoutManager(this) viewManager = LinearLayoutManager(this)
@@ -114,13 +108,13 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
try { try {
if(this.pagination == 0){ if(this.pagination == 0){
(viewAdapter as VideosAdapter)?.clearData() (viewAdapter as VideosAdapter).clearData()
recyclerView.scrollToPosition(0) recyclerView.scrollToPosition(0)
} }
(viewAdapter as VideosAdapter)?.addData(videos) (viewAdapter as VideosAdapter).addData(videos)
}catch (err: Exception){ }catch (err: Exception){
err.printStackTrace() err.printStackTrace()
ManagerSingleton.Toast(getString(R.string.errorMsg)) ManagerSingleton.Toast(getString(R.string.errorMsg), this)
} }
this.swipeContainer.isRefreshing = false this.swipeContainer.isRefreshing = false
@@ -146,7 +140,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
private fun getSubscriptionVideos(){ private fun getSubscriptionVideos(){
if(ManagerSingleton.user.status != 1){ if(ManagerSingleton.user.status != 1){
ManagerSingleton.Toast("Inicia session primero") ManagerSingleton.Toast("Inicia session primero", this)
startActivity(Intent(this, LoginActivity::class.java)) startActivity(Intent(this, LoginActivity::class.java))
return return
} }
@@ -316,7 +310,6 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
ManagerSingleton.context = this
setSideData() setSideData()
} }
@@ -351,7 +344,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
ManagerSingleton.logout() ManagerSingleton.logout()
this.refresh() 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(){ private fun searchVideos(){
swipeContainer.isRefreshing = true swipeContainer.isRefreshing = true
section = "search" section = "search"
setTitle(this.searchVal) this.title = this.searchVal
AsyncTask.execute { AsyncTask.execute {
val videos = client.search(this.searchVal, this.pagination) val videos = client.search(this.searchVal, this.pagination)
runOnUiThread { runOnUiThread {

View File

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

View File

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

View File

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

View File

@@ -32,13 +32,12 @@ class SettingsActivity : AppCompatPreferenceActivity() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
ManagerSingleton.context = this
setupActionBar() setupActionBar()
} }
override fun onBackPressed() { override fun onBackPressed() {
super.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) settings = PreferenceManager.getDefaultSharedPreferences(this)
ManagerSingleton.context = this
ManagerSingleton.nfsw = settings.getBoolean("show_nfsw", false) ManagerSingleton.nfsw = settings.getBoolean("show_nfsw", false)
ManagerSingleton.videos_count = settings.getInt("videos_count", 15)
val host = settings.getString("hostP2play","") val host = settings.getString("hostP2play","")
val lastHost = settings.getString("last_host","") val lastHost = settings.getString("last_host","")
@@ -93,7 +93,7 @@ class SplashActivity : AppCompatActivity() {
private fun startApp() { private fun startApp() {
runOnUiThread { runOnUiThread {
val intent = Intent(ManagerSingleton.context, MainActivity::class.java) val intent = Intent(this, MainActivity::class.java)
startActivity(intent) startActivity(intent)
this.finish() this.finish()
} }
@@ -101,7 +101,7 @@ class SplashActivity : AppCompatActivity() {
private fun startHostActivity() { private fun startHostActivity() {
runOnUiThread { runOnUiThread {
val intent = Intent(ManagerSingleton.context, HostActivity::class.java) val intent = Intent(this, HostActivity::class.java)
startActivity(intent) startActivity(intent)
this.finish() this.finish()
} }

View File

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

View File

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