diff --git a/.idea/misc.xml b/.idea/misc.xml index 3386629..17426a4 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -29,7 +29,7 @@ - + diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 50acd89..c376c4c 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -11,22 +11,21 @@ android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> - - + - - + - - + - - + + + diff --git a/app/src/main/java/com/example/wijaya_pc/footballapps/adapter/SearchMatchAdapter.kt b/app/src/main/java/com/example/wijaya_pc/footballapps/adapter/SearchMatchAdapter.kt new file mode 100644 index 0000000..6ec1ae1 --- /dev/null +++ b/app/src/main/java/com/example/wijaya_pc/footballapps/adapter/SearchMatchAdapter.kt @@ -0,0 +1,2 @@ +package com.example.wijaya_pc.footballapps.adapter + diff --git a/app/src/main/java/com/example/wijaya_pc/footballapps/api/TheSportDBApi.kt b/app/src/main/java/com/example/wijaya_pc/footballapps/api/TheSportDBApi.kt index 815b389..510ff3a 100644 --- a/app/src/main/java/com/example/wijaya_pc/footballapps/api/TheSportDBApi.kt +++ b/app/src/main/java/com/example/wijaya_pc/footballapps/api/TheSportDBApi.kt @@ -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 + } } \ No newline at end of file diff --git a/app/src/main/java/com/example/wijaya_pc/footballapps/feature/main/MainActivity.kt b/app/src/main/java/com/example/wijaya_pc/footballapps/feature/main/MainActivity.kt index 2118e1b..417ab2f 100644 --- a/app/src/main/java/com/example/wijaya_pc/footballapps/feature/main/MainActivity.kt +++ b/app/src/main/java/com/example/wijaya_pc/footballapps/feature/main/MainActivity.kt @@ -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.* @@ -40,7 +40,8 @@ class MainActivity : AppCompatActivity() { .beginTransaction() .replace( R.id.main_container, - MatchFragment(), MatchFragment::class.java.simpleName) + MatchFragment(), MatchFragment::class.java.simpleName + ) .commit() } @@ -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() } } diff --git a/app/src/main/java/com/example/wijaya_pc/footballapps/feature/match/MatchFragment.kt b/app/src/main/java/com/example/wijaya_pc/footballapps/feature/match/MatchFragment.kt index d8fac1b..c417ff2 100644 --- a/app/src/main/java/com/example/wijaya_pc/footballapps/feature/match/MatchFragment.kt +++ b/app/src/main/java/com/example/wijaya_pc/footballapps/feature/match/MatchFragment.kt @@ -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 @@ -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() + true + } + else -> super.onOptionsItemSelected(item) + } + + + } } \ No newline at end of file diff --git a/app/src/main/java/com/example/wijaya_pc/footballapps/feature/match/SearchMatchActivity.kt b/app/src/main/java/com/example/wijaya_pc/footballapps/feature/match/SearchMatchActivity.kt new file mode 100644 index 0000000..4ce868b --- /dev/null +++ b/app/src/main/java/com/example/wijaya_pc/footballapps/feature/match/SearchMatchActivity.kt @@ -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 = 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( + "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) { + matches.clear() + matches.addAll(data) + matchAdapter.notifyDataSetChanged() + } + +} diff --git a/app/src/main/java/com/example/wijaya_pc/footballapps/feature/team/TeamsFragment.kt b/app/src/main/java/com/example/wijaya_pc/footballapps/feature/team/TeamsFragment.kt index 8d0149b..221ef2c 100644 --- a/app/src/main/java/com/example/wijaya_pc/footballapps/feature/team/TeamsFragment.kt +++ b/app/src/main/java/com/example/wijaya_pc/footballapps/feature/team/TeamsFragment.kt @@ -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 @@ -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, TeamView { @@ -40,6 +40,8 @@ class TeamsFragment : Fragment(), AnkoComponent, TeamView { private lateinit var leagueName : String + private lateinit var searchView : SearchView + override fun onActivityCreated(savedInstanceState: Bundle?) { super.onActivityCreated(savedInstanceState) @@ -109,6 +111,30 @@ class TeamsFragment : Fragment(), AnkoComponent, 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() } diff --git a/app/src/main/java/com/example/wijaya_pc/footballapps/model/MatchResponse.kt b/app/src/main/java/com/example/wijaya_pc/footballapps/model/MatchResponse.kt index 9537671..58cec9d 100644 --- a/app/src/main/java/com/example/wijaya_pc/footballapps/model/MatchResponse.kt +++ b/app/src/main/java/com/example/wijaya_pc/footballapps/model/MatchResponse.kt @@ -2,4 +2,8 @@ package com.example.wijaya_pc.footballapps.model data class MatchResponse( val events: List +) + +data class SearchMatchResponse( + val event: List ) \ No newline at end of file diff --git a/app/src/main/java/com/example/wijaya_pc/footballapps/presenter/SearchMatchPresenter.kt b/app/src/main/java/com/example/wijaya_pc/footballapps/presenter/SearchMatchPresenter.kt new file mode 100644 index 0000000..469654a --- /dev/null +++ b/app/src/main/java/com/example/wijaya_pc/footballapps/presenter/SearchMatchPresenter.kt @@ -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() + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/wijaya_pc/footballapps/view/SearchMatchView.kt b/app/src/main/java/com/example/wijaya_pc/footballapps/view/SearchMatchView.kt new file mode 100644 index 0000000..e59a9e9 --- /dev/null +++ b/app/src/main/java/com/example/wijaya_pc/footballapps/view/SearchMatchView.kt @@ -0,0 +1,9 @@ +package com.example.wijaya_pc.footballapps.view + +import com.example.wijaya_pc.footballapps.model.Match + +interface SearchMatchView { + fun showLoading() + fun hideLoading() + fun showSearchMatchList(data: List) +} \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_loupe.xml b/app/src/main/res/drawable/ic_loupe.xml new file mode 100644 index 0000000..736d3f3 --- /dev/null +++ b/app/src/main/res/drawable/ic_loupe.xml @@ -0,0 +1,7 @@ + + + + + + diff --git a/app/src/main/res/layout/activity_search_match.xml b/app/src/main/res/layout/activity_search_match.xml new file mode 100644 index 0000000..fa0d4a6 --- /dev/null +++ b/app/src/main/res/layout/activity_search_match.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_match.xml b/app/src/main/res/layout/fragment_match.xml index ddc24f0..0d7adec 100644 --- a/app/src/main/res/layout/fragment_match.xml +++ b/app/src/main/res/layout/fragment_match.xml @@ -5,7 +5,6 @@ android:layout_width="match_parent" android:layout_height="match_parent"> - + + + + + \ No newline at end of file diff --git a/app/src/main/res/menu/search_menu.xml b/app/src/main/res/menu/search_menu.xml new file mode 100644 index 0000000..3bdeb82 --- /dev/null +++ b/app/src/main/res/menu/search_menu.xml @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 53a1d32..e37dc0b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -33,5 +33,6 @@ Team Weight (KG) Height (m) + Search diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index f2a3c31..01fc3eb 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -12,6 +12,7 @@ false true +