From 369411ffc6e99dbb48ca3ff5ce30c687443fa838 Mon Sep 17 00:00:00 2001 From: h4h13 Date: Sat, 25 Apr 2020 00:20:46 +0530 Subject: [PATCH] Added tranlators --- .../retromusic/activities/AboutActivity.kt | 45 ++++++++++----- .../retromusic/dagger/MusicComponent.kt | 2 + .../dagger/module/PresenterModile.kt | 6 ++ .../retromusic/data/RetroDataService.kt | 26 +++++++++ .../retromusic/deezer/DeezerApiService.kt | 7 +-- .../mvp/presenter/AboutPresenter.kt | 57 +++++++++++++++++++ .../retromusic/providers/RepositoryImpl.kt | 17 ++++++ .../providers/interfaces/Repository.kt | 4 ++ .../monkey/retromusic/rest/LastFmClient.kt | 5 +- .../main/assets => data}/contributors.json | 0 10 files changed, 147 insertions(+), 22 deletions(-) create mode 100644 app/src/main/java/code/name/monkey/retromusic/data/RetroDataService.kt create mode 100644 app/src/main/java/code/name/monkey/retromusic/mvp/presenter/AboutPresenter.kt rename {app/src/main/assets => data}/contributors.json (100%) 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 f938222d..736d3560 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,29 +25,28 @@ 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 java.io.IOException -import java.nio.charset.StandardCharsets +import javax.inject.Inject -class AboutActivity : AbsBaseActivity(), View.OnClickListener { +class AboutActivity : AbsBaseActivity(), View.OnClickListener, AboutView { - private val assetJsonData: String? + /*private val assetJsonData: String? get() { val json: String try { - val inputStream = assets.open("contributors.json") + val inputStream = assets.open("data/contributors.json") val size = inputStream.available() val buffer = ByteArray(size) inputStream.read(buffer) @@ -60,6 +59,9 @@ class AboutActivity : AbsBaseActivity(), View.OnClickListener { return json } +*/ + @Inject + lateinit var aboutPresenter: AboutPresenter override fun onCreate(savedInstanceState: Bundle?) { setDrawUnderStatusBar() @@ -69,13 +71,27 @@ class AboutActivity : AbsBaseActivity(), View.OnClickListener { 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() + //loadContributors() + } + + override fun onResume() { + super.onResume() + aboutPresenter.loadContributors() + aboutPresenter.loadTranslators() + } + + override fun onDestroy() { + super.onDestroy() + aboutPresenter.detachView() } override fun onOptionsItemSelected(item: MenuItem): Boolean { @@ -159,15 +175,14 @@ class AboutActivity : AbsBaseActivity(), View.OnClickListener { .setText(String.format(getString(R.string.app_share), packageName)).startChooser() } - private fun loadContributors() { - val type = object : TypeToken>() { - - }.type - val contributors = Gson().fromJson>(assetJsonData, type) - - val contributorAdapter = ContributorAdapter(contributors) + override fun showContributors(contributor: List) { + val contributorAdapter = ContributorAdapter(contributor) 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/dagger/MusicComponent.kt b/app/src/main/java/code/name/monkey/retromusic/dagger/MusicComponent.kt index 361bb91a..579b08c0 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,4 +54,6 @@ 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 880ecc88..d81ca859 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,6 +16,7 @@ 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 @@ -98,4 +99,9 @@ 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 new file mode 100644 index 00000000..02987574 --- /dev/null +++ b/app/src/main/java/code/name/monkey/retromusic/data/RetroDataService.kt @@ -0,0 +1,26 @@ +package code.name.monkey.retromusic.data + +import code.name.monkey.retromusic.model.Contributor +import com.google.gson.GsonBuilder +import retrofit2.Retrofit +import retrofit2.converter.gson.GsonConverterFactory +import retrofit2.http.GET + +private const val BASE_URL = "https://github.com/h4h13/RetroMusicPlayer/blob/dev/data/" + +interface RetroDataService { + + @GET("translators.json") + suspend fun getContributors(): List + + @GET("translators.json") + suspend fun getTranslators(): List + + companion object { + val retoService = Retrofit.Builder() + .baseUrl(BASE_URL) + .addConverterFactory(GsonConverterFactory.create(GsonBuilder().create())) + .build() + .create(RetroDataService::class.java) + } +} diff --git a/app/src/main/java/code/name/monkey/retromusic/deezer/DeezerApiService.kt b/app/src/main/java/code/name/monkey/retromusic/deezer/DeezerApiService.kt index 95daf6bd..fd107234 100644 --- a/app/src/main/java/code/name/monkey/retromusic/deezer/DeezerApiService.kt +++ b/app/src/main/java/code/name/monkey/retromusic/deezer/DeezerApiService.kt @@ -37,10 +37,9 @@ interface DeezerApiService { fun createDefaultOkHttpClient( context: Context - ): OkHttpClient.Builder = - OkHttpClient.Builder() - .cache(createDefaultCache(context)) - .addInterceptor(createCacheControlInterceptor()) + ): OkHttpClient.Builder = OkHttpClient.Builder() + .cache(createDefaultCache(context)) + .addInterceptor(createCacheControlInterceptor()) private fun createDefaultCache( context: Context 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 new file mode 100644 index 00000000..9cbcaf24 --- /dev/null +++ b/app/src/main/java/code/name/monkey/retromusic/mvp/presenter/AboutPresenter.kt @@ -0,0 +1,57 @@ +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 a85017f0..85560bd6 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,6 +20,7 @@ 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 @@ -283,6 +284,22 @@ 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 = 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 a3e9f4d8..ef0d64a7 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,4 +58,8 @@ 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/java/code/name/monkey/retromusic/rest/LastFmClient.kt b/app/src/main/java/code/name/monkey/retromusic/rest/LastFmClient.kt index 5c2e6e85..f2449e72 100644 --- a/app/src/main/java/code/name/monkey/retromusic/rest/LastFmClient.kt +++ b/app/src/main/java/code/name/monkey/retromusic/rest/LastFmClient.kt @@ -24,13 +24,12 @@ object LastFmClient { } init { - val retrofit = Retrofit.Builder() + lastFMService = Retrofit.Builder() .baseUrl(baseUrl) .callFactory(createDefaultOkHttpClientBuilder().build()) .addConverterFactory(GsonConverterFactory.create(Gson())) .build() - - lastFMService = retrofit.create(LastFMService::class.java) + .create(LastFMService::class.java) } private fun createDefaultOkHttpClientBuilder(): OkHttpClient.Builder { diff --git a/app/src/main/assets/contributors.json b/data/contributors.json similarity index 100% rename from app/src/main/assets/contributors.json rename to data/contributors.json