Refactor code

This commit is contained in:
h4h13 2020-04-25 03:04:39 +05:30
parent 369411ffc6
commit 6cdaf27eda
13 changed files with 28 additions and 160 deletions

View file

@ -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<Contributor>) {
val contributorAdapter = ContributorAdapter(contributor)
private fun loadContributors() {
val type = object : TypeToken<List<Contributor>>() {
}.type
val contributors = Gson().fromJson<List<Contributor>>(contributorsJson, type)
val contributorAdapter = ContributorAdapter(contributors)
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.itemAnimator = DefaultItemAnimator()
recyclerView.adapter = contributorAdapter
}
override fun translators(contributor: List<Contributor>) {
}
}

View file

@ -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<Contributor>
@ -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()
}
}
}

View file

@ -54,6 +54,4 @@ interface MusicComponent {
fun inject(searchActivity: SearchActivity)
fun inject(bannerHomeFragment: BannerHomeFragment)
fun inject(aboutActivity: AboutActivity)
}

View file

@ -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
}
}

View file

@ -17,7 +17,7 @@ interface RetroDataService {
suspend fun getTranslators(): List<Contributor>
companion object {
val retoService = Retrofit.Builder()
val retoService: RetroDataService = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(GsonBuilder().create()))
.build()

View file

@ -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

View file

@ -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<Contributor>)
fun translators(contributor: List<Contributor>)
}
interface AboutPresenter : Presenter<AboutView> {
fun loadContributors()
fun loadTranslators()
class AboutPresenterImpl @Inject constructor(
private val repository: Repository
) : PresenterImpl<AboutView>(), 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
}
}

View file

@ -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<List<Contributor>> {
return try {
Success(RetroDataService.retoService.getContributors())
} catch (e: Exception) {
Error(Throwable(e))
}
}
override suspend fun translators(): Result<List<Contributor>> {
return try {
Success(RetroDataService.retoService.getTranslators())
} catch (e: Exception) {
Error(Throwable(e))
}
}
}
suspend fun <T : Any> safeApiCall(call: suspend () -> Result<T>, errorMessage: String): Result<T> =

View file

@ -58,8 +58,4 @@ interface Repository {
suspend fun albumInfo(artist: String, album: String): Result<LastFmAlbum>
suspend fun artistById(artistId: Int): Result<Artist>
suspend fun contributors(): Result<List<Contributor>>
suspend fun translators(): Result<List<Contributor>>
}

View file

@ -1,8 +1,9 @@
<!-- drawable/ic_license_white_24dpnse_white_24dp.xml -->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path android:fillColor="#000" android:pathData="M9 10A3.04 3.04 0 0 1 12 7A3.04 3.04 0 0 1 15 10A3.04 3.04 0 0 1 12 13A3.04 3.04 0 0 1 9 10M12 19L16 20V16.92A7.54 7.54 0 0 1 12 18A7.54 7.54 0 0 1 8 16.92V20M12 4A5.78 5.78 0 0 0 7.76 5.74A5.78 5.78 0 0 0 6 10A5.78 5.78 0 0 0 7.76 14.23A5.78 5.78 0 0 0 12 16A5.78 5.78 0 0 0 16.24 14.23A5.78 5.78 0 0 0 18 10A5.78 5.78 0 0 0 16.24 5.74A5.78 5.78 0 0 0 12 4M20 10A8.04 8.04 0 0 1 19.43 12.8A7.84 7.84 0 0 1 18 15.28V23L12 21L6 23V15.28A7.9 7.9 0 0 1 4 10A7.68 7.68 0 0 1 6.33 4.36A7.73 7.73 0 0 1 12 2A7.73 7.73 0 0 1 17.67 4.36A7.68 7.68 0 0 1 20 10Z" />
<path
android:fillColor="#FFFFFF"
android:pathData="M9 10A3.04 3.04 0 0 1 12 7A3.04 3.04 0 0 1 15 10A3.04 3.04 0 0 1 12 13A3.04 3.04 0 0 1 9 10M12 19L16 20V16.92A7.54 7.54 0 0 1 12 18A7.54 7.54 0 0 1 8 16.92V20M12 4A5.78 5.78 0 0 0 7.76 5.74A5.78 5.78 0 0 0 6 10A5.78 5.78 0 0 0 7.76 14.23A5.78 5.78 0 0 0 12 16A5.78 5.78 0 0 0 16.24 14.23A5.78 5.78 0 0 0 18 10A5.78 5.78 0 0 0 16.24 5.74A5.78 5.78 0 0 0 12 4M20 10A8.04 8.04 0 0 1 19.43 12.8A7.84 7.84 0 0 1 18 15.28V23L12 21L6 23V15.28A7.9 7.9 0 0 1 4 10A7.68 7.68 0 0 1 6.33 4.36A7.73 7.73 0 0 1 12 2A7.73 7.73 0 0 1 17.67 4.36A7.68 7.68 0 0 1 20 10Z" />
</vector>

View file

@ -43,7 +43,7 @@
app:listItemTitle="@string/changelog" />
<code.name.monkey.retromusic.views.ListItemView
android:id="@+id/translators"
android:id="@+id/openSource"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constrainedWidth="true"
@ -51,33 +51,20 @@
app:layout_constraintHorizontal_bias="0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/changelog"
app:listItemIcon="@drawable/ic_account_group_white_24dp"
app:listItemSummary="@string/translators_summary"
app:listItemTitle="@string/translators" />
<code.name.monkey.retromusic.views.ListItemView
android:id="@+id/openSource"
android:layout_width="0dp"
app:listItemIcon="@drawable/ic_license_white_24dp"
android:layout_height="wrap_content"
app:layout_constrainedWidth="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/translators"
app:listItemSummary="@string/pref_summary_open_source_licences"
app:listItemTitle="@string/pref_title_open_source_licences" />
<code.name.monkey.retromusic.views.ListItemView
android:id="@+id/version"
android:layout_width="0dp"
app:listItemIcon="@drawable/ic_info_outline_white_24dp"
android:layout_height="wrap_content"
app:layout_constrainedWidth="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/openSource"
app:listItemIcon="@drawable/ic_info_outline_white_24dp"
app:listItemSummary="@string/pref_summary_open_source_licences"
app:listItemTitle="@string/version" />

View file

@ -1,12 +0,0 @@
[
{
"name": "Akos Paha",
"summary": "Hungarian",
"flag": ""
},
{
"name": "Akos Paha",
"summary": "Hungarian",
"flag": ""
}
]