Skip to content

Commit

Permalink
- menambah activity untuk search match
Browse files Browse the repository at this point in the history
  • Loading branch information
andremw96 committed Nov 1, 2018
1 parent 56b36af commit c827a27
Show file tree
Hide file tree
Showing 18 changed files with 318 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 7 additions & 8 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,21 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">

<activity android:name="com.example.wijaya_pc.footballapps.feature.main.MainActivity">
<activity android:name=".feature.main.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

<activity android:name="com.example.wijaya_pc.footballapps.feature.match.DetailMatchActivity">
<activity android:name=".feature.match.DetailMatchActivity">
</activity>

<activity android:name="com.example.wijaya_pc.footballapps.feature.team.DetailTeamActivity">
<activity android:name=".feature.team.DetailTeamActivity">
</activity>

<activity android:name="com.example.wijaya_pc.footballapps.feature.player.DetailPlayerActivity">
<activity android:name=".feature.player.DetailPlayerActivity">
</activity>
<activity android:name=".feature.match.SearchMatchActivity"
android:theme="@style/AppTheme.NoActionBar">
</activity>
</application>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package com.example.wijaya_pc.footballapps.adapter

Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,8 @@ object TheSportDBApi {
fun getPlayerDetails(playerId: String?) : String {
return BuildConfig.BASE_URL + "api/v1/json/${BuildConfig.TSDB_API_KEY}" + "/lookupplayer.php?id=" + playerId
}

fun getSearchMatch(matchName: String?) : String {
return BuildConfig.BASE_URL + "api/v1/json/${BuildConfig.TSDB_API_KEY}" + "/searchevents.php?e=" + matchName
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package com.example.wijaya_pc.footballapps.feature.main

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import com.example.wijaya_pc.footballapps.feature.match.MatchFragment
import com.example.wijaya_pc.footballapps.R
import com.example.wijaya_pc.footballapps.R.id.*
import com.example.wijaya_pc.footballapps.feature.favoritematch.FavoriteFragment
import com.example.wijaya_pc.footballapps.feature.match.MatchFragment
import com.example.wijaya_pc.footballapps.feature.team.TeamsFragment
import kotlinx.android.synthetic.main.activity_main.*

Expand Down Expand Up @@ -40,7 +40,8 @@ class MainActivity : AppCompatActivity() {
.beginTransaction()
.replace(
R.id.main_container,
MatchFragment(), MatchFragment::class.java.simpleName)
MatchFragment(), MatchFragment::class.java.simpleName
)
.commit()
}

Expand All @@ -49,17 +50,19 @@ class MainActivity : AppCompatActivity() {
.beginTransaction()
.replace(
R.id.main_container,
TeamsFragment(), TeamsFragment::class.java.simpleName)
TeamsFragment(), TeamsFragment::class.java.simpleName
)
.commit()
}

private fun loadFavoritesFragment() {
supportFragmentManager
.beginTransaction()
.replace(
R.id.main_container,
FavoriteFragment(), FavoriteFragment::class.java.simpleName)
.commit()
supportFragmentManager
.beginTransaction()
.replace(
R.id.main_container,
FavoriteFragment(), FavoriteFragment::class.java.simpleName
)
.commit()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,36 @@ import android.os.Bundle
import android.support.design.widget.TabLayout
import android.support.v4.app.Fragment
import android.support.v4.view.ViewPager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.support.v7.widget.SearchView
import android.support.v7.widget.Toolbar
import android.view.*
import android.widget.Button
import com.example.wijaya_pc.footballapps.R
import com.example.wijaya_pc.footballapps.R.id.match_button_search
import com.example.wijaya_pc.footballapps.adapter.pager.MatchPagerAdapter
import org.jetbrains.anko.find
import org.jetbrains.anko.sdk25.coroutines.onClick
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.support.v4.ctx
import org.jetbrains.anko.support.v4.toast
import org.jetbrains.anko.toast

class MatchFragment : Fragment() {

private lateinit var viewPager : ViewPager
private lateinit var tabs : TabLayout

private lateinit var searchButton : Button

private lateinit var mMatchPagerAdapter: MatchPagerAdapter

private var menuItem: Menu? = null

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val rootView = inflater.inflate(R.layout.fragment_match, container, false)

setHasOptionsMenu(true)

viewPager = rootView.find(R.id.viewPagerContainer) as ViewPager
tabs = rootView.find(R.id.tabs) as TabLayout

Expand All @@ -32,4 +45,24 @@ class MatchFragment : Fragment() {

return rootView
}

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
super.onCreateOptionsMenu(menu, inflater)

inflater.inflate(R.menu.match_fragment_menu, menu)
menuItem = menu

}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when(item.itemId) {
match_button_search -> {
ctx.startActivity<SearchMatchActivity>()
true
}
else -> super.onOptionsItemSelected(item)
}


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package com.example.wijaya_pc.footballapps.feature.match

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.SearchView
import android.support.v7.widget.Toolbar
import android.view.Menu
import android.view.MenuItem
import android.widget.ProgressBar
import com.example.wijaya_pc.footballapps.R
import com.example.wijaya_pc.footballapps.R.menu.search_menu
import com.example.wijaya_pc.footballapps.adapter.MatchAdapter
import com.example.wijaya_pc.footballapps.api.ApiRepository
import com.example.wijaya_pc.footballapps.invisible
import com.example.wijaya_pc.footballapps.model.Match
import com.example.wijaya_pc.footballapps.presenter.MatchPresenter
import com.example.wijaya_pc.footballapps.presenter.SearchMatchPresenter
import com.example.wijaya_pc.footballapps.view.SearchMatchView
import com.example.wijaya_pc.footballapps.visible
import com.google.gson.Gson
import org.jetbrains.anko.ctx
import org.jetbrains.anko.find
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.support.v4.onRefresh
import org.jetbrains.anko.toast

class SearchMatchActivity : AppCompatActivity(), SearchMatchView {
private lateinit var toolbar: Toolbar

private lateinit var recyclerView: RecyclerView
private lateinit var searchView : SearchView
private lateinit var progressBar : ProgressBar

private var matches: MutableList<Match> = mutableListOf()

private lateinit var matchAdapter: MatchAdapter

private lateinit var presenter: SearchMatchPresenter

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_search_match)

toolbar = find(R.id.toolbar_search)
recyclerView = find(R.id.rv_search)
progressBar = find(R.id.progress_bar_search)

setSupportActionBar(toolbar)
supportActionBar?.title = "Search Match"
supportActionBar?.setDisplayHomeAsUpEnabled(true)

val request = ApiRepository()
val gson = Gson()
presenter = SearchMatchPresenter(this, request, gson)

recyclerView.layoutManager = LinearLayoutManager(ctx)
matchAdapter = MatchAdapter(matches) {
ctx.startActivity<DetailMatchActivity>(
"id" to "${it.matchId}",
"homeTeamId" to "${it.idHomeTeam}",
"awayTeamId" to "${it.idAwayTeam}"
)
}
recyclerView.adapter = matchAdapter

}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(search_menu, menu)

