Skip to content

Commit

Permalink
refactor: switch to MMKV and mini program of WeChat
Browse files Browse the repository at this point in the history
  • Loading branch information
bqliang committed Feb 24, 2022
1 parent d7e9dab commit cc3ab86
Show file tree
Hide file tree
Showing 23 changed files with 114 additions and 118 deletions.
6 changes: 4 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,17 @@ android {
}

dependencies {
//MMKV
implementation 'com.tencent:mmkv:1.2.11'
// OKHttp
implementation("com.squareup.okhttp3:okhttp:4.9.2")
implementation("com.squareup.okhttp3:okhttp:4.9.3")
// UltimateBarX
implementation 'com.gitee.zackratos:UltimateBarX:0.7.1'
// Welcome Pages
implementation 'com.github.AppIntro:AppIntro:6.1.0'
// 彩云 json 解析
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.1'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2'
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.3.1"
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.google.code.gson:gson:2.8.8'
Expand Down
1 change: 0 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
<queries>
<package android:name="com.eg.android.AlipayGphone" />
<package android:name="com.tencent.mm" />
<package android:name="com.unionpay" />
<package android:name="com.taobao.taobao" />
<package android:name="com.cainiao.wireless" />
<package android:name="com.yek.android.kfc.activitys"/>
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/java/com/bqliang/nfushortcuts/Test.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.bqliang.nfushortcuts


fun main() {

}
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
package com.bqliang.nfushortcuts.activity

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import androidx.fragment.app.Fragment
import com.bqliang.nfushortcuts.R
import com.bqliang.nfushortcuts.tools.SharedPreferencesUtil
import com.bqliang.nfushortcuts.tools.MyApplication
import com.bqliang.nfushortcuts.tools.mmkv
import com.github.appintro.AppIntro2
import com.github.appintro.AppIntroFragment
import com.zackratos.ultimatebarx.ultimatebarx.navigationBar
import com.zackratos.ultimatebarx.ultimatebarx.statusBar
import kotlin.concurrent.thread

class IntroActivity : AppIntro2() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

// 迁移:SharedPreferences -> MMKV
thread {
val sp = MyApplication.context
.getSharedPreferences("app_data", Context.MODE_PRIVATE)
mmkv.importFromSharedPreferences(sp)
sp.edit().clear().commit()
}

isColorTransitionsEnabled = true
showStatusBar(true)
statusBar { transparent() }
Expand Down Expand Up @@ -72,8 +84,9 @@ class IntroActivity : AppIntro2() {
}

private fun close(){
SharedPreferencesUtil.saveBoolean("isFirstStart", false)
startActivity(Intent(this, MainActivity::class.java))
mmkv.encode("isFirstStart", false)
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
finish()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import androidx.appcompat.app.AppCompatActivity
import com.bqliang.nfushortcuts.R
import com.bqliang.nfushortcuts.databinding.ActivityLibraryCardBinding
import com.bqliang.nfushortcuts.dialog.IDSettingAlertDialog
import com.bqliang.nfushortcuts.tools.SharedPreferencesUtil
import com.bqliang.nfushortcuts.tools.mmkv
import com.bumptech.glide.Glide
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.android.material.snackbar.Snackbar
Expand Down Expand Up @@ -36,7 +36,7 @@ class LibraryCardActivity : AppCompatActivity() {
fab = binding.settingFab

thread {
id = SharedPreferencesUtil.getString("id",null)
id = mmkv.decodeString("id",null)
if (id.isNullOrBlank()){
Snackbar.make(fab, R.string.tooltip_configure_id, Snackbar.LENGTH_SHORT)
.setAction(R.string.ok){ IDSettingAlertDialog(this) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import com.bqliang.nfushortcuts.model.Weather
import com.bqliang.nfushortcuts.model.WeatherViewModel
import com.bqliang.nfushortcuts.model.getSky
import com.bqliang.nfushortcuts.tools.ClipboardUtil
import com.bqliang.nfushortcuts.tools.SharedPreferencesUtil
import com.bqliang.nfushortcuts.tools.mmkv
import com.bqliang.nfushortcuts.view.MyItemDecoration
import com.google.android.material.appbar.MaterialToolbar
import com.google.android.material.bottomsheet.BottomSheetDialog
Expand Down Expand Up @@ -73,9 +73,9 @@ class MainActivity : AppCompatActivity() {
spanCount.observe(this@MainActivity){
setRecyclerView()
setMenuIcon()
SharedPreferencesUtil.saveInt("span_count", it)
mmkv.encode("span_count", it)
}
spanCount.value = SharedPreferencesUtil.getInt("span_count", 2)
spanCount.value = mmkv.decodeInt("span_count", 2)

show()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,21 @@ import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.bqliang.nfushortcuts.listener.MyDistributeListener
import com.bqliang.nfushortcuts.tools.SharedPreferencesUtil
import com.bqliang.nfushortcuts.tools.mmkv
import com.microsoft.appcenter.AppCenter
import com.microsoft.appcenter.analytics.Analytics
import com.microsoft.appcenter.crashes.Crashes
import com.microsoft.appcenter.distribute.Distribute
import com.tencent.mmkv.MMKV


class NavigationActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val intent = Intent()
val isFirstStart = SharedPreferencesUtil.getBoolean("isFirstStart", true)

if (isFirstStart) intent.setClass(this, IntroActivity::class.java)
else intent.setClass(this, MainActivity::class.java)

startActivity(intent)
val isFirstStart = mmkv.decodeBool("isFirstStart", true)
val activityClass = if(isFirstStart) IntroActivity::class.java else MainActivity::class.java
Intent(this, activityClass).let(::startActivity)

AppCenter.start(
application, "f3e1fad6-dc60-4b23-8101-b7c98999bfdb",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ class HealthCodeActivity : AppCompatActivity() {
val intent = Intent(Intent.ACTION_VIEW)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK

if (AppUtil.isAppInstalled(DependentApp.UNIONPAY)){
intent.data = Uri.parse("upwallet://applet?toLink=https://ywtb.fpsd.unionpay.com/pub/jmas/jmasbucket/jmopen_files/unzip/4c94e5e6d269477983c5992bfd97d95e/gdykm/index.html#&encryptAppId=bb2b4babcbfd06bc")
if (AppUtil.isAppInstalled(DependentApp.WECHAT)){
//intent.data = Uri.parse("upwallet://applet?toLink=https://ywtb.fpsd.unionpay.com/pub/jmas/jmasbucket/jmopen_files/unzip/4c94e5e6d269477983c5992bfd97d95e/gdykm/index.html#&encryptAppId=bb2b4babcbfd06bc")
intent.data = Uri.parse("weixin://dl/business/?t=QDZVQEO2z9f")
startActivity(intent)
finish()
}else{
MaterialAlertDialogBuilder(this)
.setTitle(R.string.not_yet_install_unionpay)
.setMessage(R.string.download_unionpay_dialog_message)
.setTitle(R.string.not_yet_install_wechat)
.setMessage(R.string.download_wechat_dialog_message)
.setPositiveButton(R.string.ok){ dialog, _ ->
intent.data = Uri.parse("market://details?id=${DependentApp.UNIONPAY}")
startActivity(intent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import android.app.Activity
import android.view.LayoutInflater
import android.widget.TextView
import com.bqliang.nfushortcuts.R
import com.bqliang.nfushortcuts.tools.SharedPreferencesUtil
import com.bqliang.nfushortcuts.tools.mmkv
import com.bqliang.nfushortcuts.tools.showToast
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.microsoft.appcenter.analytics.Analytics
Expand All @@ -15,15 +15,15 @@ class CaptivePortalSettingAlertDialog(activity: Activity) {
val views = LayoutInflater.from(activity).inflate(R.layout.id_pw_setting, null)
val id = views.findViewById<TextView>(R.id.id_input)
val password = views.findViewById<TextView>(R.id.pw_input)
id.text = SharedPreferencesUtil.getString("id","")
password.text = SharedPreferencesUtil.getString("password", "")
id.text = mmkv.decodeString("id","")
password.text = mmkv.decodeString("password", "")

MaterialAlertDialogBuilder(activity).apply {
setTitle(R.string.please_enter_id_pwd)
setView(views)
setPositiveButton(R.string.save) { _, _ ->
SharedPreferencesUtil.saveString("id", id.text.toString())
SharedPreferencesUtil.saveString("password", password.text.toString())
mmkv.encode("id", id.text.toString())
mmkv.encode("password", password.text.toString())
R.string.save_successfully.showToast()
Analytics.trackEvent("Save Account Info")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.bqliang.nfushortcuts.R
import com.bqliang.nfushortcuts.activity.LibraryCardActivity
import com.bqliang.nfushortcuts.tools.SharedPreferencesUtil
import com.bqliang.nfushortcuts.tools.mmkv
import com.bqliang.nfushortcuts.tools.showSnackBar
import com.google.android.material.dialog.MaterialAlertDialogBuilder

Expand All @@ -22,14 +22,14 @@ class IDSettingAlertDialog(activity: AppCompatActivity) {
val views = LayoutInflater.from(activity).inflate(R.layout.library_card_setting, null)
val idInput = views.findViewById<TextView>(R.id.id_input)

idInput.text = SharedPreferencesUtil.getString("id", "")
idInput.text = mmkv.decodeString("id", "")

MaterialAlertDialogBuilder(activity).apply {
setTitle(R.string.configure_id)
setView(views)
setPositiveButton(R.string.save){ _, _ ->
val id = idInput.text.toString()
SharedPreferencesUtil.saveString("id", id)
mmkv.encode("id", id)
(activity as LibraryCardActivity).createBarCode(id)
activity.getString(R.string.save_successfully).showSnackBar(activity.fab)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import android.view.LayoutInflater
import androidx.appcompat.app.AppCompatActivity
import com.bqliang.nfushortcuts.R
import com.bqliang.nfushortcuts.tools.MyApplication
import com.bqliang.nfushortcuts.tools.SharedPreferencesUtil
import com.bqliang.nfushortcuts.tools.mmkv
import com.bqliang.nfushortcuts.tools.showToast
import com.google.android.material.checkbox.MaterialCheckBox
import com.google.android.material.dialog.MaterialAlertDialogBuilder
Expand All @@ -16,7 +16,7 @@ private const val KEY = "if_do_not_remind"

fun tipsForPinnedShortcutAlertDialog(activity: AppCompatActivity){

var ifDoNotRemind = SharedPreferencesUtil.getBoolean(KEY, false)
var ifDoNotRemind = mmkv.decodeBool(KEY, false)

if (!ifDoNotRemind){
val view = LayoutInflater.from(activity).inflate(R.layout.do_not_remind_again_layout, null)
Expand All @@ -30,14 +30,14 @@ fun tipsForPinnedShortcutAlertDialog(activity: AppCompatActivity){
.setMessage(R.string.tips_of_grant_permission)
.setView(view)
.setPositiveButton(R.string.go_to_setting){ _, _ ->
SharedPreferencesUtil.saveBoolean(KEY, ifDoNotRemind)
mmkv.encode(KEY, ifDoNotRemind)
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
val uri = Uri.fromParts("package", MyApplication.context.packageName, null)
intent.data = uri
activity.startActivity(intent)
}
.setNegativeButton(R.string.already_know){ _, _ ->
SharedPreferencesUtil.saveBoolean(KEY, ifDoNotRemind)
mmkv.encode(KEY, ifDoNotRemind)
}
.show()
}else R.string.tooltip_create_pinned_shortcut.showToast()
Expand Down
34 changes: 16 additions & 18 deletions app/src/main/java/com/bqliang/nfushortcuts/service/MyService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import android.net.Network
import android.net.NetworkCapabilities
import android.net.NetworkRequest
import android.os.IBinder
import android.util.Log
import android.widget.Toast
import androidx.core.app.NotificationCompat
import com.bqliang.nfushortcuts.R
import com.bqliang.nfushortcuts.tools.SharedPreferencesUtil
import com.bqliang.nfushortcuts.tools.loginWIFI
import com.bqliang.nfushortcuts.tools.mmkv
import com.bqliang.nfushortcuts.tools.showToast
import kotlin.concurrent.thread

Expand All @@ -27,10 +28,11 @@ class MyService : Service() {
override fun onCreate() {
super.onCreate()
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val channel = NotificationChannel("foreground_service", "前台服务通知", NotificationManager.IMPORTANCE_MIN)
val channel = NotificationChannel("foreground_service", "前台服务通知", NotificationManager.IMPORTANCE_LOW)
notificationManager.createNotificationChannel(channel)

val notification = NotificationCompat.Builder(this, "foreground_service")
.setSmallIcon(R.drawable.small_notification_icon_login)
.setContentTitle("Title")
.setContentText("Text")
.build()
Expand All @@ -45,26 +47,22 @@ class MyService : Service() {
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
.build()

connectivityManager.requestNetwork(
request,
object: ConnectivityManager.NetworkCallback(){
override fun onAvailable(network: Network) {
val callback = object : ConnectivityManager.NetworkCallback(){

override fun onAvailable(network: Network) {
super.onAvailable(network)
connectivityManager.bindProcessToNetwork(network)
val id = SharedPreferencesUtil.getString("id", null)
val password = SharedPreferencesUtil.getString("password", null)
if ( id.isNullOrBlank() || password.isNullOrBlank())
R.string.error_id_or_pw.showToast(Toast.LENGTH_LONG)
else loginWIFI(id, password)
loginWIFI()
connectivityManager.unregisterNetworkCallback(this)
}
}

override fun onUnavailable() {
super.onUnavailable()
R.string.login_unavailable.showToast(Toast.LENGTH_LONG)
}
}

override fun onUnavailable() {
super.onUnavailable()
R.string.login_unavailable.showToast(Toast.LENGTH_LONG)
}
},
2000)
connectivityManager.requestNetwork(request, callback, 2000)
stopSelf()
}
return super.onStartCommand(intent, flags, startId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.bqliang.nfushortcuts.tools
import android.annotation.SuppressLint
import android.app.Application
import android.content.Context
import com.tencent.mmkv.MMKV

class MyApplication:Application() {

Expand All @@ -13,6 +14,7 @@ class MyApplication:Application() {

override fun onCreate() {
super.onCreate()
MMKV.initialize(this)
context = applicationContext
}
}
Loading

0 comments on commit cc3ab86

Please sign in to comment.