From 6cdaf27edad94f63f2aee300720548ea048ece1d Mon Sep 17 00:00:00 2001 From: h4h13 Date: Sat, 25 Apr 2020 03:04:39 +0530 Subject: [PATCH] Refactor code --- .../src/main/assets}/contributors.json | 0 .../retromusic/activities/AboutActivity.kt | 49 ++++++---------- .../retromusic/adapter/TranslatorsAdapter.kt | 9 +-- .../retromusic/dagger/MusicComponent.kt | 2 - .../dagger/module/PresenterModile.kt | 6 -- .../retromusic/data/RetroDataService.kt | 2 +- .../dialogs/RenamePlaylistDialog.kt | 4 +- .../mvp/presenter/AboutPresenter.kt | 57 ------------------- .../retromusic/providers/RepositoryImpl.kt | 19 +------ .../providers/interfaces/Repository.kt | 4 -- .../res/drawable/ic_license_white_24dp.xml | 7 ++- app/src/main/res/layout/card_other.xml | 17 +----- data/translators.json | 12 ---- 13 files changed, 28 insertions(+), 160 deletions(-) rename {data => app/src/main/assets}/contributors.json (100%) delete mode 100644 app/src/main/java/code/name/monkey/retromusic/mvp/presenter/AboutPresenter.kt delete mode 100644 data/translators.json diff --git a/data/contributors.json b/app/src/main/assets/contributors.json similarity index 100% rename from data/contributors.json rename to app/src/main/assets/contributors.json diff --git a/app/src/main/java/code/name/monkey/retromusic/activities/AboutActivity.kt b/app/src/main/java/code/name/monkey/retromusic/activities/AboutActivity.kt index 736d3560..704e92c6 100644 --- a/app/src/main/java/code/name/monkey/retromusic/activities/AboutActivity.kt +++ b/app/src/main/java/code/name/monkey/retromusic/activities/AboutActivity.kt @@ -25,28 +25,29 @@ import code.name.monkey.retromusic.R import code.name.monkey.retromusic.activities.base.AbsBaseActivity import code.name.monkey.retromusic.adapter.ContributorAdapter import code.name.monkey.retromusic.model.Contributor -import code.name.monkey.retromusic.mvp.presenter.AboutPresenter -import code.name.monkey.retromusic.mvp.presenter.AboutView import code.name.monkey.retromusic.util.NavigationUtil import code.name.monkey.retromusic.util.PreferenceUtil import com.afollestad.materialdialogs.LayoutMode import com.afollestad.materialdialogs.MaterialDialog import com.afollestad.materialdialogs.bottomsheets.BottomSheet import com.afollestad.materialdialogs.list.listItems +import com.google.gson.Gson +import com.google.gson.reflect.TypeToken import kotlinx.android.synthetic.main.activity_about.* 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 javax.inject.Inject +import java.io.IOException +import java.nio.charset.StandardCharsets -class AboutActivity : AbsBaseActivity(), View.OnClickListener, AboutView { +class AboutActivity : AbsBaseActivity(), View.OnClickListener { - /*private val assetJsonData: String? + private val contributorsJson: String? get() { val json: String try { - val inputStream = assets.open("data/contributors.json") + val inputStream = assets.open("contributors.json") val size = inputStream.available() val buffer = ByteArray(size) inputStream.read(buffer) @@ -56,12 +57,9 @@ class AboutActivity : AbsBaseActivity(), View.OnClickListener, AboutView { ex.printStackTrace() return null } - return json } -*/ - @Inject - lateinit var aboutPresenter: AboutPresenter + override fun onCreate(savedInstanceState: Bundle?) { setDrawUnderStatusBar() @@ -71,27 +69,13 @@ class AboutActivity : AbsBaseActivity(), View.OnClickListener, AboutView { setNavigationbarColorAuto() setLightNavigationBar(true) - App.musicComponent.inject(this) - aboutPresenter.attachView(this) - val toolbarColor = ATHUtil.resolveColor(this, R.attr.colorSurface) toolbar.setBackgroundColor(toolbarColor) ToolbarContentTintHelper.colorBackButton(toolbar) setSupportActionBar(toolbar) version.setSummary(getAppVersion()) setUpView() - //loadContributors() - } - - override fun onResume() { - super.onResume() - aboutPresenter.loadContributors() - aboutPresenter.loadTranslators() - } - - override fun onDestroy() { - super.onDestroy() - aboutPresenter.detachView() + loadContributors() } override fun onOptionsItemSelected(item: MenuItem): Boolean { @@ -123,7 +107,6 @@ class AboutActivity : AbsBaseActivity(), View.OnClickListener, AboutView { openSource.setOnClickListener(this) pinterestLink.setOnClickListener(this) bugReportLink.setOnClickListener(this) - translators.setOnClickListener(this) } override fun onClick(view: View) { @@ -141,7 +124,6 @@ class AboutActivity : AbsBaseActivity(), View.OnClickListener, AboutView { R.id.changelog -> showChangeLogOptions() R.id.openSource -> NavigationUtil.goToOpenSource(this) R.id.bugReportLink -> NavigationUtil.bugReport(this) - R.id.translators -> openUrl(""); } } @@ -175,14 +157,15 @@ class AboutActivity : AbsBaseActivity(), View.OnClickListener, AboutView { .setText(String.format(getString(R.string.app_share), packageName)).startChooser() } - override fun showContributors(contributor: List) { - val contributorAdapter = ContributorAdapter(contributor) + private fun loadContributors() { + val type = object : TypeToken>() { + + }.type + val contributors = Gson().fromJson>(contributorsJson, type) + + val contributorAdapter = ContributorAdapter(contributors) recyclerView.layoutManager = LinearLayoutManager(this) recyclerView.itemAnimator = DefaultItemAnimator() recyclerView.adapter = contributorAdapter } - - override fun translators(contributor: List) { - - } } diff --git a/app/src/main/java/code/name/monkey/retromusic/adapter/TranslatorsAdapter.kt b/app/src/main/java/code/name/monkey/retromusic/adapter/TranslatorsAdapter.kt index 45966071..90585c36 100644 --- a/app/src/main/java/code/name/monkey/retromusic/adapter/TranslatorsAdapter.kt +++ b/app/src/main/java/code/name/monkey/retromusic/adapter/TranslatorsAdapter.kt @@ -7,10 +7,10 @@ import android.view.ViewGroup import android.widget.TextView import androidx.recyclerview.widget.RecyclerView import code.name.monkey.retromusic.R +import code.name.monkey.retromusic.extensions.hide import code.name.monkey.retromusic.model.Contributor import code.name.monkey.retromusic.util.RetroUtil import code.name.monkey.retromusic.views.RetroShapeableImageView -import com.bumptech.glide.Glide class TranslatorsAdapter( private var contributors: List @@ -46,12 +46,7 @@ class TranslatorsAdapter( internal fun bindData(contributor: Contributor) { title.text = contributor.name text.text = contributor.summary - Glide.with(image.context) - .load(contributor.profileImage) - .error(R.drawable.ic_account_white_24dp) - .placeholder(R.drawable.ic_account_white_24dp) - .dontAnimate() - .into(image) + image.hide() } } } \ No newline at end of file diff --git a/app/src/main/java/code/name/monkey/retromusic/dagger/MusicComponent.kt b/app/src/main/java/code/name/monkey/retromusic/dagger/MusicComponent.kt index 579b08c0..361bb91a 100644 --- a/app/src/main/java/code/name/monkey/retromusic/dagger/MusicComponent.kt +++ b/app/src/main/java/code/name/monkey/retromusic/dagger/MusicComponent.kt @@ -54,6 +54,4 @@ interface MusicComponent { fun inject(searchActivity: SearchActivity) fun inject(bannerHomeFragment: BannerHomeFragment) - - fun inject(aboutActivity: AboutActivity) } diff --git a/app/src/main/java/code/name/monkey/retromusic/dagger/module/PresenterModile.kt b/app/src/main/java/code/name/monkey/retromusic/dagger/module/PresenterModile.kt index d81ca859..880ecc88 100644 --- a/app/src/main/java/code/name/monkey/retromusic/dagger/module/PresenterModile.kt +++ b/app/src/main/java/code/name/monkey/retromusic/dagger/module/PresenterModile.kt @@ -16,7 +16,6 @@ package code.name.monkey.retromusic.dagger.module import android.content.Context import code.name.monkey.retromusic.mvp.presenter.* -import code.name.monkey.retromusic.mvp.presenter.AboutPresenter.AboutPresenterImpl import code.name.monkey.retromusic.mvp.presenter.AlbumDetailsPresenter.AlbumDetailsPresenterImpl import code.name.monkey.retromusic.mvp.presenter.AlbumsPresenter.AlbumsPresenterImpl import code.name.monkey.retromusic.mvp.presenter.ArtistDetailsPresenter.ArtistDetailsPresenterImpl @@ -99,9 +98,4 @@ class PresenterModule { fun providesSongPresenter(presenter: SongPresenterImpl): SongPresenter { return presenter } - - @Provides - fun providesAboutPresenter(presenter: AboutPresenterImpl): AboutPresenter { - return presenter - } } \ No newline at end of file diff --git a/app/src/main/java/code/name/monkey/retromusic/data/RetroDataService.kt b/app/src/main/java/code/name/monkey/retromusic/data/RetroDataService.kt index 02987574..cb7cee6d 100644 --- a/app/src/main/java/code/name/monkey/retromusic/data/RetroDataService.kt +++ b/app/src/main/java/code/name/monkey/retromusic/data/RetroDataService.kt @@ -17,7 +17,7 @@ interface RetroDataService { suspend fun getTranslators(): List companion object { - val retoService = Retrofit.Builder() + val retoService: RetroDataService = Retrofit.Builder() .baseUrl(BASE_URL) .addConverterFactory(GsonConverterFactory.create(GsonBuilder().create())) .build() diff --git a/app/src/main/java/code/name/monkey/retromusic/dialogs/RenamePlaylistDialog.kt b/app/src/main/java/code/name/monkey/retromusic/dialogs/RenamePlaylistDialog.kt index c8a85059..e94d83b6 100644 --- a/app/src/main/java/code/name/monkey/retromusic/dialogs/RenamePlaylistDialog.kt +++ b/app/src/main/java/code/name/monkey/retromusic/dialogs/RenamePlaylistDialog.kt @@ -63,10 +63,10 @@ class RenamePlaylistDialog : DialogFragment() { MaterialUtil.setTint(actionNewPlaylistContainer, false) - val playlistId = arguments!!.getLong(PLAYLIST_ID) + val playlistId = requireArguments().getLong(PLAYLIST_ID) playlistView.appHandleColor() .setText( - PlaylistsUtil.getNameForPlaylist(context!!, playlistId), + PlaylistsUtil.getNameForPlaylist(requireContext(), playlistId), TextView.BufferType.EDITABLE ) return materialDialog diff --git a/app/src/main/java/code/name/monkey/retromusic/mvp/presenter/AboutPresenter.kt b/app/src/main/java/code/name/monkey/retromusic/mvp/presenter/AboutPresenter.kt deleted file mode 100644 index 9cbcaf24..00000000 --- a/app/src/main/java/code/name/monkey/retromusic/mvp/presenter/AboutPresenter.kt +++ /dev/null @@ -1,57 +0,0 @@ -package code.name.monkey.retromusic.mvp.presenter - -import code.name.monkey.retromusic.Result.Error -import code.name.monkey.retromusic.Result.Success -import code.name.monkey.retromusic.model.Contributor -import code.name.monkey.retromusic.mvp.Presenter -import code.name.monkey.retromusic.mvp.PresenterImpl -import code.name.monkey.retromusic.providers.interfaces.Repository -import kotlinx.coroutines.* -import javax.inject.Inject -import kotlin.coroutines.CoroutineContext - -interface AboutView { - fun showContributors(contributor: List) - fun translators(contributor: List) - -} - -interface AboutPresenter : Presenter { - fun loadContributors() - fun loadTranslators() - class AboutPresenterImpl @Inject constructor( - private val repository: Repository - ) : PresenterImpl(), AboutPresenter, CoroutineScope { - - override fun loadContributors() { - launch { - when (val result = repository.contributors()) { - is Success -> withContext(Dispatchers.Main) { - view.showContributors( - result.data - ) - } - is Error -> withContext(Dispatchers.Main) {} - } - } - } - - override fun loadTranslators() { - launch { - when (val result = repository.translators()) { - is Success -> withContext(Dispatchers.Main) { - view.translators( - result.data - ) - } - is Error -> withContext(Dispatchers.Main) {} - } - } - } - - private var job: Job = Job() - override val coroutineContext: CoroutineContext - get() = Dispatchers.IO + job - - } -} \ No newline at end of file diff --git a/app/src/main/java/code/name/monkey/retromusic/providers/RepositoryImpl.kt b/app/src/main/java/code/name/monkey/retromusic/providers/RepositoryImpl.kt index 85560bd6..e54cd5d0 100644 --- a/app/src/main/java/code/name/monkey/retromusic/providers/RepositoryImpl.kt +++ b/app/src/main/java/code/name/monkey/retromusic/providers/RepositoryImpl.kt @@ -20,7 +20,6 @@ import code.name.monkey.retromusic.Result import code.name.monkey.retromusic.Result.Error import code.name.monkey.retromusic.Result.Success import code.name.monkey.retromusic.adapter.HomeAdapter -import code.name.monkey.retromusic.data.RetroDataService import code.name.monkey.retromusic.loaders.* import code.name.monkey.retromusic.model.* import code.name.monkey.retromusic.providers.interfaces.Repository @@ -284,22 +283,6 @@ class RepositoryImpl @Inject constructor(private val context: Context) : Reposit Error(Throwable("Error loading artist")) } } - - override suspend fun contributors(): Result> { - return try { - Success(RetroDataService.retoService.getContributors()) - } catch (e: Exception) { - Error(Throwable(e)) - } - } - - override suspend fun translators(): Result> { - return try { - Success(RetroDataService.retoService.getTranslators()) - } catch (e: Exception) { - Error(Throwable(e)) - } - } } suspend fun safeApiCall(call: suspend () -> Result, errorMessage: String): Result = @@ -307,4 +290,4 @@ suspend fun safeApiCall(call: suspend () -> Result, errorMessage: S call.invoke() } catch (e: Exception) { Error(IOException(errorMessage, e)) - } + } \ No newline at end of file diff --git a/app/src/main/java/code/name/monkey/retromusic/providers/interfaces/Repository.kt b/app/src/main/java/code/name/monkey/retromusic/providers/interfaces/Repository.kt index ef0d64a7..a3e9f4d8 100644 --- a/app/src/main/java/code/name/monkey/retromusic/providers/interfaces/Repository.kt +++ b/app/src/main/java/code/name/monkey/retromusic/providers/interfaces/Repository.kt @@ -58,8 +58,4 @@ interface Repository { suspend fun albumInfo(artist: String, album: String): Result suspend fun artistById(artistId: Int): Result - - suspend fun contributors(): Result> - - suspend fun translators(): Result> } \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_license_white_24dp.xml b/app/src/main/res/drawable/ic_license_white_24dp.xml index f66c7104..9cba8427 100644 --- a/app/src/main/res/drawable/ic_license_white_24dp.xml +++ b/app/src/main/res/drawable/ic_license_white_24dp.xml @@ -1,8 +1,9 @@ - - + \ No newline at end of file diff --git a/app/src/main/res/layout/card_other.xml b/app/src/main/res/layout/card_other.xml index aeb37d9e..30c263f5 100644 --- a/app/src/main/res/layout/card_other.xml +++ b/app/src/main/res/layout/card_other.xml @@ -43,7 +43,7 @@ app:listItemTitle="@string/changelog" /> - - diff --git a/data/translators.json b/data/translators.json deleted file mode 100644 index 0e474b39..00000000 --- a/data/translators.json +++ /dev/null @@ -1,12 +0,0 @@ -[ - { - "name": "Akos Paha", - "summary": "Hungarian", - "flag": "" - }, - { - "name": "Akos Paha", - "summary": "Hungarian", - "flag": "" - } -] \ No newline at end of file