From 46dc5eb58e1d473279ccb3037a8286b7244e23f2 Mon Sep 17 00:00:00 2001 From: Hemanth S Date: Thu, 24 Sep 2020 16:23:23 +0530 Subject: [PATCH] Fix crashing on About on release build --- app/build.gradle | 2 +- app/src/main/assets/contributors.json | 8 ++--- .../code/name/monkey/retromusic/MainModule.kt | 4 +++ .../retromusic/adapter/ContributorAdapter.kt | 7 +++- .../retromusic/fragments/LibraryViewModel.kt | 5 ++- .../fragments/about/AboutFragment.kt | 36 +++++-------------- .../monkey/retromusic/model/Contributor.kt | 16 +++++---- .../repository/LocalDataRepository.kt | 25 +++++++++++++ .../retromusic/repository/Repository.kt | 6 +++- 9 files changed, 66 insertions(+), 43 deletions(-) create mode 100644 app/src/main/java/code/name/monkey/retromusic/repository/LocalDataRepository.kt diff --git a/app/build.gradle b/app/build.gradle index dd90ecf6..ef46eb09 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -25,7 +25,7 @@ android { } signingConfigs { release { - Properties properties = getProperties('/Users/apple/Documents/Github/retro.properties ') + Properties properties = getProperties('C:/Users/h4h13/Documents/GitHub/retro.properties ') storeFile file(getProperty(properties, 'storeFile')) keyAlias getProperty(properties, 'keyAlias') storePassword getProperty(properties, 'storePassword') diff --git a/app/src/main/assets/contributors.json b/app/src/main/assets/contributors.json index b7620161..217d718a 100644 --- a/app/src/main/assets/contributors.json +++ b/app/src/main/assets/contributors.json @@ -3,24 +3,24 @@ "name": "Hemanth Savarala", "summary": "Lead Developer & Designer", "link": "https://github.com/h4h13", - "profile_image": "https://i.imgur.com/AoVs9oj.jpg" + "image": "https://i.imgur.com/AoVs9oj.jpg" }, { "name": "Lennart Glamann", "summary": "Play Store banner and Images", "link": "https://t.me/FlixbusLennart", - "profile_image": "https://i.imgur.com/Q5Nsx1R.jpg" + "image": "https://i.imgur.com/Q5Nsx1R.jpg" }, { "name": "Daksh P. Jain", "summary": "Support Representative & Moderator", "link": "https://daksh.eu.org", - "profile_image": "https://i.imgur.com/fnYpg65.jpg" + "image": "https://i.imgur.com/fnYpg65.jpg" }, { "name": "Milind Goel", "summary": "Support Representative & Moderator", "link": "https://t.me/MilindGoel15", - "profile_image": "https://i.imgur.com/Bz4De21_d.jpg" + "image": "https://i.imgur.com/Bz4De21_d.jpg" } ] diff --git a/app/src/main/java/code/name/monkey/retromusic/MainModule.kt b/app/src/main/java/code/name/monkey/retromusic/MainModule.kt index 4552702a..cfe9c54e 100644 --- a/app/src/main/java/code/name/monkey/retromusic/MainModule.kt +++ b/app/src/main/java/code/name/monkey/retromusic/MainModule.kt @@ -108,6 +108,7 @@ private val dataModule = module { get(), get(), get(), + get(), get() ) } bind Repository::class @@ -153,6 +154,9 @@ private val dataModule = module { get() ) } + single { + RealLocalDataRepository(get()) + } bind LocalDataRepository::class } private val viewModules = module { diff --git a/app/src/main/java/code/name/monkey/retromusic/adapter/ContributorAdapter.kt b/app/src/main/java/code/name/monkey/retromusic/adapter/ContributorAdapter.kt index ca7d604a..84290ffe 100644 --- a/app/src/main/java/code/name/monkey/retromusic/adapter/ContributorAdapter.kt +++ b/app/src/main/java/code/name/monkey/retromusic/adapter/ContributorAdapter.kt @@ -59,6 +59,11 @@ class ContributorAdapter( return contributors.size } + fun swapData(it: List) { + contributors = it + notifyDataSetChanged() + } + inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { val title: TextView = itemView.findViewById(R.id.title) val text: TextView = itemView.findViewById(R.id.text) @@ -68,7 +73,7 @@ class ContributorAdapter( title.text = contributor.name text.text = contributor.summary Glide.with(image.context) - .load(contributor.profileImage) + .load(contributor.image) .error(R.drawable.ic_account) .placeholder(R.drawable.ic_account) .dontAnimate() diff --git a/app/src/main/java/code/name/monkey/retromusic/fragments/LibraryViewModel.kt b/app/src/main/java/code/name/monkey/retromusic/fragments/LibraryViewModel.kt index 24895135..2109bde0 100644 --- a/app/src/main/java/code/name/monkey/retromusic/fragments/LibraryViewModel.kt +++ b/app/src/main/java/code/name/monkey/retromusic/fragments/LibraryViewModel.kt @@ -32,7 +32,6 @@ class LibraryViewModel( private val legacyPlaylists = MutableLiveData>() private val genres = MutableLiveData>() private val searchResults = MutableLiveData>() - val paletteColor: LiveData = _paletteColor val panelState: MutableLiveData = MutableLiveData() @@ -293,6 +292,10 @@ class LibraryViewModel( fun artist(artistId: Long): LiveData = liveData { emit(repository.artistById(artistId)) } + + fun fetchContributors(): LiveData> = liveData { + emit(repository.contributor()) + } } enum class ReloadType { diff --git a/app/src/main/java/code/name/monkey/retromusic/fragments/about/AboutFragment.kt b/app/src/main/java/code/name/monkey/retromusic/fragments/about/AboutFragment.kt index 5cbe0b76..abfea08e 100644 --- a/app/src/main/java/code/name/monkey/retromusic/fragments/about/AboutFragment.kt +++ b/app/src/main/java/code/name/monkey/retromusic/fragments/about/AboutFragment.kt @@ -13,18 +13,17 @@ import code.name.monkey.retromusic.App import code.name.monkey.retromusic.Constants import code.name.monkey.retromusic.R import code.name.monkey.retromusic.adapter.ContributorAdapter -import code.name.monkey.retromusic.model.Contributor +import code.name.monkey.retromusic.fragments.LibraryViewModel import code.name.monkey.retromusic.util.NavigationUtil -import com.google.gson.Gson -import com.google.gson.reflect.TypeToken import kotlinx.android.synthetic.main.card_credit.* import kotlinx.android.synthetic.main.card_other.* import kotlinx.android.synthetic.main.card_retro_info.* import kotlinx.android.synthetic.main.card_social.* -import java.io.IOException -import java.nio.charset.StandardCharsets +import org.koin.androidx.viewmodel.ext.android.sharedViewModel class AboutFragment : Fragment(R.layout.fragment_about), View.OnClickListener { + private val libraryViewModel by sharedViewModel() + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) version.setSummary(getAppVersion()) @@ -32,23 +31,6 @@ class AboutFragment : Fragment(R.layout.fragment_about), View.OnClickListener { loadContributors() } - private val contributorsJson: String? - get() { - val json: String - try { - val inputStream = requireActivity().assets.open("contributors.json") - val size = inputStream.available() - val buffer = ByteArray(size) - inputStream.read(buffer) - inputStream.close() - json = String(buffer, StandardCharsets.UTF_8) - } catch (ex: IOException) { - ex.printStackTrace() - return null - } - return json - } - private fun openUrl(url: String) { val i = Intent(Intent.ACTION_VIEW) @@ -111,16 +93,14 @@ class AboutFragment : Fragment(R.layout.fragment_about), View.OnClickListener { } private fun loadContributors() { - val type = object : TypeToken>() { - - }.type - val contributors = Gson().fromJson>(contributorsJson, type) - - val contributorAdapter = ContributorAdapter(contributors) + val contributorAdapter = ContributorAdapter(emptyList()) recyclerView.apply { layoutManager = LinearLayoutManager(requireContext()) itemAnimator = DefaultItemAnimator() adapter = contributorAdapter } + libraryViewModel.fetchContributors().observe(viewLifecycleOwner, { contributors -> + contributorAdapter.swapData(contributors) + }) } } diff --git a/app/src/main/java/code/name/monkey/retromusic/model/Contributor.kt b/app/src/main/java/code/name/monkey/retromusic/model/Contributor.kt index 6ea869f3..23f952a3 100644 --- a/app/src/main/java/code/name/monkey/retromusic/model/Contributor.kt +++ b/app/src/main/java/code/name/monkey/retromusic/model/Contributor.kt @@ -14,12 +14,14 @@ package code.name.monkey.retromusic.model +import android.os.Parcelable import com.google.gson.annotations.SerializedName +import kotlinx.android.parcel.Parcelize -data class Contributor( - val name: String, - val summary: String, - val link: String, - @SerializedName("profile_image") - val profileImage: String -) \ No newline at end of file +@Parcelize +class Contributor( + @SerializedName("name") val name: String = "", + @SerializedName("summary") val summary: String = "", + @SerializedName("link") val link: String = "", + @SerializedName("image") val image: String = "" +) : Parcelable \ No newline at end of file diff --git a/app/src/main/java/code/name/monkey/retromusic/repository/LocalDataRepository.kt b/app/src/main/java/code/name/monkey/retromusic/repository/LocalDataRepository.kt new file mode 100644 index 00000000..a4063305 --- /dev/null +++ b/app/src/main/java/code/name/monkey/retromusic/repository/LocalDataRepository.kt @@ -0,0 +1,25 @@ +package code.name.monkey.retromusic.repository + +import android.content.Context +import code.name.monkey.retromusic.model.Contributor +import com.google.gson.GsonBuilder +import com.google.gson.reflect.TypeToken + +interface LocalDataRepository { + fun contributors(): List +} + +class RealLocalDataRepository( + private val context: Context +) : LocalDataRepository { + + override fun contributors(): List { + val jsonString = context.assets.open("contributors.json") + .bufferedReader().use { it.readText() } + + val gsonBuilder = GsonBuilder() + val gson = gsonBuilder.create() + val listContributorType = object : TypeToken>() {}.type + return gson.fromJson(jsonString, listContributorType) + } +} \ No newline at end of file diff --git a/app/src/main/java/code/name/monkey/retromusic/repository/Repository.kt b/app/src/main/java/code/name/monkey/retromusic/repository/Repository.kt index fb549aa4..9264198a 100644 --- a/app/src/main/java/code/name/monkey/retromusic/repository/Repository.kt +++ b/app/src/main/java/code/name/monkey/retromusic/repository/Repository.kt @@ -98,6 +98,7 @@ interface Repository { suspend fun blackListPaths(): List suspend fun lyrics(artist: String, title: String): Result suspend fun deleteSongs(songs: List) + suspend fun contributor(): List } class RealRepository( @@ -112,7 +113,8 @@ class RealRepository( private val searchRepository: RealSearchRepository, private val topPlayedRepository: TopPlayedRepository, private val roomRepository: RoomRepository, - private val lyricsRestService: LyricsRestService + private val lyricsRestService: LyricsRestService, + private val localDataRepository: LocalDataRepository ) : Repository { override suspend fun lyrics(artist: String, title: String): Result = try { @@ -124,6 +126,8 @@ class RealRepository( override suspend fun deleteSongs(songs: List) = roomRepository.deleteSongs(songs) + override suspend fun contributor(): List = localDataRepository.contributors() + override suspend fun fetchAlbums(): List = albumRepository.albums() override suspend fun albumByIdAsync(albumId: Long): Album = albumRepository.album(albumId)