Skip to content

Commit

Permalink
[W.I.P] Rewrite app to MVP with Dependency Injection part 5 #22
Browse files Browse the repository at this point in the history
  • Loading branch information
xdk78 committed Mar 4, 2018
1 parent 57cdd13 commit f452d89
Show file tree
Hide file tree
Showing 21 changed files with 494 additions and 268 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import cat.pantsu.nyaapantsu.R
import cat.pantsu.nyaapantsu.mvp.model.TorrentListResponse
import cat.pantsu.nyaapantsu.mvp.model.TorrentModel
import cat.pantsu.nyaapantsu.ui.activity.TorrentActivity
import cat.pantsu.nyaapantsu.util.download
import cat.pantsu.nyaapantsu.util.formatDate
import kotlinx.android.synthetic.main.torrent_item.view.*
import org.jetbrains.anko.toast


class TorrentListAdapter(var context: Context, private var torrentList: TorrentListResponse<TorrentModel>) : RecyclerView.Adapter<TorrentListAdapter.TorrentListViewHolder>() {
Expand All @@ -33,21 +33,21 @@ class TorrentListAdapter(var context: Context, private var torrentList: TorrentL
holder.itemView.stats.text = "S: " + item.seeders + " L: " + item.leechers
holder.itemView.date.text = item.date.formatDate()

holder.itemView.download.setOnClickListener { _ ->
holder.itemView.download.setOnClickListener {
when {
!TextUtils.isEmpty(item.torrent) -> download(context as Activity, holder.itemView, item.torrent, item.name)
else -> context.toast(context.getString(R.string.torrent_not_available))
else -> Toast.makeText(context, R.string.torrent_not_available, Toast.LENGTH_SHORT).show()
}
}

holder.itemView.copy.setOnClickListener { _ ->
holder.itemView.copy.setOnClickListener {
val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val clipData = ClipData.newPlainText(item.name, item.magnet)
clipboard.primaryClip = clipData
context.toast(context.getString(R.string.magnet_copied))
Toast.makeText(context, R.string.magnet_copied, Toast.LENGTH_SHORT).show()
}

holder.itemView.cardview.setOnClickListener { _ ->
holder.itemView.cardview.setOnClickListener {
context.startActivity(TorrentActivity.createIntent(context, item.id, item.name))
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cat.pantsu.nyaapantsu.di.module

import cat.pantsu.nyaapantsu.di.module.about.AboutFragmentProvider
import cat.pantsu.nyaapantsu.di.module.recent.RecentFragmentProvider
import cat.pantsu.nyaapantsu.di.module.favorite.FavoritesFragmentProvider
import cat.pantsu.nyaapantsu.di.module.search.SearchTorrentListModule
import cat.pantsu.nyaapantsu.di.module.torrent.TorrentListFragmentProvider
import cat.pantsu.nyaapantsu.di.module.torrent.TorrentListModule
Expand All @@ -19,7 +19,7 @@ abstract class ActivityBuilder {

@ContributesAndroidInjector(modules = [
TorrentListModule::class, TorrentListFragmentProvider::class,
AboutFragmentProvider::class, RecentFragmentProvider::class
AboutFragmentProvider::class, FavoritesFragmentProvider::class
])

abstract fun bindHomeActivity(): HomeActivity
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package cat.pantsu.nyaapantsu.di.module.favorite

import cat.pantsu.nyaapantsu.ui.fragment.FavoritesFragment
import dagger.Module
import dagger.android.ContributesAndroidInjector


@Module
abstract class FavoritesFragmentProvider {
@ContributesAndroidInjector()
abstract fun provideFavoritesFragment(): FavoritesFragment

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import android.view.MenuItem
import cat.pantsu.nyaapantsu.R
import cat.pantsu.nyaapantsu.base.BaseActivity
import cat.pantsu.nyaapantsu.ui.fragment.AboutFragment
import cat.pantsu.nyaapantsu.ui.fragment.RecentFragment
import cat.pantsu.nyaapantsu.ui.fragment.FavoritesFragment
import cat.pantsu.nyaapantsu.ui.fragment.TorrentListFragment
import kotlinx.android.synthetic.main.activity_home.*
import org.jetbrains.anko.startActivity
Expand Down Expand Up @@ -58,8 +58,8 @@ class HomeActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList
R.id.nav_search -> {
startActivity<SearchActivity>()
}
R.id.nav_recent -> {
val fragment = RecentFragment.newInstance()
R.id.nav_favorites -> {
val fragment = FavoritesFragment.newInstance()
supportFragmentManager.beginTransaction().replace(R.id.main_fragment, fragment).commit()
}
R.id.nav_about -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import android.support.v7.widget.SearchView
import android.util.Log
import android.view.Menu
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import cat.pantsu.nyaapantsu.R
import cat.pantsu.nyaapantsu.adapter.TorrentListAdapter
import cat.pantsu.nyaapantsu.base.BaseActivity
Expand All @@ -17,6 +19,7 @@ import cat.pantsu.nyaapantsu.mvp.presenter.SearchTorrentListPresenter
import cat.pantsu.nyaapantsu.mvp.view.SearchTorrentListView
import kotlinx.android.synthetic.main.activity_search.*
import kotlinx.android.synthetic.main.fragment_search_torrent_list.*
import kotlinx.android.synthetic.main.search_filter_layout.*
import kotlinx.android.synthetic.main.toolbar.*
import org.jetbrains.anko.contentView
import javax.inject.Inject
Expand All @@ -31,18 +34,79 @@ class SearchActivity : BaseActivity(), SearchTorrentListView {

lateinit var recyclerView: RecyclerView

private val categories = arrayOf("_", "3_", "3_12", "3_5", "3_13", "3_6", "2_", "2_3", "2_4", "4_", "4_7", "4_14", "4_8", "5_", "5_9", "5_10", "5_18", "5_11", "6_", "6_15", "6_16", "1_", "1_1", "1_2")
private val status = arrayOf("0", "2", "3", "4")
private val sizes = arrayOf("B", "KiB", "MiB", "GiB")
private val sizesVal = arrayOf("b", "k", "m", "g")
private var c = ""
private var s = ""
private var selectedSize = ""

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_search)
setSupportActionBar(toolbar)
supportActionBar!!.setDisplayHomeAsUpEnabled(true)
supportActionBar!!.setDisplayShowHomeEnabled(true)

val catAdapter = ArrayAdapter.createFromResource(this, R.array.cat_array, R.layout.spinner_layout)
catSpinner.adapter = catAdapter
catSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
c = categories[p2]
}

override fun onNothingSelected(p0: AdapterView<*>?) {
c = "_"
}
}

val sizeAdapter = ArrayAdapter(this, R.layout.spinner_layout, sizes)
sizeFormat.adapter = sizeAdapter
sizeFormat.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
selectedSize = sizesVal[p2]
}

override fun onNothingSelected(p0: AdapterView<*>?) {
selectedSize = "b"
}
}

val statusAdapter = ArrayAdapter.createFromResource(this, R.array.status_array, R.layout.spinner_layout)
statusSpinner.adapter = statusAdapter
statusSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
s = status[p2]
}

override fun onNothingSelected(p0: AdapterView<*>?) {
s = ""
}
}

filterButton.setOnClickListener {
presenter.subscribe(this)
presenter.loadData(
c,
filterQuery.text.toString(),
maxNumber.text.toString(),
s,
toDate.text.toString(),
fromDate.text.toString(),
fromSize.text.toString(),
toSize.text.toString(),
selectedSize
)
}

filterFab.setOnClickListener {
when (filterLayout.visibility) {
View.VISIBLE -> filterLayout.visibility = View.GONE
else -> filterLayout.visibility = View.VISIBLE
}
}

}

override fun onSupportNavigateUp(): Boolean {
Expand All @@ -60,7 +124,17 @@ class SearchActivity : BaseActivity(), SearchTorrentListView {
override fun onQueryTextSubmit(query: String?): Boolean {
query?.let {
presenter.subscribe(this@SearchActivity)
presenter.loadData(null, null, null, null, null, null, null, null, null)
presenter.loadData(
null,
query,
null,
null,
null,
null,
null,
null,
null
)
searchView.clearFocus()
}
return true
Expand Down Expand Up @@ -95,4 +169,5 @@ class SearchActivity : BaseActivity(), SearchTorrentListView {
super.onDestroy()
presenter.unsubscribe()
}

}
103 changes: 50 additions & 53 deletions app/src/main/java/cat/pantsu/nyaapantsu/ui/activity/TorrentActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,25 @@ package cat.pantsu.nyaapantsu.ui.activity


import android.annotation.SuppressLint
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.support.design.widget.Snackbar
import android.text.Html
import android.text.Spanned
import android.text.TextUtils
import android.util.Log
import android.widget.Toast
import cat.pantsu.nyaapantsu.R
import cat.pantsu.nyaapantsu.base.BaseActivity
import cat.pantsu.nyaapantsu.mvp.model.TorrentModel
import cat.pantsu.nyaapantsu.mvp.presenter.TorrentPresenter
import cat.pantsu.nyaapantsu.mvp.view.TorrentView
import cat.pantsu.nyaapantsu.util.download
import cat.pantsu.nyaapantsu.util.formatDate
import kotlinx.android.synthetic.main.content_torrent_view.*
import kotlinx.android.synthetic.main.toolbar.*
import org.jetbrains.anko.contentView
Expand All @@ -22,8 +31,6 @@ class TorrentActivity : BaseActivity(), TorrentView {
@Inject
lateinit var presenter: TorrentPresenter

// private var showDet = false

companion object {
const val EXTRA_TORRENT_ID = "EXTRA_TORRENT_ID"
const val EXTRA_TORRENT_TITLE = "EXTRA_TORRENT_TITLE"
Expand Down Expand Up @@ -63,59 +70,49 @@ class TorrentActivity : BaseActivity(), TorrentView {
@SuppressLint("SetTextI18n")
override fun onItemsLoaded(items: TorrentModel) {
torrentName.text = items.name
// torrentCategory.text = resources.getStringArray(R.array.cat_array)[items.category.toInt()]
// torrentUser.text = items.uploaderName
// torrentHash.text = items.hash
// torrentDate.text = items.date.formatDate()
// torrentSize.text = items.filesize.toString()
// val spanned: Spanned = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
// Html.fromHtml(items.description, Html.FROM_HTML_MODE_COMPACT)
// } else {
// Html.fromHtml(items.description)
// }
//
// torrentDescription.text = spanned
// torrentDownloads.text = items.completed.toString()
// torrentWebsite.text = items.websiteLink
// torrentSeeders.text = "S: ${items.seeders}"
// torrentLeechers.text = "L: ${items.leechers}"
// torrentLastScraped.text = items.lastScrape
// torrentFiles.text = items.fileList.toString()
//
torrentCategory.text = resources.getStringArray(R.array.cat_array)[items.category.toInt()]
torrentUser.text = items.uploaderName
torrentHash.text = items.hash
torrentDate.text = items.date.formatDate()
torrentSize.text = items.filesize.toString()
val spanned: Spanned = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Html.fromHtml(items.description, Html.FROM_HTML_MODE_COMPACT)
} else {
Html.fromHtml(items.description)
}

torrentDescription.text = spanned
torrentDownloads.text = items.completed.toString()
torrentWebsite.text = items.websiteLink
torrentSeeders.text = "S: ${items.seeders}"
torrentLeechers.text = "L: ${items.leechers}"
torrentLastScraped.text = items.lastScrape
torrentFiles.text = items.fileList.toString()

// when (items.status) {
// 2 -> torrentName.backgroundColor = ContextCompat.getColor(this, R.color.colorRemake)
// 3 -> torrentName.backgroundColor = ContextCompat.getColor(this, R.color.colorTrusted)
// 4 -> torrentName.backgroundColor = ContextCompat.getColor(this, R.color.colorAPlus)
// }
// torrentProgress.max = (items.seeders + items.leechers)
// torrentProgress.progress = items.seeders
//
// torrentDetails.visibility = View.VISIBLE
//
// downloadButton.setOnClickListener { _ ->
// if (!TextUtils.isEmpty(items.torrent)) {
// download(this, downloadButton, items.torrent, items.name)
// } else {
// toast(getString(R.string.torrent_not_available))
// }
// }
//
// copyButton.setOnClickListener { _ ->
// val clipboard = this.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
// val clipData = ClipData.newPlainText(items.name, items.magnet)
// clipboard.primaryClip = clipData
// toast(getString(R.string.magnet_copied))
// }
//
// showMoreToggle.setOnClickListener { _ ->
// if (showDet) {
// moreDetails.visibility = View.GONE
// showDet = false
// } else {
// moreDetails.visibility = View.VISIBLE
// showDet = true
// }
// 2 -> torrentName.setBackgroundColor(ContextCompat.getColor(this, R.color.colorRemake))
// 3 -> torrentName.setBackgroundColor(ContextCompat.getColor(this, R.color.colorTrusted))
// 4 -> torrentName.setBackgroundColor(ContextCompat.getColor(this, R.color.colorAPlus))
// }

torrentProgress.max = (items.seeders + items.leechers)
torrentProgress.progress = items.seeders

downloadButton.setOnClickListener {
if (!TextUtils.isEmpty(items.torrent)) {
download(this, downloadButton, items.torrent, items.name)
} else {
Toast.makeText(this, R.string.torrent_not_available, Toast.LENGTH_SHORT).show()
}
}

copyButton.setOnClickListener {
val clipboard = this.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val clipData = ClipData.newPlainText(items.name, items.magnet)
clipboard.primaryClip = clipData
Toast.makeText(this, R.string.magnet_copied, Toast.LENGTH_SHORT).show()
}

}

override fun onError(e: Throwable?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ import cat.pantsu.nyaapantsu.R
import cat.pantsu.nyaapantsu.base.BaseFragment


class RecentFragment : BaseFragment() {
class FavoritesFragment : BaseFragment() {

companion object {
fun newInstance(): RecentFragment {
return RecentFragment()
fun newInstance(): FavoritesFragment {
return FavoritesFragment()
}
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
activity!!.title = getString(R.string.recent)
activity!!.title = getString(R.string.favorites)

return inflater.inflate(R.layout.fragment_recent, container, false)
return inflater.inflate(R.layout.fragment_favorites, container, false)
}


Expand Down
Loading

0 comments on commit f452d89

Please sign in to comment.