Converted RetroUtil to Kotlin

This commit is contained in:
Prathamesh More 2022-04-21 22:35:54 +05:30
parent 6cc1ba746a
commit e605b8c695
23 changed files with 175 additions and 175 deletions

View file

@ -96,7 +96,6 @@ class MainActivity : AbsCastActivity(), OnSharedPreferenceChangeListener {
}
}
navController.addOnDestinationChangedListener { _, destination, _ ->
val start = System.currentTimeMillis()
if (destination.id == navGraph.startDestinationId) {
currentFragment(R.id.fragment_container)?.enterTransition = null
}
@ -117,7 +116,6 @@ class MainActivity : AbsCastActivity(), OnSharedPreferenceChangeListener {
animate = true
) // Hide Bottom Navigation Bar
}
showToast("Time : ${System.currentTimeMillis() - start}")
}
}

View file

@ -132,8 +132,8 @@ class SongFileAdapter(
return dataSet[position]
}
override fun getName(`object`: File): String {
return getFileTitle(`object`)
override fun getName(model: File): String {
return getFileTitle(model)
}
override fun onMultipleItemAction(menuItem: MenuItem, selection: List<File>) {

View file

@ -135,8 +135,8 @@ open class AlbumAdapter(
return dataSet[position]
}
override fun getName(album: Album): String {
return album.title
override fun getName(model: Album): String {
return model.title
}
override fun onMultipleItemAction(

View file

@ -132,8 +132,8 @@ class ArtistAdapter(
return dataSet[position]
}
override fun getName(artist: Artist): String {
return artist.name
override fun getName(model: Artist): String {
return model.name
}
override fun onMultipleItemAction(

View file

@ -64,9 +64,8 @@ abstract class AbsMultiSelectAdapter<V : RecyclerView.ViewHolder?, I>(
}
protected abstract fun getIdentifier(position: Int): I?
protected open fun getName(i: I): String? {
return i.toString()
}
protected abstract fun getName(model: I): String?
protected fun isChecked(identifier: I): Boolean {
return checked.contains(identifier)

View file

@ -120,8 +120,8 @@ class PlaylistAdapter(
return dataSet[position]
}
override fun getName(playlist: PlaylistWithSongs): String {
return playlist.playlistEntity.playlistName
override fun getName(model: PlaylistWithSongs): String {
return model.playlistEntity.playlistName
}
override fun onMultipleItemAction(menuItem: MenuItem, selection: List<PlaylistWithSongs>) {

View file

@ -60,7 +60,7 @@ class ShuffleButtonSongAdapter(
}
} else {
super.onBindViewHolder(holder, position - 1)
val landscape = RetroUtil.isLandscape()
val landscape = RetroUtil.isLandscape
if ((PreferenceUtil.songGridSize > 2 && !landscape) || (PreferenceUtil.songGridSizeLand > 5 && landscape)) {
holder.menu?.isVisible = false
}

View file

@ -100,7 +100,7 @@ open class SongAdapter(
holder.text?.text = getSongText(song)
holder.text2?.text = getSongText(song)
loadAlbumCover(song, holder)
val landscape = RetroUtil.isLandscape()
val landscape = RetroUtil.isLandscape
if ((PreferenceUtil.songGridSize > 2 && !landscape) || (PreferenceUtil.songGridSizeLand > 5 && landscape)) {
holder.menu?.isVisible = false
}
@ -149,8 +149,8 @@ open class SongAdapter(
return dataSet[position]
}
override fun getName(song: Song): String {
return song.title
override fun getName(model: Song): String {
return model.title
}
override fun onMultipleItemAction(menuItem: MenuItem, selection: List<Song>) {

View file

@ -1,6 +1,7 @@
package code.name.monkey.retromusic.extensions
import android.content.Context
import android.content.res.Configuration
import android.widget.Toast
import androidx.annotation.StringRes
@ -11,3 +12,7 @@ fun Context.showToast(@StringRes stringRes: Int, duration: Int = Toast.LENGTH_SH
fun Context.showToast(message: String, duration: Int = Toast.LENGTH_SHORT) {
Toast.makeText(this, message, duration).show()
}
val Context.isLandscape: Boolean get() = resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE
val Context.isTablet: Boolean get() = resources.configuration.smallestScreenWidthDp >= 600

View file

@ -8,6 +8,6 @@ fun WindowInsetsCompat?.safeGetBottomInsets(): Int {
return if (PreferenceUtil.isFullScreenMode) {
return 0
} else {
this?.getInsets(WindowInsetsCompat.Type.systemBars())?.bottom ?: RetroUtil.getNavigationBarHeight()
this?.getInsets(WindowInsetsCompat.Type.systemBars())?.bottom ?: RetroUtil.navigationBarHeight
}
}

View file

@ -161,7 +161,7 @@ class AlbumsFragment : AbsRecyclerViewCustomGridSizeFragment<AlbumAdapter, GridL
override fun onCreateMenu(menu: Menu, inflater: MenuInflater) {
super.onCreateMenu(menu, inflater)
val gridSizeItem: MenuItem = menu.findItem(R.id.action_grid_size)
if (RetroUtil.isLandscape()) {
if (RetroUtil.isLandscape) {
gridSizeItem.setTitle(R.string.action_grid_size_land)
}
setUpGridSizeMenu(gridSizeItem.subMenu)

View file

@ -171,7 +171,7 @@ class ArtistsFragment : AbsRecyclerViewCustomGridSizeFragment<ArtistAdapter, Gri
override fun onCreateMenu(menu: Menu, inflater: MenuInflater) {
super.onCreateMenu(menu, inflater)
val gridSizeItem: MenuItem = menu.findItem(R.id.action_grid_size)
if (RetroUtil.isLandscape()) {
if (RetroUtil.isLandscape) {
gridSizeItem.setTitle(R.string.action_grid_size_land)
}
setUpGridSizeMenu(gridSizeItem.subMenu)

View file

@ -116,8 +116,8 @@ class BackupFragment : Fragment(R.layout.fragment_backup), BackupAdapter.BackupC
}
@SuppressLint("CheckResult")
override fun onBackupMenuClicked(file: File, item: MenuItem): Boolean {
when (item.itemId) {
override fun onBackupMenuClicked(file: File, menuItem: MenuItem): Boolean {
when (menuItem.itemId) {
R.id.action_delete -> {
try {
file.delete()

View file

@ -28,7 +28,7 @@ abstract class AbsRecyclerViewCustomGridSizeFragment<A : RecyclerView.Adapter<*>
private var sortOrder: String? = null
private var currentLayoutRes: Int = 0
private val isLandscape: Boolean
get() = RetroUtil.isLandscape()
get() = RetroUtil.isLandscape
val maxGridSize: Int
get() = if (isLandscape) {

View file

@ -48,7 +48,7 @@ GenresFragment : AbsRecyclerViewFragment<GenreAdapter, LinearLayoutManager>(),
}
override fun createLayoutManager(): LinearLayoutManager {
return if (RetroUtil.isLandscape()) {
return if (RetroUtil.isLandscape) {
GridLayoutManager(activity, 4)
} else {
GridLayoutManager(activity, 2)

View file

@ -64,9 +64,8 @@ class DetailListFragment : AbsMainActivityFragment(R.layout.fragment_playlist_de
private val binding get() = _binding!!
private var showClearHistoryOption = false
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
_binding = FragmentPlaylistDetailBinding.bind(view)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
when (args.type) {
TOP_ARTISTS,
RECENT_ARTISTS,
@ -81,6 +80,13 @@ class DetailListFragment : AbsMainActivityFragment(R.layout.fragment_playlist_de
returnTransition = MaterialSharedAxis(MaterialSharedAxis.Y, false)
}
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
_binding = FragmentPlaylistDetailBinding.bind(view)
postponeEnterTransition()
view.doOnPreDraw { startPostponedEnterTransition() }
mainActivity.setSupportActionBar(binding.toolbar)
binding.progressIndicator.hide()
when (args.type) {
@ -106,8 +112,6 @@ class DetailListFragment : AbsMainActivityFragment(R.layout.fragment_playlist_de
})
binding.appBarLayout.statusBarForeground =
MaterialShapeDrawable.createWithElevationOverlay(requireContext())
postponeEnterTransition()
view.doOnPreDraw { startPostponedEnterTransition() }
requireActivity().onBackPressedDispatcher.addCallback(viewLifecycleOwner) {
if (!handleBackPress()) {
remove()
@ -232,10 +236,10 @@ class DetailListFragment : AbsMainActivityFragment(R.layout.fragment_playlist_de
GridLayoutManager(requireContext(), gridCount(), GridLayoutManager.VERTICAL, false)
private fun gridCount(): Int {
if (RetroUtil.isTablet()) {
return if (RetroUtil.isLandscape()) 6 else 4
if (RetroUtil.isTablet) {
return if (RetroUtil.isLandscape) 6 else 4
}
return if (RetroUtil.isLandscape()) 4 else 2
return if (RetroUtil.isLandscape) 4 else 2
}

View file

@ -67,7 +67,7 @@ open class MiniPlayerFragment : AbsMusicServiceFragment(R.layout.fragment_mini_p
view.setOnTouchListener(FlingPlayBackController(requireContext()))
setUpMiniPlayer()
if (RetroUtil.isTablet()) {
if (RetroUtil.isTablet) {
binding.actionNext.show()
binding.actionPrevious.show()
} else {

View file

@ -205,7 +205,6 @@ class GradientPlayerFragment : AbsPlayerFragment(R.layout.fragment_gradient_play
}
override fun onBackPressed(): Boolean {
println("OK")
var wasExpanded = false
if (getQueuePanel().state == STATE_EXPANDED) {
wasExpanded = getQueuePanel().state == STATE_EXPANDED

View file

@ -74,7 +74,7 @@ class PlaylistsFragment :
override fun onCreateMenu(menu: Menu, inflater: MenuInflater) {
super.onCreateMenu(menu, inflater)
val gridSizeItem: MenuItem = menu.findItem(R.id.action_grid_size)
if (RetroUtil.isLandscape()) {
if (RetroUtil.isLandscape) {
gridSizeItem.setTitle(R.string.action_grid_size_land)
}
setupGridSizeMenu(gridSizeItem.subMenu)
@ -109,7 +109,7 @@ class PlaylistsFragment :
7 -> gridSizeMenu.findItem(R.id.action_grid_size_7).isChecked = true
8 -> gridSizeMenu.findItem(R.id.action_grid_size_8).isChecked = true
}
val gridSize = if (RetroUtil.isLandscape()) 4 else 2
val gridSize = if (RetroUtil.isLandscape) 4 else 2
if (gridSize < 8) {
gridSizeMenu.findItem(R.id.action_grid_size_8).isVisible = false
}

View file

@ -128,7 +128,7 @@ class SongsFragment : AbsRecyclerViewCustomGridSizeFragment<SongAdapter, GridLay
override fun onCreateMenu(menu: Menu, inflater: MenuInflater) {
super.onCreateMenu(menu, inflater)
val gridSizeItem: MenuItem = menu.findItem(R.id.action_grid_size)
if (RetroUtil.isLandscape()) {
if (RetroUtil.isLandscape) {
gridSizeItem.setTitle(R.string.action_grid_size_land)
}
setUpGridSizeMenu(gridSizeItem.subMenu)

View file

@ -11,149 +11,144 @@
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*/
package code.name.monkey.retromusic.util
package code.name.monkey.retromusic.util;
import android.content.Context
import android.content.res.Configuration
import android.content.res.Resources
import code.name.monkey.retromusic.App.Companion.getContext
import android.view.WindowManager
import androidx.annotation.DrawableRes
import androidx.annotation.ColorInt
import android.graphics.drawable.Drawable
import code.name.monkey.appthemehelper.util.TintHelper
import android.content.res.Resources.Theme
import android.graphics.Point
import android.view.Display
import androidx.core.content.res.ResourcesCompat
import androidx.core.view.DisplayCompat
import java.lang.Exception
import java.net.InetAddress
import java.net.NetworkInterface
import java.text.DecimalFormat
import java.util.*
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.view.Display;
import android.view.WindowManager;
import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.res.ResourcesCompat;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.text.DecimalFormat;
import java.util.Collections;
import java.util.List;
import code.name.monkey.appthemehelper.util.TintHelper;
import code.name.monkey.retromusic.App;
public class RetroUtil {
public static String formatValue(float value) {
String[] arr = {"", "K", "M", "B", "T", "P", "E"};
int index = 0;
while ((value / 1000) >= 1) {
value = value / 1000;
index++;
object RetroUtil {
fun formatValue(value: Float): String {
var value = value
val arr = arrayOf("", "K", "M", "B", "T", "P", "E")
var index = 0
while (value / 1000 >= 1) {
value /= 1000
index++
}
DecimalFormat decimalFormat = new DecimalFormat("#.##");
return String.format("%s %s", decimalFormat.format(value), arr[index]);
val decimalFormat = DecimalFormat("#.##")
return String.format("%s %s", decimalFormat.format(value.toDouble()), arr[index])
}
public static float frequencyCount(int frequency) {
return (float) (frequency / 1000.0);
fun frequencyCount(frequency: Int): Float {
return (frequency / 1000.0).toFloat()
}
public static Point getScreenSize(@NonNull Context c) {
Display display = null;
fun getScreenSize(c: Context): Point {
var display: Display? = null
if (c.getSystemService(Context.WINDOW_SERVICE) != null) {
display = ((WindowManager) c.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
display = (c.getSystemService(Context.WINDOW_SERVICE) as WindowManager).defaultDisplay
}
Point size = new Point();
if (display != null) {
display.getSize(size);
}
return size;
val size = Point()
display?.getSize(size)
return size
}
public static int getStatusBarHeight() {
int result = 0;
int resourceId =
App.Companion.getContext()
.getResources()
.getIdentifier("status_bar_height", "dimen", "android");
val statusBarHeight: Int
get() {
var result = 0
val resourceId = getContext()
.resources
.getIdentifier("status_bar_height", "dimen", "android")
if (resourceId > 0) {
result = App.Companion.getContext().getResources().getDimensionPixelSize(resourceId);
result = getContext().resources.getDimensionPixelSize(resourceId)
}
return result;
return result
}
public static int getNavigationBarHeight() {
int result = 0;
int resourceId =
App.Companion.getContext()
.getResources()
.getIdentifier("navigation_bar_height", "dimen", "android");
val navigationBarHeight: Int
get() {
var result = 0
val resourceId = getContext()
.resources
.getIdentifier("navigation_bar_height", "dimen", "android")
if (resourceId > 0) {
result = App.Companion.getContext().getResources().getDimensionPixelSize(resourceId);
result = getContext().resources.getDimensionPixelSize(resourceId)
}
return result;
return result
}
@NonNull
public static Drawable getTintedVectorDrawable(
@NonNull Context context, @DrawableRes int id, @ColorInt int color) {
fun getTintedVectorDrawable(
context: Context, @DrawableRes id: Int, @ColorInt color: Int
): Drawable {
return TintHelper.createTintedDrawable(
getVectorDrawable(context.getResources(), id, context.getTheme()), color);
getVectorDrawable(context.resources, id, context.theme), color)
}
@NonNull
public static Drawable getTintedVectorDrawable(
@NonNull Resources res,
@DrawableRes int resId,
@Nullable Resources.Theme theme,
@ColorInt int color) {
return TintHelper.createTintedDrawable(getVectorDrawable(res, resId, theme), color);
fun getTintedVectorDrawable(
res: Resources,
@DrawableRes resId: Int,
theme: Theme?,
@ColorInt color: Int
): Drawable {
return TintHelper.createTintedDrawable(getVectorDrawable(res, resId, theme), color)
}
@Nullable
public static Drawable getVectorDrawable(
@NonNull Resources res, @DrawableRes int resId, @Nullable Resources.Theme theme) {
return ResourcesCompat.getDrawable(res, resId, theme);
private fun getVectorDrawable(
res: Resources, @DrawableRes resId: Int, theme: Theme?
): Drawable? {
return ResourcesCompat.getDrawable(res, resId, theme)
}
public static boolean isLandscape() {
return App.Companion.getContext().getResources().getConfiguration().orientation
== Configuration.ORIENTATION_LANDSCAPE;
}
val isLandscape: Boolean
get() = (getContext().resources.configuration.orientation
== Configuration.ORIENTATION_LANDSCAPE)
val isTablet: Boolean
get() = (getContext().resources.configuration.smallestScreenWidthDp
>= 600)
public static boolean isTablet() {
return App.Companion.getContext().getResources().getConfiguration().smallestScreenWidthDp
>= 600;
}
public static String getIpAddress(boolean useIPv4) {
fun getIpAddress(useIPv4: Boolean): String? {
try {
List<NetworkInterface> interfaces =
Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface intf : interfaces) {
List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
for (InetAddress addr : addrs) {
if (!addr.isLoopbackAddress()) {
String sAddr = addr.getHostAddress();
//boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
boolean isIPv4 = sAddr.indexOf(':') < 0;
val interfaces: List<NetworkInterface> =
Collections.list(NetworkInterface.getNetworkInterfaces())
for (intf in interfaces) {
val addrs: List<InetAddress> = Collections.list(intf.inetAddresses)
for (addr in addrs) {
if (!addr.isLoopbackAddress) {
val sAddr = addr.hostAddress
if (sAddr != null) {
val isIPv4 = sAddr.indexOf(':') < 0
if (useIPv4) {
if (isIPv4) return sAddr;
if (isIPv4) return sAddr
} else {
if (!isIPv4) {
int delim = sAddr.indexOf('%'); // drop ip6 zone suffix
if (delim < 0) {
return sAddr.toUpperCase();
val delim = sAddr.indexOf('%') // drop ip6 zone suffix
return if (delim < 0) {
sAddr.uppercase()
} else {
return sAddr.substring(
sAddr.substring(
0,
delim
).toUpperCase();
}
).uppercase()
}
}
}
} else {
return null
}
}
} catch (Exception ignored) {
}
return "";
}
} catch (ignored: Exception) {
}
return ""
}
}

View file

@ -12,7 +12,7 @@ class InsetsConstraintLayout @JvmOverloads constructor(
defStyleAttr: Int = 0
) : ConstraintLayout(context, attrs, defStyleAttr) {
init {
if (!RetroUtil.isLandscape())
if (!RetroUtil.isLandscape)
drawAboveSystemBarsWithPadding()
}
}

View file

@ -12,7 +12,7 @@ class InsetsLinearLayout @JvmOverloads constructor(
defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {
init {
if (!RetroUtil.isLandscape())
if (!RetroUtil.isLandscape)
drawAboveSystemBarsWithPadding()
}
}