Skip to content

Commit

Permalink
add launcher name variation based on build types
Browse files Browse the repository at this point in the history
fetch user name after authorization
  • Loading branch information
aJIEw committed Dec 10, 2021
1 parent 488b906 commit 86b404a
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 56 deletions.
9 changes: 7 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ android {
applicationId "me.ajiew.jithub"
minSdk 21
targetSdk 31
versionCode 1
versionName "1.0.0"
versionCode 2
versionName "1.0.1"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

buildConfigField "String", "GITHUB_CLIENT_ID", (project.findProperty("githubClientId") ?: "")
Expand All @@ -38,10 +38,15 @@ android {
}
}
buildTypes {
debug {
applicationIdSuffix ".dev"
manifestPlaceholders = [APP_NAME : 'Jithub Dev']
}
release {
signingConfig signingConfigs.release
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
manifestPlaceholders = [APP_NAME : 'Jithub']
}
}
compileOptions {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
android:name=".JithubApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:label="${APP_NAME}"
android:supportsRtl="true"
android:theme="@style/Theme.Jithub"
tools:ignore="LockedOrientationActivity">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package me.ajiew.jithub.data.service.interceptor

import me.ajiew.jithub.data.model.UserProfile
import okhttp3.Interceptor
import okhttp3.Request
import okhttp3.Response
import java.net.SocketTimeoutException

/**
*
Expand All @@ -13,13 +13,18 @@ import okhttp3.Response
class AuthHeaderInterceptor : Interceptor {

override fun intercept(chain: Interceptor.Chain): Response {
val accessToken = UserProfile.accessToken
val request: Request = chain.request()
.newBuilder()
.addHeader("Authorization", "Bearer $accessToken")
.build()
try {
val accessToken = UserProfile.accessToken
val request = chain.request()
.newBuilder()
.addHeader("Authorization", "Bearer $accessToken")
.build()
return chain.proceed(request)
} catch (exception: SocketTimeoutException) {
exception.printStackTrace()
}

return chain.proceed(request)
return Response.Builder().build()
}
}

Expand Down
16 changes: 4 additions & 12 deletions app/src/main/java/me/ajiew/jithub/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.google.android.material.bottomnavigation.BottomNavigationView
import dagger.hilt.android.AndroidEntryPoint
import me.ajiew.core.base.BaseActivity
import me.ajiew.core.util.SPUtils
import me.ajiew.core.util.setLightStatusBar
import me.ajiew.jithub.BR
import me.ajiew.jithub.R
import me.ajiew.jithub.common.Constants
Expand Down Expand Up @@ -42,29 +43,18 @@ class MainActivity : BaseActivity<ActivityMainBinding, MainViewModel>() {
super.initData()

currentIndex = if (hasLoggedIn) 0 else 1
if (currentIndex == 0) {
viewModel.getUserName()
}
}

override fun initView() {
super.initView()

setLightStatusBar()
setLightStatusBar(this)

initFragment()

setupBottomNavigationBar()
}

private fun setLightStatusBar() {
val root = findViewById<View>(android.R.id.content).rootView

window.navigationBarColor = ContextCompat.getColor(this, android.R.color.background_dark)
window.statusBarColor = Color.TRANSPARENT
WindowInsetsControllerCompat(window, root).isAppearanceLightStatusBars = true
}

private fun initFragment() {
mainFragments = ArrayList<Fragment>()
mainFragments.add(HomeFragment())
Expand Down Expand Up @@ -152,6 +142,8 @@ class MainActivity : BaseActivity<ActivityMainBinding, MainViewModel>() {

intent?.data.apply {
if (this?.toString()?.startsWith(Constants.GITHUB_OAUTH_REDIRECT_URL) == true) {
setLightStatusBar(this@MainActivity)

val code = getQueryParameter("code")
if (!TextUtils.isEmpty(code)) {
viewModel.getAccessToken(code!!)
Expand Down
36 changes: 13 additions & 23 deletions app/src/main/java/me/ajiew/jithub/ui/viewmodel/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import me.ajiew.core.base.viewmodel.BaseViewModel
import me.ajiew.core.data.Results
import me.ajiew.jithub.BuildConfig
import me.ajiew.jithub.data.repository.UserRepository
import timber.log.Timber
import javax.inject.Inject

/**
Expand All @@ -17,7 +18,8 @@ import javax.inject.Inject
* Created on: 2021/11/3 15:22
*/
@HiltViewModel
class MainViewModel @Inject constructor(private val repository: UserRepository) : BaseViewModel<UserRepository>() {
class MainViewModel @Inject constructor(private val repository: UserRepository) :
BaseViewModel<UserRepository>() {

private var accessToken = ""

Expand All @@ -40,43 +42,31 @@ class MainViewModel @Inject constructor(private val repository: UserRepository)
repository.saveAccessToken(accessToken)

if (BuildConfig.DEBUG) {
ToastUtils.show(accessToken)
Timber.d(accessToken)
}

getUserName()

UIState.Success(results.data, "Get Access Token Success")
}
is Results.Error -> UIState.Error(null, results.message)
}
}
}

