Skip to content

Commit

Permalink
added communication with backend
Browse files Browse the repository at this point in the history
  • Loading branch information
JaCzekanski committed Jun 11, 2018
1 parent 60df7e8 commit 9ac6cd8
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 8 deletions.
8 changes: 5 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ gen/

# Gradle files
.gradle/
build/
retrofit/

# Local configuration file (sdk path, etc)
local.properties
Expand Down Expand Up @@ -130,7 +130,9 @@ atlassian-ide-plugin.xml
# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
crashlytics-retrofit.properties

# Keystore
*.jks
*.jks

build/
1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/retrofit
/build
9 changes: 9 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ dependencies {
implementation "android.arch.navigation:navigation-fragment-ktx:1.0.0-alpha02"
implementation "android.arch.navigation:navigation-ui-ktx:1.0.0-alpha02"

implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0'
implementation 'com.squareup.retrofit2:converter-moshi:2.4.0'
implementation "io.reactivex.rxjava2:rxjava:2.1.14"
implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
implementation'io.reactivex.rxjava2:rxkotlin:2.2.0'

implementation 'com.squareup.moshi:moshi:1.6.0'

testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
Expand Down
18 changes: 17 additions & 1 deletion app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
# proguardFiles setting in retrofit.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
Expand All @@ -19,3 +19,19 @@
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile

# Retain generic type information for use by reflection by converters and adapters.
-keepattributes Signature
# Retain service method parameters.
-keepclassmembernames,allowobfuscation interface * {
@retrofit2.http.* <methods>;
}
# Ignore annotation used for retrofit tooling.
-dontwarn org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement

-dontwarn okio.**
-dontwarn javax.annotation.**
-keepclasseswithmembers class * {
@com.squareup.moshi.* <methods>;
}
-keep @com.squareup.moshi.JsonQualifier interface *
30 changes: 28 additions & 2 deletions app/src/main/java/info/czekanski/bet/domain/home/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package info.czekanski.bet.domain.home

import android.os.Bundle
import android.support.v4.app.Fragment
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.navigation.fragment.findNavController
import com.google.firebase.firestore.FirebaseFirestore
import info.czekanski.bet.R
Expand All @@ -15,9 +17,14 @@ import info.czekanski.bet.domain.home.cells.WelcomeCell
import info.czekanski.bet.domain.home.utils.ListDecorator
import info.czekanski.bet.domain.match.bundleOf
import info.czekanski.bet.model.Match
import info.czekanski.bet.network.BetService
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.rxkotlin.subscribeBy
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.fragment_home.*

class HomeFragment : Fragment() {
val betService: BetService by lazy { BetService.instance }
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_home, container, false)
}
Expand All @@ -32,13 +39,32 @@ class HomeFragment : Fragment() {
.collection("matches")
.addSnapshotListener { querySnapshot, _ ->
if (querySnapshot?.documents == null) return@addSnapshotListener
val matches: List<MatchCell> = querySnapshot.documents.filterNotNull().map { it.toObject(Match::class.java)!! }.map { MatchCell(it) }
val matches: List<MatchCell> = querySnapshot.documents
.filterNotNull()
.map { it.toObject(Match::class.java)!!.copy(id = it.id) }
.map { MatchCell(it) }

recyclerView.adapter = MatchesAdapter(listOf(
WelcomeCell("Krachtan"),
HeaderCell("Najbliższe mecze")
) + matches, {
if (it is MatchCell) {
goToMatchView(it.match)
if (it.match.id.isEmpty()) {
Toast.makeText(context, "Invalid match id!", Toast.LENGTH_SHORT).show()
return@MatchesAdapter
}

// Create bet and go to it
betService.api.createBet(it.match.id, "XDXDXDXDXD")
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy(onSuccess = { result ->
result.id // TODO: USE ME!!!
goToMatchView(it.match)
}, onError = {
Toast.makeText(context, "Unable to create bet!", Toast.LENGTH_SHORT).show()
Log.w("CreateBet", it)
})
}
})
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/info/czekanski/bet/model/Match.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import java.util.*

@Parcelize
data class Match(
val id: String = "",
val team1: Team = "",
val team2: Team = "",
val date: Date = Date(),
Expand Down
19 changes: 19 additions & 0 deletions app/src/main/java/info/czekanski/bet/network/BetApi.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package info.czekanski.bet.network

import info.czekanski.bet.network.model.Bet
import info.czekanski.bet.network.model.ReturnId
import io.reactivex.Completable
import io.reactivex.Single
import retrofit2.http.*

interface BetApi {

@POST("/api/bet/{matchId}")
fun createBet(@Path("matchId") matchId: String,
@Header("Authorization") token: String): Single<ReturnId>

@PUT("/api/bet/{betId}")
fun placeBet(@Path("betId") betId: String,
@Body bet: Bet,
@Header("Authorization") token: String): Completable
}
22 changes: 22 additions & 0 deletions app/src/main/java/info/czekanski/bet/network/BetService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package info.czekanski.bet.network

import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.moshi.MoshiConverterFactory

class BetService(val api: BetApi) {

companion object {
val instance by lazy {
val retrofit = Retrofit.Builder()
.baseUrl("https://bet.czekanski.info/")
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(MoshiConverterFactory.create())
.build()

val api = retrofit.create(BetApi::class.java)

return@lazy BetService(api)
}
}
}
8 changes: 8 additions & 0 deletions app/src/main/java/info/czekanski/bet/network/model/Bet.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package info.czekanski.bet.network.model

import java.util.*

data class Bet(
val bid: Int?,
val score: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package info.czekanski.bet.network.model

data class ReturnId(
val id: String
)
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# For more details on how to configure your retrofit environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
Expand Down
2 changes: 1 addition & 1 deletion gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ if $cygwin ; then
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
JAVACMD=`cygpath --unix "$JAVACMD"`

# We build the pattern for arguments to be converted via cygpath
# We retrofit the pattern for arguments to be converted via cygpath
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
SEP=""
for dir in $ROOTDIRSRAW ; do
Expand Down

0 comments on commit 9ac6cd8

Please sign in to comment.