Skip to content
This repository has been archived by the owner on Aug 4, 2019. It is now read-only.

Commit

Permalink
Create app module for DI , closed #19 , retrofit configuration , closed
Browse files Browse the repository at this point in the history
#14 , create user repository  , closed #13 , create verification code viewmodel , clsoed #11 , create verification code view , closed #10
  • Loading branch information
miladsalimiiii committed Jul 24, 2019
1 parent 9bc6909 commit 8d284c5
Show file tree
Hide file tree
Showing 23 changed files with 346 additions and 59 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ dependencies {
implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0'

implementation 'com.github.EhsanMashhadi:helpdroid:0.9.0'
implementation 'com.github.franmontiel:PersistentCookieJar:v1.0.1'

implementation ('com.alimuzaffar.lib:pinentryedittext:2.0.6') {
exclude group: 'androidx.appcompat', module: 'appcompat'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package de.netalic.peacock

import androidx.test.InstrumentationRegistry
import androidx.test.runner.AndroidJUnit4

import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
Expand Down
14 changes: 9 additions & 5 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.netalic.peacock">

<uses-permission android:name="android.permission.INTERNET"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
android:theme="@style/AppTheme"
android:name="de.netalic.MyApp">
<activity android:name=".MainActivity"
android:windowSoftInputMode="adjustPan">

<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
Expand Down
38 changes: 38 additions & 0 deletions app/src/main/java/de/netalic/MyApp.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package de.netalic

import android.app.Application
import de.netalic.peacock.di.apiModule
import de.netalic.peacock.di.repositoryModule
import de.netalic.peacock.di.viewModelModule
import org.koin.android.ext.koin.androidContext
import org.koin.android.ext.koin.androidLogger
import org.koin.core.context.startKoin

class MyApp : Application() {


override fun onCreate() {
super.onCreate()

instance = this

startKoin {

androidLogger()
androidContext(this@MyApp)

modules(
listOf(
repositoryModule,
apiModule,
viewModelModule
)
)
}
}

companion object {

lateinit var instance: Application
}
}
7 changes: 5 additions & 2 deletions app/src/main/java/de/netalic/peacock/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package de.netalic.peacock

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import de.netalic.peacock.ui.registeration.codeverification.CodeVerificationFragment

class MainActivity : AppCompatActivity() {

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

supportFragmentManager.beginTransaction().add(R.id.frameLayout_mainActivity_fragmentContainer, CodeVerificationFragment()).commit()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ data class MyResponse<T>(
}

enum class Status {

LOADING,
SUCCESS,
FAILED
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/de/netalic/peacock/data/model/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ data class User( val phone:String,
val deviceName:String,
val deviceType:String,
val firebaseRegistrationId:String,
val actionCode:Int
val actionCode:String
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package de.netalic.peacock.data.repository
import de.netalic.peacock.data.model.User
import de.netalic.peacock.data.webservice.InterfaceApi
import io.reactivex.Single
import okhttp3.ResponseBody
import retrofit2.Response


class UserRepository(private val usrApi:InterfaceApi){


fun bind(user:User) :Single<Long>{
fun bind(user:User) :Single<Response<ResponseBody>>{

return usrApi.bind(user.phone,user.udid,user.deviceName,user.deviceType,user.firebaseRegistrationId,
user.actionCode)
Expand Down
81 changes: 73 additions & 8 deletions app/src/main/java/de/netalic/peacock/data/webservice/ApiClient.kt
Original file line number Diff line number Diff line change
@@ -1,22 +1,87 @@
package de.netalic.peacock.data.webservice


import com.franmontiel.persistentcookiejar.PersistentCookieJar
import com.franmontiel.persistentcookiejar.cache.SetCookieCache
import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor
import de.netalic.MyApp
import nuesoft.helpdroid.network.SharedPreferencesJwtPersistor
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.Response
import okhttp3.ResponseBody
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import java.io.IOException

class ApiClient {

companion object{

fun getClient():InterfaceApi{
companion object {
private var sRetrofit: Retrofit? = null
private var sApi: InterfaceApi? = null


private fun getClient(): Retrofit {

if (sRetrofit == null) {

val okHttpClient = OkHttpClient().newBuilder()
val cookieJar = PersistentCookieJar(SetCookieCache(), SharedPrefsCookiePersistor(MyApp.instance))
okHttpClient.cookieJar(cookieJar).addInterceptor(AuthorizationInterceptor())

val retrofit=Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl("https://nightly-alpha.carrene.com/apiv1/")
.build()
return retrofit.create(InterfaceApi::class.java)
sRetrofit = Retrofit.Builder().baseUrl("https://nightly-alpha.carrene.com/apiv1/")
.client(okHttpClient.build())
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build()
}
return sRetrofit!!
}


fun getService(): InterfaceApi? {

if (sApi == null) {
sApi = getClient().create(InterfaceApi::class.java)
}
return sApi
}
}

private class AuthorizationInterceptor : Interceptor {

internal var sharedPreferencesJwtPersistor = SharedPreferencesJwtPersistor(MyApp.instance)

@Throws(IOException::class)
override fun intercept(chain: Interceptor.Chain): Response {

val token = sharedPreferencesJwtPersistor.get()
var request = chain.request()

if (token != null) {
request = request.newBuilder().addHeader("Authorization", "Bearer $token").build()
}

var response = chain.proceed(request)

if (response.request().method() == "BIND" && response.code() == 200) {

val responseBodyString = response.body()!!.string()
val contentType = response.body()!!.contentType()
val body = ResponseBody.create(contentType, responseBodyString)
response = response.newBuilder().body(body).build()
}

val newJwtToken = response.header("X-New-JWT-Token")

if (newJwtToken != null) {
sharedPreferencesJwtPersistor.save(newJwtToken)
}

return response
}
}

}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package de.netalic.peacock.data.webservice

import io.reactivex.Single
import okhttp3.ResponseBody
import retrofit2.Response
import retrofit2.http.Field
import retrofit2.http.FormUrlEncoded
import retrofit2.http.HTTP
Expand All @@ -12,5 +14,5 @@ interface InterfaceApi {
fun bind(@Field("phone") phone:String , @Field("udid") udid:String ,
@Field("deviceName") deviceName:String,@Field("deviceType") deviceType:String ,
@Field("firebaseRegistrationId") firebaseRegistrationId:String,
@Field("activationCode") activationCode:Int):Single<Long>
@Field("activationCode") activationCode:String):Single<Response<ResponseBody>>
}
32 changes: 32 additions & 0 deletions app/src/main/java/de/netalic/peacock/di/AppModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package de.netalic.peacock.di

import de.netalic.peacock.data.repository.UserRepository
import de.netalic.peacock.data.webservice.ApiClient
import de.netalic.peacock.ui.registeration.codeverification.CodeVerificationViewModel
import org.koin.android.viewmodel.dsl.viewModel
import org.koin.dsl.module

val repositoryModule = module {

single {

UserRepository(get())
}
}
val viewModelModule = module {

viewModel {

CodeVerificationViewModel(get())
}
}

val apiModule= module {

single {

ApiClient.getService()

}
}

5 changes: 2 additions & 3 deletions app/src/main/java/de/netalic/peacock/ui/base/BaseFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment

abstract class BaseFragment :Fragment(){
abstract class BaseFragment : Fragment() {

abstract fun initUiListener()

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

initUiListener()
}

open abstract fun initUiListener()

}
Loading

0 comments on commit 8d284c5

Please sign in to comment.