Skip to content

Commit

Permalink
Update some deprecated functions & other warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Pepijn98 committed Jul 14, 2020
1 parent a61b7bd commit 1f0e334
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 132 deletions.
3 changes: 2 additions & 1 deletion app/src/main/java/xyz/kurozero/nekosmoe/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class MainActivity : AppCompatActivity(), ConnectivityReceiver.ConnectivityRecei
toggle.syncState()

tvAbout.text = Html.fromHtml("""
<p>© 2019 — <a href="https://kurozeropb.info">Kurozero</a> | v${Api.version} (${Api.versionCode})<br/>
<p>© 2020 — <a href="https://vdbroek.dev">Kurozero</a> | v${Api.version} (${Api.versionCode})<br/>
Made possible with <a href="https://nekos.moe">nekos.moe</a><br/></p>
""".trimIndent(), Html.FROM_HTML_MODE_LEGACY)
tvAbout.movementMethod = LinkMovementMethod.getInstance()
Expand Down Expand Up @@ -177,6 +177,7 @@ class MainActivity : AppCompatActivity(), ConnectivityReceiver.ConnectivityRecei
Fresco.initialize(this, config)

GlobalScope.launch {
@Suppress("BlockingMethodInNonBlockingContext")
Thread.sleep(5_000)
init = false
}
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/xyz/kurozero/nekosmoe/ProfileActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class ProfileActivity : AppCompatActivity() {
tvLikes.text = user!!.likesReceived.toString()
tvFavorites.text = user!!.favoritesReceived.toString()
tvJoined.text = timestamp(user!!.createdAt)
tvPosted.text = "${user!!.uploads} ${if (user!!.uploads == 1) "image" else "images"}"
tvGiven.text = "Has given ${user!!.likes.size} likes and ${user!!.favorites.size} favorites"
tvPosted.text = getString(R.string.posted2, user!!.uploads, if (user!!.uploads == 1) "image" else "images")
tvGiven.text = getString(R.string.given, user!!.likes.size, user!!.favorites.size)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import xyz.kurozero.nekosmoe.R
import xyz.kurozero.nekosmoe.model.Neko
import xyz.kurozero.nekosmoe.model.Nekos

class NekosGridRecyclerAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private var listOfNekos = listOf<Neko>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,12 @@ class NekosViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
user?.likes?.add(neko.id)
sharedPreferences.edit().putString("user", Api.gson.toJson(user!!)).apply()
Snackbar.make(view, "Liked", Snackbar.LENGTH_SHORT).show()
GlobalScope.launch(Dispatchers.Main) { view.btnLikeNeko.text = "Unlike" }
GlobalScope.launch(Dispatchers.Main) { view.btnLikeNeko.text = view.context.getString(R.string.unlike) }
} else {
user?.likes?.remove(neko.id)
sharedPreferences.edit().putString("user", Api.gson.toJson(user!!)).apply()
Snackbar.make(view, "Unliked", Snackbar.LENGTH_SHORT).show()
GlobalScope.launch(Dispatchers.Main) { view.btnLikeNeko.text = "Like" }
GlobalScope.launch(Dispatchers.Main) { view.btnLikeNeko.text = view.context.getString(R.string.like) }
}
}
response.close()
Expand Down Expand Up @@ -171,12 +171,12 @@ class NekosViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
user?.favorites?.add(neko.id)
sharedPreferences.edit().putString("user", Api.gson.toJson(user!!)).apply()
Snackbar.make(view, "Favorited", Snackbar.LENGTH_SHORT).show()
GlobalScope.launch(Dispatchers.Main) { view.btnFavNeko.text = "Unfavorite" }
GlobalScope.launch(Dispatchers.Main) { view.btnFavNeko.text = view.context.getString(R.string.unfav) }
} else {
user?.favorites?.remove(neko.id)
sharedPreferences.edit().putString("user", Api.gson.toJson(user!!)).apply()
Snackbar.make(view, "Unfavorited", Snackbar.LENGTH_SHORT).show()
GlobalScope.launch(Dispatchers.Main) { view.btnFavNeko.text = "Favorite" }
GlobalScope.launch(Dispatchers.Main) { view.btnFavNeko.text = view.context.getString(R.string.fav) }
}
}
response.close()
Expand All @@ -193,8 +193,8 @@ class NekosViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
nekoDialog.dismiss()
}

