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

Commit

Permalink
login activity add import btn
Browse files Browse the repository at this point in the history
  • Loading branch information
itning committed Nov 28, 2018
1 parent 75af83b commit a78e313
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,44 @@
package top.itning.yunshuclassschedule.ui.activity;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.AppCompatButton;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Objects;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import top.itning.yunshuclassschedule.R;
import top.itning.yunshuclassschedule.common.App;
import top.itning.yunshuclassschedule.common.BaseActivity;
import top.itning.yunshuclassschedule.common.ConstantPool;
import top.itning.yunshuclassschedule.entity.ClassSchedule;
import top.itning.yunshuclassschedule.entity.ClassScheduleDao;
import top.itning.yunshuclassschedule.entity.DataEntity;
import top.itning.yunshuclassschedule.entity.EventEntity;
import top.itning.yunshuclassschedule.util.DateUtils;
import top.itning.yunshuclassschedule.util.ThemeChangeUtil;

import static top.itning.yunshuclassschedule.ui.activity.ShareActivity.FILE_SELECT_CODE;
import static top.itning.yunshuclassschedule.ui.activity.ShareActivity.TIME_LIST_SIZE;

/**
* 登陆
*
Expand All @@ -31,6 +51,8 @@ public class LoginActivity extends BaseActivity {

@BindView(R.id.btn_custom)
AppCompatButton btnCustom;
@BindView(R.id.btn_share)
AppCompatButton btnShare;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -89,4 +111,104 @@ public void onCustomBtnClicked() {
startActivity(new Intent(this, CustomActivity.class));
finish();
}

@OnClick(R.id.btn_share)
public void onViewClicked() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(Intent.createChooser(intent, "选择课程数据文件进行导入"), FILE_SELECT_CODE);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, "没有找到文件管理APP", Toast.LENGTH_SHORT).show();
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == FILE_SELECT_CODE && resultCode == RESULT_OK) {
doImportFile(data);
}
super.onActivityResult(requestCode, resultCode, data);
}

private void doImportFile(Intent data) {
Uri uri = data.getData();
if (uri == null) {
Toast.makeText(this, "解析失败", Toast.LENGTH_LONG).show();
return;
}
Log.d(TAG, "File Uri: " + uri.toString());
try (InputStreamReader i = new InputStreamReader(Objects.requireNonNull(getContentResolver().openInputStream(uri)));
BufferedReader reader = new BufferedReader(i)) {
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
Gson gson = new Gson();
DataEntity dataEntity = gson.fromJson(stringBuilder.toString(), DataEntity.class);
List<ClassSchedule> classScheduleList = dataEntity.getClassScheduleList();
List<String> timeList = dataEntity.getTimeList();
if (classScheduleList == null || classScheduleList.isEmpty() || timeList == null || timeList.isEmpty() || timeList.size() != TIME_LIST_SIZE) {
Toast.makeText(this, "解析失败", Toast.LENGTH_LONG).show();
return;
}
new AlertDialog.Builder(this)
.setTitle("警告")
.setMessage("即将导入课程数据,这会将原有课程信息清空,确定导入吗?")
.setPositiveButton("确定", (dialog, which) -> {
TreeMap<String, String> timeMap = new TreeMap<>();
timeMap.put("1", timeList.get(0));
timeMap.put("2", timeList.get(1));
timeMap.put("3", timeList.get(2));
timeMap.put("4", timeList.get(3));
timeMap.put("5", timeList.get(4));
if (!DateUtils.isDataLegitimate(timeMap, this)) {
Toast.makeText(this, "解析失败", Toast.LENGTH_LONG).show();
return;
}
if (App.sharedPreferences.edit()
.putString("1", timeList.get(0))
.putString("2", timeList.get(1))
.putString("3", timeList.get(2))
.putString("4", timeList.get(3))
.putString("5", timeList.get(4))
.commit()) {
DateUtils.refreshTimeList();
} else {
Toast.makeText(this, "解析失败", Toast.LENGTH_LONG).show();
return;
}
ClassScheduleDao classScheduleDao = ((App) getApplication()).getDaoSession().getClassScheduleDao();
classScheduleDao.deleteAll();
for (ClassSchedule classSchedule : classScheduleList) {
classScheduleDao.insert(classSchedule);
}
EventBus.getDefault().post(new EventEntity(ConstantPool.Int.TIME_TICK_CHANGE, ""));
if (classScheduleList.size() == classScheduleDao.count()) {
Toast.makeText(this, "导入成功", Toast.LENGTH_LONG).show();
enterMainActivity();
} else {
Toast.makeText(this, "写入数据库失败,请重试", Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("取消", null)
.show();
} catch (Exception e) {
Log.e(TAG, " ", e);
Toast.makeText(this, "解析失败", Toast.LENGTH_LONG).show();
}
}

private void enterMainActivity() {
App.sharedPreferences.edit()
.putString(ConstantPool.Str.APP_CLASS_SCHEDULE_VERSION.get(), "")
.putString(ConstantPool.Str.USER_USERNAME.get(), "test")
.putString(ConstantPool.Str.USER_CLASS_ID.get(), "-1")
.putBoolean(ConstantPool.Str.FIRST_IN_APP.get(), false)
.apply();
startActivity(new Intent(this, MainActivity.class));
finish();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@
public class ShareActivity extends BaseActivity {
private static final String TAG = "ShareActivity";

private static final int FILE_SELECT_CODE = 1;
public static final int FILE_SELECT_CODE = 1;
public static final int TIME_LIST_SIZE = 5;
private static final int WRITE_REQUEST_CODE = 2;
private static final int TIME_LIST_SIZE = 5;

@BindView(R.id.tv_import_title)
AppCompatTextView tvImportTitle;
Expand Down
46 changes: 33 additions & 13 deletions app/src/main/res/layout/activity_login.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,42 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.activity.LoginActivity"
tools:layout_editor_absoluteY="81dp">
tools:layout_editor_absoluteY="89dp">

<android.support.v7.widget.AppCompatButton
android:id="@+id/btn_custom"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:text="@string/login_btn_custom"
android:textColor="#fff"
android:theme="@style/AppTheme"
app:backgroundTint="@color/colorAccent"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintTop_toTopOf="parent">

<android.support.v7.widget.AppCompatButton
android:id="@+id/btn_share"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:layout_marginBottom="5dp"
android:text="@string/login_btn_share"
android:textColor="#fff"
android:theme="@style/AppTheme"
app:backgroundTint="@color/colorAccent" />

<android.support.v7.widget.AppCompatButton
android:id="@+id/btn_custom"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:text="@string/login_btn_custom"
android:textColor="#fff"
android:theme="@style/AppTheme"
app:backgroundTint="@color/colorAccent" />

</LinearLayout>
</android.support.constraint.ConstraintLayout>
3 changes: 2 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
<string name="navigation_drawer_open">打开导航抽屉</string>
<string name="navigation_drawer_close">关闭导航抽屉</string>
<string name="action_set_text_size">设置字体大小</string>
<string name="login_name">登陆</string>
<string name="login_name">欢迎使用</string>
<string name="login_btn_share">导入课程数据</string>
<string name="action_set_background_image">更换背景图片</string>
<string name="action_show_teacher_info">显示授课教师</string>
<string name="check_score_input_name">姓名</string>
Expand Down

0 comments on commit a78e313

Please sign in to comment.