Skip to content

Commit

Permalink
save user token
Browse files Browse the repository at this point in the history
  • Loading branch information
fashare2015 committed Sep 12, 2017
1 parent 90e3209 commit 3ad198b
Show file tree
Hide file tree
Showing 17 changed files with 284 additions and 21 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ dependencies {
compile project(":adapter")
compile project(":databinding")
compile project(":base-ui")
compile project(":util")

/******* support 包 ****************************************************************************/
compile "com.android.support:support-v4:$rootProject.ext.supportLibraryVersion"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<uses-permission android:name="android.permission.INTERNET"/>

<application
android:name=".JueJinApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
Expand Down
7 changes: 6 additions & 1 deletion app/src/main/kotlin/com/fashare/mvvm_juejin/JueJinApp.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.fashare.mvvm_juejin

import android.app.Application
import android.content.Context

/**
* <pre>
Expand All @@ -9,9 +10,13 @@ import android.app.Application
* desc :
* </pre>
*/
class JueJinApp: Application(){
class JueJinApp: Application() {
companion object {
lateinit var instance: Context
}

override fun onCreate() {
super.onCreate()
instance = this
}
}
85 changes: 67 additions & 18 deletions app/src/main/kotlin/com/fashare/mvvm_juejin/model/user/UserBean.kt
Original file line number Diff line number Diff line change
@@ -1,31 +1,80 @@
package com.fashare.mvvm_juejin.model.user

/**
* role : editor
* username : 梁山boy
* selfDescription : 擅长面向基佬编程
* email :
* mobilePhoneNumber : 18818276018
* jobTitle : Android
* company : 上海
* avatarHd : https://avatars.githubusercontent.com/u/15076541?v=3
* avatarLarge : https://avatars.githubusercontent.com/u/15076541?v=3
* blogAddress : http://blog.csdn.net/a153614131
* deviceType : android
* editorType : markdown
* allowNotification : false
* emailVerified : false
* mobilePhoneVerified : false
* isAuthor : true
* isUnitedAuthor : false
* blacklist : false
* followeesCount : 116
* followersCount : 39
* postedPostsCount : 5
* postedEntriesCount : 5
* collectedEntriesCount : 238
* viewedEntriesCount : 1832
* subscribedTagsCount : 18
* totalCollectionsCount : 1349
* totalViewsCount : 35326
* totalCommentsCount : 73
* latestLoginedInAt : 2017-09-12T10:55:48.674Z
* createdAt : 2016-08-24T04:43:32.001Z
* updatedAt : 2017-09-12T12:10:05.860Z
* collectionSetCount : 28
* useLeancloudPwd : false
* objectId : 57bd25f4a34131005b211b84
* uid : 57bd25f4a34131005b211b84
*/
/**
* 用户信息
*/
class UserBean {
/**
* weibo : {"uid":"5885816355","nickname":"墨镜猫jacky"}
* wechat : {"nickname":"Android王世昌","uid":"oDv1Eww5c17raaESRVI8l9M6zsuE","expires_at":"1468246472191"}
* github : {"nickname":"Jacky Wang","uid":"17797018","expires_at":"1468239341180"}
*/

var community: CommunityBean? = null
var collectedEntriesCount: Int = 0
var role: String? = null
var username: String? = null
var selfDescription: String? = null
var email: String? = null
var mobilePhoneNumber: String? = null
var jobTitle: String? = null
var company: String? = null
var followersCount: Int = 0
var avatarHd: String? = null
var avatarLarge: String? = null
var blogAddress: String? = null
var deviceType: String? = null
var editorType: String? = null
var allowNotification: Boolean = false
var emailVerified: Boolean = false
var mobilePhoneVerified: Boolean = false
var isAuthor: Boolean = false
var isUnitedAuthor: Boolean = false
var blacklist: Boolean = false
var followeesCount: Int = 0
var role: String? = null
var followersCount: Int = 0
var postedPostsCount: Int = 0
var isAuthor: Boolean = false
var postedEntriesCount: Int = 0
var totalCommentsCount: Int = 0
var ngxCachedTime: Int = 0
var ngxCached: Boolean = false
var collectedEntriesCount: Int = 0
var viewedEntriesCount: Int = 0
var jobTitle: String? = null
var subscribedTagsCount: Int = 0
var totalCollectionsCount: Int = 0
var username: String? = null
var avatarLarge: String? = null
var totalViewsCount: Int = 0
var totalCommentsCount: Int = 0
var latestLoginedInAt: String? = null
var createdAt: String? = null
var updatedAt: String? = null
var collectionSetCount: Int = 0
var useLeancloudPwd: Boolean = false
var objectId: String? = null
var uid: String? = null

class CommunityBean {
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.fashare.mvvm_juejin.repo.local

import com.fashare.mvvm_juejin.JueJinApp
import com.fashare.mvvm_juejin.model.user.UserBean
import com.saike.library.util.PreferencesUtil

/**
* Created by apple on 2017/9/12.
*/
object LocalUser{
val appContext by lazy { JueJinApp.instance }

var user: UserBean? = null
var userToken: UserBean.TokenBean? = null
set(value) {
field = value

if(field != null) {
// 保存 token 到本地
PreferencesUtil.getInstance(appContext)
.putEntity("userToken", field!!)
}else{
// 清除本地 token
PreferencesUtil.getInstance(appContext)
.remove("userToken")
}
}
get() {
return if(field != null) field else
PreferencesUtil.getInstance(appContext)
.getEntity("userToken", UserBean.TokenBean::class.java)
}


}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.fashare.mvvm_juejin.view.home

import android.databinding.DataBindingUtil
import android.databinding.ObservableField
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.fashare.base_ui.BaseFragment
import com.fashare.mvvm_juejin.R
import com.fashare.mvvm_juejin.databinding.FragmentProfileBinding
import com.fashare.mvvm_juejin.repo.local.LocalUser
import com.fashare.mvvm_juejin.viewmodel.ProfileVM

/**
Expand All @@ -18,9 +20,16 @@ import com.fashare.mvvm_juejin.viewmodel.ProfileVM
</pre> *
*/
class ProfileFragment : BaseFragment(){
lateinit var binding: FragmentProfileBinding
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return DataBindingUtil.inflate<FragmentProfileBinding>(inflater, R.layout.fragment_profile, container, false).apply {
binding = this
this.profileVM = ProfileVM()
}.root
}

override fun onResume() {
super.onResume()
binding.profileVM.userToken.set(LocalUser.userToken?.token?: null)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.databinding.ObservableField
import android.view.View
import com.fashare.mvvm_juejin.repo.Composers
import com.fashare.mvvm_juejin.repo.JueJinApis
import com.fashare.mvvm_juejin.repo.local.LocalUser
import com.fashare.net.ApiFactory

/**
Expand All @@ -26,6 +27,7 @@ class LoginVM(){
}.toMap())
.compose(Composers.compose())
.subscribe({
LocalUser.userToken = it
(view.context as Activity).finish()
}, {})
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.fashare.mvvm_juejin.viewmodel

import android.content.Intent
import android.databinding.ObservableField
import android.view.View
import com.fashare.mvvm_juejin.view.profile.login.LoginActivity

Expand All @@ -12,6 +13,8 @@ import com.fashare.mvvm_juejin.view.profile.login.LoginActivity
* </pre>
*/
class ProfileVM(){
var userToken = ObservableField<String>(null)

val toLogin = View.OnClickListener{
it.context.startActivity(Intent(it.context, LoginActivity:: class.java))
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/fragment_profile.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录/注册"
android:text='@{profileVM.userToken!=null? profileVM.userToken: "登录/注册"}'
android:textSize="20sp"
android:textColor="@color/profile_title"/>

Expand Down
1 change: 1 addition & 0 deletions ext.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ project.ext{
okhttp3Version = "3.8.0"
retrofitVersion = "2.3.0"
retrofit2rxjava2Version = "1.0.0"
gsonVersion = "2.7"
klogVersion = "1.6.0"

leakcanaryVersion = "1.5.1"
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1 +1 @@
include ':app', ':net', ':databinding', ':adapter', ':imageloader', ':base-ui'
include ':app', ':net', ':databinding', ':adapter', ':imageloader', ':base-ui', ':util'
1 change: 1 addition & 0 deletions util/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
5 changes: 5 additions & 0 deletions util/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
apply plugin: 'com.android.library'

dependencies {
compile "com.google.code.gson:gson:$rootProject.ext.gsonVersion"
}
17 changes: 17 additions & 0 deletions util/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /Users/apple/Library/Android/sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
12 changes: 12 additions & 0 deletions util/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.fashare.util">

<application
android:allowBackup="true"
android:label="@string/app_name"
android:supportsRtl="true"
>

</application>

</manifest>
Loading

0 comments on commit 3ad198b

Please sign in to comment.