view.btnShareNeko.onClick {
if (!connected || !isConnected(itemView.context)) return@onClick
view.btnShareNeko.onClick shareNeko@ {
if (!connected || !isConnected(itemView.context)) return@shareNeko

picasso.load(neko.getImageUrl()).into {
onFailed { e, _ ->
Expand Down Expand Up @@ -236,8 +236,8 @@ class NekosViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
}
}

view.btnSaveNeko.onClick {
if (!connected || ! isConnected(view.context)) return@onClick
view.btnSaveNeko.onClick saveNeko@ {
if (!connected || ! isConnected(view.context)) return@saveNeko

if (!hasPermissions(view.context, permissions)) {
ActivityCompat.requestPermissions(view.context as MainActivity, permissions, 999)
Expand All @@ -260,7 +260,7 @@ private fun downloadAndSave(neko: Neko, view: View) {
val fos: OutputStream

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val resolver = view.context.contentResolver;
val resolver = view.context.contentResolver
val values = ContentValues()
values.put(MediaStore.MediaColumns.DISPLAY_NAME, neko.id)
values.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class ConnectivityReceiver : BroadcastReceiver() {
}
}

@Suppress("DEPRECATION")
private fun isConnectedOrConnecting(context: Context): Boolean {
val connMgr = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val networkInfo = connMgr.activeNetworkInfo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.StaggeredGridLayoutManager


abstract class EndlessRecyclerViewScrollListener : RecyclerView.OnScrollListener {
abstract class EndlessRecyclerViewScrollListener(layoutManager: GridLayoutManager) :
RecyclerView.OnScrollListener() {
// The minimum amount of items to have below your current scroll position
// before loading more.
private var visibleThreshold = 5
Expand All @@ -19,23 +19,14 @@ abstract class EndlessRecyclerViewScrollListener : RecyclerView.OnScrollListener
// Sets the starting page index
private val startingPageIndex = 0

private var mLayoutManager: RecyclerView.LayoutManager

constructor(layoutManager: LinearLayoutManager) {
this.mLayoutManager = layoutManager
}

constructor(layoutManager: GridLayoutManager) {
this.mLayoutManager = layoutManager
visibleThreshold *= layoutManager.spanCount
}
private var mLayoutManager: RecyclerView.LayoutManager = layoutManager

constructor(layoutManager: StaggeredGridLayoutManager) {
this.mLayoutManager = layoutManager
init {
visibleThreshold *= layoutManager.spanCount
}

fun getLastVisibleItem(lastVisibleItemPositions: IntArray): Int {
@Suppress("unused")
private fun getLastVisibleItem(lastVisibleItemPositions: IntArray): Int {
var maxSize = 0
for (i in lastVisibleItemPositions.indices) {
if (i == 0) {
Expand Down Expand Up @@ -113,6 +104,7 @@ abstract class EndlessRecyclerViewScrollListener : RecyclerView.OnScrollListener
this.loading = true
}

@Suppress("unused")
fun setLoading(state: Boolean) = run { loading = state }

// Defines the process for actually loading more data based on page
Expand Down
106 changes: 2 additions & 104 deletions app/src/main/java/xyz/kurozero/nekosmoe/helper/Utils.kt
Original file line number Diff line number Diff line change
@@ -1,117 +1,14 @@
package xyz.kurozero.nekosmoe.helper


import android.annotation.SuppressLint
import android.content.ContentUris
import android.content.Context
import android.content.pm.PackageManager
import android.database.Cursor
import android.net.Uri
import android.os.Build
import android.os.Environment
import android.provider.DocumentsContract
import android.provider.MediaStore
import android.graphics.Typeface
import android.net.ConnectivityManager
import android.text.format.DateUtils
import androidx.core.app.ActivityCompat
import java.text.SimpleDateFormat
import java.util.*

object FilePickUtils {
private fun getPathDeprecated(ctx: Context, uri: Uri?): String? {
if (uri == null) {
return null
}
val projection = arrayOf(MediaStore.Images.Media.DATA)
val cursor = ctx.contentResolver.query(uri, projection, null, null, null)
if (cursor != null) {
val columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)
cursor.moveToFirst()
val returnVal = cursor.getString(columnIndex)
cursor.close()
return returnVal
}
return uri.path
}

@SuppressLint("ObsoleteSdkInt")
fun getSmartFilePath(ctx: Context, uri: Uri): String? {
if (Build.VERSION.SDK_INT < 19) {
return getPathDeprecated(ctx, uri)
}
return getPath(ctx, uri)
}

private fun getPath(context: Context, uri: Uri): String? {
if (DocumentsContract.isDocumentUri(context, uri)) {
if (isExternalStorageDocument(uri)) {
val docId = DocumentsContract.getDocumentId(uri)
val split = docId.split(":".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
val type = split[0]

if ("primary".equals(type, ignoreCase = true)) {
return Environment.getExternalStorageDirectory().toString() + "/" + split[1]
}
} else if (isDownloadsDocument(uri)) {
val id = DocumentsContract.getDocumentId(uri)
val contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), java.lang.Long.valueOf(id)!!)
return getDataColumn(context, contentUri, null, null)
} else if (isMediaDocument(uri)) {
val docId = DocumentsContract.getDocumentId(uri)
val split = docId.split(":".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
val type = split[0]

var contentUri: Uri? = null
when (type) {
"image" -> contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
"video" -> contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI
"audio" -> contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
}

val selection = "_id=?"
val selectionArgs = arrayOf(split[1])

return contentUri?.let { getDataColumn(context, it, selection, selectionArgs) }
}
} else if ("content".equals(uri.scheme, ignoreCase = true)) {
return getDataColumn(context, uri, null, null)
} else if ("file".equals(uri.scheme, ignoreCase = true)) {
return uri.path
}
return null
}

private fun getDataColumn(context: Context, uri: Uri, selection: String?, selectionArgs: Array<String>?): String? {
var cursor: Cursor? = null
val column = "_data"
val projection = arrayOf(column)

try {
cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null)
if (cursor != null && cursor.moveToFirst()) {
val columnIndex = cursor.getColumnIndexOrThrow(column)
return cursor.getString(columnIndex)
}
} finally {
cursor?.close()
}
return null
}

private fun isExternalStorageDocument(uri: Uri): Boolean {
return "com.android.externalstorage.documents" == uri.authority
}

private fun isDownloadsDocument(uri: Uri): Boolean {
return "com.android.providers.downloads.documents" == uri.authority
}

private fun isMediaDocument(uri: Uri): Boolean {
return "com.android.providers.media.documents" == uri.authority
}
}

object FontsOverride {
fun setDefaultFont(context: Context, staticTypefaceFieldName: String, fontAssetName: String) {
val regular = Typeface.createFromAsset(context.assets, fontAssetName)
Expand All @@ -134,7 +31,7 @@ object FontsOverride {

fun timestamp(timeCreated: String): String {
val dateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ENGLISH)
val timeCreatedDate = dateFormat.parse(timeCreated)
val timeCreatedDate = dateFormat.parse(timeCreated)!!
return DateUtils.getRelativeTimeSpanString(timeCreatedDate.time, System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS) as String
}

Expand All @@ -149,6 +46,7 @@ fun hasPermissions(context: Context?, permissions: Array<String>): Boolean {
return true
}

@Suppress("DEPRECATION")
fun isConnected(context: Context): Boolean {
val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val activeNetwork = cm.activeNetworkInfo
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/xyz/kurozero/nekosmoe/model/NekoModel.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("unused")

package xyz.kurozero.nekosmoe.model

data class Nekos(
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,9 @@
<string name="post">/post/…</string>
<string name="toolbar_title">Nekos.moe</string>
<string name="toolbar_title_profile">Profile</string>
<string name="unlike">Unlike</string>
<string name="like">Like</string>
<string name="unfav">Unfavorite</string>
<string name="fav">Favorite</string>
<string name="posted2">%1$d %2$s</string>
</resources>

0 comments on commit 1f0e334

Please sign in to comment.