fun getUserName() {
private suspend fun getUserName() {
val userName = repository.getUserName()
if (userName.isEmpty()) {
fetchUserFeeds()
}
}

private fun fetchUserFeeds() {
uiState.value = UIState.Loading

viewModelScope.launch {
val results = repository.requestUserFeeds()
uiState.value = when (results) {
is Results.Success -> {
val feedsTemplate = results.data
val userUrl = feedsTemplate.current_user_public_url
if (userUrl != null) {
val name = userUrl.substring(userUrl.lastIndexOf("/") + 1)
repository.saveUserName(name)
}

UIState.Success(results.data, "Load Success")
if (results is Results.Success) {
val feedsTemplate = results.data
val userUrl = feedsTemplate.current_user_public_url
if (userUrl != null) {
val name = userUrl.substring(userUrl.lastIndexOf("/") + 1)
repository.saveUserName(name)
}
is Results.Error -> UIState.Error(null, results.message)
}
}
}


}
13 changes: 2 additions & 11 deletions core/src/main/java/me/ajiew/core/base/ContainerActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import androidx.fragment.app.FragmentTransaction
import dagger.hilt.android.AndroidEntryPoint
import me.ajiew.core.R
import me.ajiew.core.base.viewmodel.BaseViewModel.Companion.LIGHT_STATUS_BAR
import me.ajiew.core.util.setLightStatusBar
import java.lang.ref.WeakReference

/**
Expand Down Expand Up @@ -44,16 +45,6 @@ open class ContainerActivity : AppCompatActivity() {
trans.commitAllowingStateLoss()
}

private fun setLightStatusBar() {
val root = findViewById<View>(android.R.id.content).rootView

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.navigationBarColor = ContextCompat.getColor(this, android.R.color.background_dark)
window.statusBarColor = Color.TRANSPARENT
}
WindowInsetsControllerCompat(window, root).isAppearanceLightStatusBars = true
}

private fun initFromIntent(data: Intent?): Fragment {
if (data == null) {
throw RuntimeException(
Expand All @@ -70,7 +61,7 @@ open class ContainerActivity : AppCompatActivity() {
fragment.arguments = args

if (args.getBoolean(LIGHT_STATUS_BAR)) {
setLightStatusBar()
setLightStatusBar(this)
}
}
return fragment
Expand Down
25 changes: 25 additions & 0 deletions core/src/main/java/me/ajiew/core/util/ActivityUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package me.ajiew.core.util

import android.app.Activity
import android.graphics.Color
import android.os.Build
import android.view.View
import androidx.core.content.ContextCompat
import androidx.core.view.WindowInsetsControllerCompat

/**
*
* @author aJIEw
* Created on: 2021/12/10 15:35
*/
fun setLightStatusBar(activity: Activity) {
activity.apply {
val root = findViewById<View>(android.R.id.content).rootView

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.navigationBarColor = ContextCompat.getColor(this, android.R.color.background_dark)
window.statusBarColor = Color.TRANSPARENT
}
WindowInsetsControllerCompat(window, root).isAppearanceLightStatusBars = true
}
}

0 comments on commit 86b404a

Please sign in to comment.