val searchMenu = menu.findItem(R.id.action_search)
searchMenu.expandActionView()
searchView = searchMenu.actionView as SearchView
searchView.queryHint = "Search"

searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?): Boolean {
presenter.getSearchMatch(query)

return false
}

override fun onQueryTextChange(newText: String?): Boolean {
return false
}

})

return true
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
android.R.id.home -> {
finish()
true
}
else -> super.onOptionsItemSelected(item)
}
}

override fun showLoading() {
progressBar.visible()
}

override fun hideLoading() {
progressBar.invisible()
}

override fun showSearchMatchList(data: List<Match>) {
matches.clear()
matches.addAll(data)
matchAdapter.notifyDataSetChanged()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import android.support.v4.app.Fragment
import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.support.v7.widget.SearchView
import android.view.*
import android.widget.*
import com.example.wijaya_pc.footballapps.R.array.league
import com.example.wijaya_pc.footballapps.R.color.colorAccent
Expand All @@ -26,6 +25,7 @@ import org.jetbrains.anko.recyclerview.v7.recyclerView
import org.jetbrains.anko.support.v4.ctx
import org.jetbrains.anko.support.v4.onRefresh
import org.jetbrains.anko.support.v4.swipeRefreshLayout
import org.jetbrains.anko.support.v4.toast

class TeamsFragment : Fragment(), AnkoComponent<Context>, TeamView {

Expand All @@ -40,6 +40,8 @@ class TeamsFragment : Fragment(), AnkoComponent<Context>, TeamView {

private lateinit var leagueName : String

private lateinit var searchView : SearchView

override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)

Expand Down Expand Up @@ -109,6 +111,30 @@ class TeamsFragment : Fragment(), AnkoComponent<Context>, TeamView {
}
}

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
super.onCreateOptionsMenu(menu, inflater)

inflater.inflate(com.example.wijaya_pc.footballapps.R.menu.search_menu, menu)

val searchMenu = menu.findItem(com.example.wijaya_pc.footballapps.R.id.action_search)
searchView = searchMenu.actionView as SearchView
searchView.queryHint = "Search"

searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {

override fun onQueryTextSubmit(query: String?): Boolean {
toast("$query")

return false
}

override fun onQueryTextChange(newText: String?): Boolean {
return false
}

})
}

override fun showLoading() {
progressBar.visible()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ package com.example.wijaya_pc.footballapps.model

data class MatchResponse(
val events: List<Match>
)

data class SearchMatchResponse(
val event: List<Match>
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.example.wijaya_pc.footballapps.presenter

import com.example.wijaya_pc.footballapps.api.ApiRepository
import com.example.wijaya_pc.footballapps.api.TheSportDBApi
import com.example.wijaya_pc.footballapps.coroutine.CoroutineContextProvider
import com.example.wijaya_pc.footballapps.model.MatchResponse
import com.example.wijaya_pc.footballapps.model.SearchMatchResponse
import com.example.wijaya_pc.footballapps.view.SearchMatchView
import com.google.gson.Gson
import kotlinx.coroutines.experimental.async
import org.jetbrains.anko.coroutines.experimental.bg

class SearchMatchPresenter(
private val view: SearchMatchView,
private val apiRepository: ApiRepository,
private val gson: Gson,
private val context: CoroutineContextProvider = CoroutineContextProvider()
) {
fun getSearchMatch(matchName: String?) {
view.showLoading()

async(context.main) {
val data = bg {
gson.fromJson(
apiRepository.doRequest(TheSportDBApi.getSearchMatch(matchName)),
SearchMatchResponse::class.java
)
}
view.showSearchMatchList(data.await().event)
view.hideLoading()
}
}
}
Loading

0 comments on commit c827a27

Please sign in to comment.