Skip to content

Commit

Permalink
demonstrate more useful situation eg: login logout
Browse files Browse the repository at this point in the history
  • Loading branch information
Aspsine committed Sep 3, 2016
1 parent 844001f commit dc903d4
Show file tree
Hide file tree
Showing 26 changed files with 453 additions and 101 deletions.
20 changes: 12 additions & 8 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "24.0.0 rc1"
compileSdkVersion 24
buildToolsVersion "24.0.2"

defaultConfig {
applicationId "com.aspsine.fragmentnavigator.demo"
minSdkVersion 11
targetSdkVersion 23
versionCode 2
versionName "2.0"
targetSdkVersion 24
versionCode 3
versionName "3.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
repositories {
maven { url "https://jitpack.io" }
}
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile project(':library')
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:support-v4:23.3.0'
// compile 'com.github.Aspsine:FragmentNavigator:1.0.2'
compile 'com.android.support:appcompat-v7:24.2.0'
compile 'com.android.support:design:24.2.0'
testCompile 'junit:junit:4.12'
}
14 changes: 12 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,32 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.aspsine.fragmentnavigator.demo">

<!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:name=".ui.activity.MainActivity"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ExceptionActivity" />

<activity android:name=".ui.activity.ExceptionActivity" />

<activity
android:name=".ui.activity.LoginActivity"
android:label="@string/title_activity_login"/>
</application>

</manifest>
10 changes: 10 additions & 0 deletions app/src/main/java/com/aspsine/fragmentnavigator/demo/Action.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.aspsine.fragmentnavigator.demo;

/**
* Created by aspsine on 16/9/3.
*/

public class Action {
public static final String LOGIN = Action.class.getName() + ".LOGIN";
public static final String LOGOUT = Action.class.getName() + ".LOGOUT";
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.aspsine.fragmentnavigator.demo.broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;

import com.aspsine.fragmentnavigator.demo.Action;

/**
* Created by aspsine on 16/9/3.
*/

public class BroadcastManager {

public static void register(Context context, BroadcastReceiver receiver, String... actions){
IntentFilter filter = new IntentFilter();
for (String action: actions){
filter.addAction(action);
}
LocalBroadcastManager.getInstance(context).registerReceiver(receiver, filter);
}

public static void unregister(Context context, BroadcastReceiver receiver){
LocalBroadcastManager.getInstance(context).unregisterReceiver(receiver);
}

public static void sendLoginBroadcast(Context context, int position){
Intent intent = new Intent(Action.LOGIN);
intent.putExtra("EXTRA_POSITION", position);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}

public static void sendLogoutBroadcast(Context context, int position){
Intent intent = new Intent(Action.LOGOUT);
intent.putExtra("EXTRA_POSITION", position);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.aspsine.fragmentnavigator.demo;
package com.aspsine.fragmentnavigator.demo.ui.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import com.aspsine.fragmentnavigator.demo.R;

public class ExceptionActivity extends AppCompatActivity {

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.aspsine.fragmentnavigator.demo.ui.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.aspsine.fragmentnavigator.demo.R;
import com.aspsine.fragmentnavigator.demo.broadcast.BroadcastManager;
import com.aspsine.fragmentnavigator.demo.utils.SharedPrefUtils;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener{

private EditText etEmail;
private EditText etPassword;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
etEmail = (EditText) findViewById(R.id.et_email);
etPassword = (EditText)findViewById(R.id.et_password);
Button button = (Button) findViewById(R.id.login_in_button);
button.setOnClickListener(this);
}

@Override
public void onClick(View v) {
if (v.getId() == R.id.login_in_button){
tryLogin();
}
}

void tryLogin(){
String email = String.valueOf(etEmail.getText()).trim();
String password = String.valueOf(etPassword.getText()).trim();

if(check(email, password)){
markUserLogin();
notifyUserLogin();
finish();
}
}

boolean check(String email, String password){
if (TextUtils.isEmpty(email)){
etEmail.setError(getString(R.string.error_invalid_email));
return false;
}
if (TextUtils.isEmpty(password)){
etPassword.setError(getString(R.string.error_invalid_password));
return false;
}
return true;
}

private void markUserLogin(){
SharedPrefUtils.login(this);
}

private void notifyUserLogin(){
BroadcastManager.sendLoginBroadcast(this, 1);
}
}

Loading

0 comments on commit dc903d4

Please sign in to comment.