Commit 14a115b3 authored by jiangjiantao's avatar jiangjiantao

dev

parent 7a78f29f
...@@ -37,7 +37,7 @@ android { ...@@ -37,7 +37,7 @@ android {
dependencies { dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs') implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" // implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.1' implementation 'androidx.core:core-ktx:1.3.1'
implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1' implementation 'com.google.android.material:material:1.2.1'
...@@ -47,6 +47,7 @@ dependencies { ...@@ -47,6 +47,7 @@ dependencies {
implementation("com.squareup.okhttp3:okhttp:4.9.3") implementation("com.squareup.okhttp3:okhttp:4.9.3")
implementation("com.squareup.okhttp3:logging-interceptor:3.9.0") implementation("com.squareup.okhttp3:logging-interceptor:3.9.0")
implementation("com.squareup.retrofit2:retrofit:2.9.0") implementation("com.squareup.retrofit2:retrofit:2.9.0")
implementation("com.google.android.material:material:1.2.0")
implementation 'androidx.annotation:annotation:1.1.0' implementation 'androidx.annotation:annotation:1.1.0'
testImplementation 'junit:junit:4.+' testImplementation 'junit:junit:4.+'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0' implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
......
package com.miya.fastcashier.dialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.constraintlayout.widget.Group;
import com.miya.fastcashier.R;
import com.miya.fastcashier.utils.ScanGunKeyEventHelper;
public class CodeScanDialog extends Dialog implements DialogInterface.OnDismissListener {
private final ScanGunKeyEventHelper scanGunKeyEventHelper;
private OnScanListener listener;
public CodeScanDialog(Context context) {
super(context);
initWindow();
setContentView(R.layout.dialog_code_scan);
setCanceledOnTouchOutside(false);
setOnDismissListener(this);
scanGunKeyEventHelper = new ScanGunKeyEventHelper();
scanGunKeyEventHelper.setOnBarCodeCatchListener(barcode -> {
if (listener != null) {
listener.onScan(barcode);
}
});
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
scanGunKeyEventHelper.analysisKeyEvent(event);
return true;
}
private void initWindow() {
Window win = this.getWindow();
win.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams lp = win.getAttributes();
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
lp.gravity = Gravity.BOTTOM;
lp.windowAnimations = R.style.BottomInAndOutStyle;
win.setAttributes(lp);
win.setBackgroundDrawableResource(android.R.color.transparent);
}
public void setOnScanListener(OnScanListener listener) {
this.listener = listener;
}
@Override
public void onDismiss(DialogInterface dialogInterface) {
if (scanGunKeyEventHelper != null) {
scanGunKeyEventHelper.onDestroy();
}
}
public interface OnScanListener {
void onScan(String result);
}
}
...@@ -4,18 +4,22 @@ import androidx.appcompat.app.AppCompatActivity ...@@ -4,18 +4,22 @@ import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle import android.os.Bundle
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
import com.miya.fastcashier.R import com.miya.fastcashier.R
import com.miya.fastcashier.viewmodel.MainViewModel import com.miya.fastcashier.dialog.CodeScanDialog
import com.miya.fastcashier.viewmodel.PayViewModel
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {
private lateinit var viewModel: MainViewModel private lateinit var viewModel: PayViewModel
private lateinit var scanDialog: CodeScanDialog
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity) setContentView(R.layout.activity_main)
viewModel = ViewModelProvider(this).get(MainViewModel::class.java) viewModel = ViewModelProvider(this).get(PayViewModel::class.java)
scanDialog = CodeScanDialog(this);
scanDialog.setOnScanListener {
viewModel.
}
} }
} }
\ No newline at end of file
package com.miya.fastcashier.utils;
import android.os.Handler;
import android.text.TextUtils;
import android.view.KeyEvent;
/**
* 扫码枪解码器
* Created by fengyu on 2018/1/12.
*/
public class ScanGunKeyEventHelper {
//延迟500ms,判断扫码是否完成。
private final static long MESSAGE_DELAY = 500;
//扫码内容
private final StringBuffer mStringBufferResult = new StringBuffer();
//大小写区分
private boolean mCaps;
private OnScanSuccessListener mOnScanSuccessListener;
private final Handler mHandler = new Handler();
private final Runnable mScanningFishedRunnable = new Runnable() {
@Override
public void run() {
performScanSuccess();
}
};
//返回扫描结果
private void performScanSuccess() {
String barcode = mStringBufferResult.toString();
if (mOnScanSuccessListener != null && !TextUtils.isEmpty(barcode))
mOnScanSuccessListener.onScanSuccess(barcode);
mStringBufferResult.setLength(0);
}
//key事件处理
public void analysisKeyEvent(KeyEvent event) {
int keyCode = event.getKeyCode();
//字母大小写判断
checkLetterStatus(event);
if (event.getAction() == KeyEvent.ACTION_DOWN) {
char aChar = getInputCode(event);
if (keyCode == KeyEvent.KEYCODE_ENTER) {
//若为回车键,直接返回
mHandler.removeCallbacks(mScanningFishedRunnable);
mHandler.post(mScanningFishedRunnable);
} else {
if (aChar != 0) {
mStringBufferResult.append(aChar);
}
//延迟post,若500ms内,有其他事件
mHandler.removeCallbacks(mScanningFishedRunnable);
mHandler.postDelayed(mScanningFishedRunnable, MESSAGE_DELAY);
}
}
}
//检查shift键
private void checkLetterStatus(KeyEvent event) {
int keyCode = event.getKeyCode();
if (keyCode == KeyEvent.KEYCODE_SHIFT_RIGHT || keyCode == KeyEvent.KEYCODE_SHIFT_LEFT) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
//按着shift键,表示大写
mCaps = true;
} else {
//松开shift键,表示小写
mCaps = false;
}
}
}
//获取扫描内容
private char getInputCode(KeyEvent event) {
int keyCode = event.getKeyCode();
char aChar;
if (keyCode >= KeyEvent.KEYCODE_A && keyCode <= KeyEvent.KEYCODE_Z) {
//字母
aChar = (char) ((mCaps ? 'A' : 'a') + keyCode - KeyEvent.KEYCODE_A);
} else if (keyCode >= KeyEvent.KEYCODE_0 && keyCode <= KeyEvent.KEYCODE_9) {
//数字
aChar = (char) ('0' + keyCode - KeyEvent.KEYCODE_0);
} else {
//其他符号
switch (keyCode) {
case KeyEvent.KEYCODE_PERIOD:
aChar = '.';
break;
case KeyEvent.KEYCODE_MINUS:
aChar = mCaps ? '_' : '-';
break;
case KeyEvent.KEYCODE_SLASH:
aChar = '/';
break;
case KeyEvent.KEYCODE_BACKSLASH:
aChar = mCaps ? '|' : '\\';
break;
default:
aChar = 0;
break;
}
}
return aChar;
}
public interface OnScanSuccessListener {
public void onScanSuccess(String barcode);
}
public void setOnBarCodeCatchListener(OnScanSuccessListener onScanSuccessListener) {
mOnScanSuccessListener = onScanSuccessListener;
}
public void onDestroy() {
mHandler.removeCallbacks(mScanningFishedRunnable);
mOnScanSuccessListener = null;
}
}
package com.miya.fastcashier.viewmodel package com.miya.fastcashier.viewmodel
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.miya.fastcashier.beans.LoginRequest
import com.miya.fastcashier.service.LoginService
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
class PayViewModel : ViewModel() {
fun refund(userName:String,passWord:String) {
viewModelScope.launch {
val refundResult = async(Dispatchers.IO) {
booksRepository.getAllBooks()
}
refundResult,
}
}
class MainViewModel : ViewModel() {
// TODO: Implement the ViewModel
} }
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="300"
android:fromYDelta="100%p"
android:toYDelta="0"/>
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="300"
android:fromXDelta="100%p"
android:toXDelta="0"/>
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="300"
android:fromYDelta="0"
android:toYDelta="100%p"/>
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="300"
android:fromXDelta="0"
android:toXDelta="-100%p"/>
</set>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tvRefund"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请扫码退款"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.258" />
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
<resources>
<style name="BottomInAndOutStyle">
<item name="android:windowEnterAnimation">@anim/in_bottom</item>
<item name="android:windowExitAnimation">@anim/out_bottom</item>
</style>
</resources>
rootProject.name = "MiYaFastCashier" rootProject.name = "MiYaFastCashier"
include ':app' include ':app'
include ':core'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment