diff --git a/.gitignore b/.gitignore index c29684d..a6572cd 100644 --- a/.gitignore +++ b/.gitignore @@ -66,7 +66,7 @@ gen/ # Gradle files .gradle/ -build/ +retrofit/ # Local configuration file (sdk path, etc) local.properties @@ -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 \ No newline at end of file +*.jks + +build/ diff --git a/app/.gitignore b/app/.gitignore index 796b96d..f8e8e4c 100644 --- a/app/.gitignore +++ b/app/.gitignore @@ -1 +1,2 @@ +/retrofit /build diff --git a/app/build.gradle b/app/build.gradle index fe42587..7e3b828 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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' diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro index f1b4245..045f0db 100644 --- a/app/proguard-rules.pro +++ b/app/proguard-rules.pro @@ -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 @@ -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.* ; +} +# Ignore annotation used for retrofit tooling. +-dontwarn org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement + +-dontwarn okio.** +-dontwarn javax.annotation.** +-keepclasseswithmembers class * { + @com.squareup.moshi.* ; +} +-keep @com.squareup.moshi.JsonQualifier interface * \ No newline at end of file diff --git a/app/src/main/java/info/czekanski/bet/domain/home/HomeFragment.kt b/app/src/main/java/info/czekanski/bet/domain/home/HomeFragment.kt index 49d262a..724d729 100644 --- a/app/src/main/java/info/czekanski/bet/domain/home/HomeFragment.kt +++ b/app/src/main/java/info/czekanski/bet/domain/home/HomeFragment.kt @@ -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 @@ -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) } @@ -32,13 +39,32 @@ class HomeFragment : Fragment() { .collection("matches") .addSnapshotListener { querySnapshot, _ -> if (querySnapshot?.documents == null) return@addSnapshotListener - val matches: List = querySnapshot.documents.filterNotNull().map { it.toObject(Match::class.java)!! }.map { MatchCell(it) } + val matches: List = 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) + }) } }) } diff --git a/app/src/main/java/info/czekanski/bet/model/Match.kt b/app/src/main/java/info/czekanski/bet/model/Match.kt index 8238020..381859d 100644 --- a/app/src/main/java/info/czekanski/bet/model/Match.kt +++ b/app/src/main/java/info/czekanski/bet/model/Match.kt @@ -6,6 +6,7 @@ import java.util.* @Parcelize data class Match( + val id: String = "", val team1: Team = "", val team2: Team = "", val date: Date = Date(), diff --git a/app/src/main/java/info/czekanski/bet/network/BetApi.kt b/app/src/main/java/info/czekanski/bet/network/BetApi.kt new file mode 100644 index 0000000..6169064 --- /dev/null +++ b/app/src/main/java/info/czekanski/bet/network/BetApi.kt @@ -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 + + @PUT("/api/bet/{betId}") + fun placeBet(@Path("betId") betId: String, + @Body bet: Bet, + @Header("Authorization") token: String): Completable +} \ No newline at end of file diff --git a/app/src/main/java/info/czekanski/bet/network/BetService.kt b/app/src/main/java/info/czekanski/bet/network/BetService.kt new file mode 100644 index 0000000..e15ab45 --- /dev/null +++ b/app/src/main/java/info/czekanski/bet/network/BetService.kt @@ -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) + } + } +} \ No newline at end of file diff --git a/app/src/main/java/info/czekanski/bet/network/model/Bet.kt b/app/src/main/java/info/czekanski/bet/network/model/Bet.kt new file mode 100644 index 0000000..beff916 --- /dev/null +++ b/app/src/main/java/info/czekanski/bet/network/model/Bet.kt @@ -0,0 +1,8 @@ +package info.czekanski.bet.network.model + +import java.util.* + +data class Bet( + val bid: Int?, + val score: String +) \ No newline at end of file diff --git a/app/src/main/java/info/czekanski/bet/network/model/ReturnId.kt b/app/src/main/java/info/czekanski/bet/network/model/ReturnId.kt new file mode 100644 index 0000000..37b5b16 --- /dev/null +++ b/app/src/main/java/info/czekanski/bet/network/model/ReturnId.kt @@ -0,0 +1,5 @@ +package info.czekanski.bet.network.model + +data class ReturnId( + val id: String +) \ No newline at end of file diff --git a/gradle.properties b/gradle.properties index 743d692..0cff883 100644 --- a/gradle.properties +++ b/gradle.properties @@ -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. diff --git a/gradlew b/gradlew index cccdd3d..0c18919 100644 --- a/gradlew +++ b/gradlew @@ -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