From a7122dc68e33424c51f168e388ebe6292ea0d216 Mon Sep 17 00:00:00 2001 From: priagungs <priagungsatyagama@gmail.com> Date: Sat, 23 Feb 2019 10:17:31 +0700 Subject: [PATCH] migrate to viewpager --- android/.idea/misc.xml | 2 +- .../main/java/com/chatman/AuthActivity.java | 37 ++++-- .../main/java/com/chatman/MainActivity.java | 86 ++----------- .../chatman/adapter/MainViewPagerAdapter.java | 50 ++++++++ .../java/com/chatman/fragment/ChatBot.java | 30 +++++ .../java/com/chatman/fragment/ChatList.java | 120 ++++++++++++++++++ .../app/src/main/res/layout/content_main.xml | 20 +-- .../src/main/res/layout/fragment_chat_bot.xml | 14 ++ .../main/res/layout/fragment_chat_list.xml | 29 +++++ android/app/src/main/res/values/strings.xml | 3 + 10 files changed, 292 insertions(+), 99 deletions(-) create mode 100644 android/app/src/main/java/com/chatman/adapter/MainViewPagerAdapter.java create mode 100644 android/app/src/main/java/com/chatman/fragment/ChatBot.java create mode 100644 android/app/src/main/java/com/chatman/fragment/ChatList.java create mode 100644 android/app/src/main/res/layout/fragment_chat_bot.xml create mode 100644 android/app/src/main/res/layout/fragment_chat_list.xml diff --git a/android/.idea/misc.xml b/android/.idea/misc.xml index 9fdea7e..37a7509 100644 --- a/android/.idea/misc.xml +++ b/android/.idea/misc.xml @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="UTF-8"?> <project version="4"> - <component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" project-jdk-name="1.8 (1)" project-jdk-type="JavaSDK"> + <component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" project-jdk-name="1.8" project-jdk-type="JavaSDK"> <output url="file://$PROJECT_DIR$/build/classes" /> </component> <component name="ProjectType"> diff --git a/android/app/src/main/java/com/chatman/AuthActivity.java b/android/app/src/main/java/com/chatman/AuthActivity.java index e3afc94..894c3d9 100644 --- a/android/app/src/main/java/com/chatman/AuthActivity.java +++ b/android/app/src/main/java/com/chatman/AuthActivity.java @@ -20,9 +20,13 @@ import com.bumptech.glide.Glide; import com.chatman.helper.FirebaseHelper; import com.chatman.helper.PreferencesHelper; import com.chatman.model.User; +import com.google.android.gms.tasks.OnCompleteListener; +import com.google.android.gms.tasks.Task; import com.google.firebase.database.DataSnapshot; import com.google.firebase.database.DatabaseError; import com.google.firebase.database.ValueEventListener; +import com.google.firebase.iid.FirebaseInstanceId; +import com.google.firebase.iid.InstanceIdResult; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; @@ -137,9 +141,9 @@ public class AuthActivity extends AppCompatActivity { // do register public void register(View view) { - String name = nameEditText.getText().toString().trim(); + final String name = nameEditText.getText().toString().trim(); final String email = emailEditText.getText().toString().trim(); - String password = passwordEditText.getText().toString().trim(); + final String password = passwordEditText.getText().toString().trim(); String confirm = confirmEditText.getText().toString().trim(); if (password.equals(confirm)) { @@ -159,18 +163,23 @@ public class AuthActivity extends AppCompatActivity { }); if (emailUnique[0] && !name.equals("") && !email.equals("") && !password.equals("") && !confirm.equals("") ) { - String key = FirebaseHelper.dbUser.push().getKey(); - User user = new User(key, name, email, md5(password)); - user.setKey(key); - PreferencesHelper.setUserFirebaseKey(this, key); - PreferencesHelper.setUserName(this, user.getName()); - PreferencesHelper.setHasLogin(this, true); - - FirebaseHelper.dbUser.child(key).setValue(user); - - - finish(); - startActivity(new Intent(this, MainActivity.class)); + FirebaseInstanceId.getInstance().getInstanceId().addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() { + @Override + public void onComplete(@NonNull Task<InstanceIdResult> task) { + String key = FirebaseHelper.dbUser.push().getKey(); + String instanceId = task.getResult().getToken(); + User user; + user = new User(instanceId, name, email, md5(password)); + user.setKey(instanceId); + PreferencesHelper.setUserFirebaseKey(AuthActivity.this, key); + PreferencesHelper.setUserName(AuthActivity.this, user.getName()); + PreferencesHelper.setHasLogin(AuthActivity.this, true); + + FirebaseHelper.dbUser.child(key).setValue(user); + finish(); + startActivity(new Intent(AuthActivity.this, MainActivity.class)); + } + }); } else { showSnackBar(this, "Email has been used"); } diff --git a/android/app/src/main/java/com/chatman/MainActivity.java b/android/app/src/main/java/com/chatman/MainActivity.java index ec7c46d..6fcae2d 100644 --- a/android/app/src/main/java/com/chatman/MainActivity.java +++ b/android/app/src/main/java/com/chatman/MainActivity.java @@ -6,6 +6,8 @@ import android.content.SharedPreferences; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; +import android.support.design.widget.TabLayout; +import android.support.v4.view.ViewPager; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.View; @@ -19,6 +21,7 @@ import android.view.Menu; import android.view.MenuItem; import com.chatman.adapter.ChatListAdapter; +import com.chatman.adapter.MainViewPagerAdapter; import com.chatman.helper.PreferencesHelper; import com.chatman.model.ChatList; @@ -31,7 +34,11 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On private RecyclerView recycler; private ChatListAdapter adapter; private List<ChatList> chatLists; + private Context context; + private MainViewPagerAdapter pagerAdapter; + private ViewPager viewPager; + private TabLayout tabLayout; @Override protected void onCreate(Bundle savedInstanceState) { @@ -41,16 +48,11 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On setSupportActionBar(toolbar); context = this; - // Recycler View - recycler = findViewById(R.id.chat_list_rv); - LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context); - linearLayoutManager.setReverseLayout(true); - linearLayoutManager.setStackFromEnd(true); - recycler.setLayoutManager(linearLayoutManager); - getChatLists(); - adapter = new ChatListAdapter(chatLists); - recycler.setAdapter(adapter); - + viewPager = findViewById(R.id.view_pager); + tabLayout =findViewById(R.id.tab_layout); + pagerAdapter = new MainViewPagerAdapter(getSupportFragmentManager(), this); + viewPager.setAdapter(pagerAdapter); + tabLayout.setupWithViewPager(viewPager); DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( @@ -117,68 +119,4 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On } // Todo: Ganti data dummmy jadi data asli ngambil dari database - private List<ChatList> getChatLists() { - chatLists = new ArrayList<>(); - chatLists.add(new ChatList( - R.drawable.priagung, - "Priagung Satyagama", - "Woi sini ke sekre 2 temani diriku yang kesepian" - )); - chatLists.add(new ChatList( - R.drawable.rama, - "Yusuf Rahmat Pratama", - "Eh, gmn gmn?" - )); - chatLists.add(new ChatList( - R.drawable.priagung, - "Priagung Satyagama", - "Woi sini ke sekre 2 temani diriku yang kesepian" - )); - chatLists.add(new ChatList( - R.drawable.rama, - "Yusuf Rahmat Pratama", - "Eh, gmn gmn?" - )); - chatLists.add(new ChatList( - R.drawable.priagung, - "Priagung Satyagama", - "Woi sini ke sekre 2 temani diriku yang kesepian" - )); - chatLists.add(new ChatList( - R.drawable.rama, - "Yusuf Rahmat Pratama", - "Eh, gmn gmn?" - )); - chatLists.add(new ChatList( - R.drawable.priagung, - "Priagung Satyagama", - "Woi sini ke sekre 2 temani diriku yang kesepian" - )); - chatLists.add(new ChatList( - R.drawable.rama, - "Yusuf Rahmat Pratama", - "Eh, gmn gmn?" - )); - chatLists.add(new ChatList( - R.drawable.priagung, - "Priagung Satyagama", - "Woi sini ke sekre 2 temani diriku yang kesepian" - )); - chatLists.add(new ChatList( - R.drawable.rama, - "Yusuf Rahmat Pratama", - "Eh, gmn gmn?" - )); - chatLists.add(new ChatList( - R.drawable.priagung, - "Priagung Satyagama", - "Woi sini ke sekre 2 temani diriku yang kesepian" - )); - chatLists.add(new ChatList( - R.drawable.rama, - "Yusuf Rahmat Pratama", - "Eh, gmn gmn?" - )); - return chatLists; - } } diff --git a/android/app/src/main/java/com/chatman/adapter/MainViewPagerAdapter.java b/android/app/src/main/java/com/chatman/adapter/MainViewPagerAdapter.java new file mode 100644 index 0000000..8eb3237 --- /dev/null +++ b/android/app/src/main/java/com/chatman/adapter/MainViewPagerAdapter.java @@ -0,0 +1,50 @@ +package com.chatman.adapter; + +import android.content.Context; +import android.support.annotation.Nullable; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentManager; +import android.support.v4.app.FragmentStatePagerAdapter; + +import com.chatman.fragment.ChatBot; +import com.chatman.fragment.ChatList; + +public class MainViewPagerAdapter extends FragmentStatePagerAdapter { + private Context context; + public MainViewPagerAdapter(FragmentManager fm, Context context) { + super(fm); + this.context = context; + } + + @Override + public Fragment getItem(int i) { + switch (i) { + case 0: + ChatList chatList = new ChatList(); + chatList.setContext(context); + return chatList; + case 1: + return new ChatBot(); + } + return null; + } + + @Override + public int getCount() { + return 2; + } + + @Nullable + @Override + public CharSequence getPageTitle(int position) { + super.getPageTitle(position); + switch (position) { + case 0: + return "Chats"; + case 1: + return "Bot"; + default: + return null; + } + } +} diff --git a/android/app/src/main/java/com/chatman/fragment/ChatBot.java b/android/app/src/main/java/com/chatman/fragment/ChatBot.java new file mode 100644 index 0000000..422908b --- /dev/null +++ b/android/app/src/main/java/com/chatman/fragment/ChatBot.java @@ -0,0 +1,30 @@ +package com.chatman.fragment; + + +import android.os.Bundle; +import android.support.v4.app.Fragment; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import com.chatman.R; + +/** + * A simple {@link Fragment} subclass. + */ +public class ChatBot extends Fragment { + + + public ChatBot() { + // Required empty public constructor + } + + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + // Inflate the layout for this fragment + return inflater.inflate(R.layout.fragment_chat_bot, container, false); + } + +} diff --git a/android/app/src/main/java/com/chatman/fragment/ChatList.java b/android/app/src/main/java/com/chatman/fragment/ChatList.java new file mode 100644 index 0000000..1401eb1 --- /dev/null +++ b/android/app/src/main/java/com/chatman/fragment/ChatList.java @@ -0,0 +1,120 @@ +package com.chatman.fragment; + + +import android.content.Context; +import android.os.Bundle; +import android.support.v4.app.Fragment; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import com.chatman.R; +import com.chatman.adapter.ChatListAdapter; + +import java.util.ArrayList; +import java.util.List; + +/** + * A simple {@link Fragment} subclass. + */ +public class ChatList extends Fragment { + + private RecyclerView recycler; + private ChatListAdapter adapter; + private List<com.chatman.model.ChatList> chatLists; + private Context context; + + public ChatList() { + + } + + public void setContext(Context context) { + this.context = context; + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + // Inflate the layout for this fragment + + View rootView = inflater.inflate(R.layout.fragment_chat_list, container, false); + recycler = rootView.findViewById(R.id.chat_list_rv); + LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context); + linearLayoutManager.setReverseLayout(true); + linearLayoutManager.setStackFromEnd(true); + recycler.setLayoutManager(linearLayoutManager); + getChatLists(); + adapter = new ChatListAdapter(chatLists); + recycler.setAdapter(adapter); + + return rootView; + } + + private List<com.chatman.model.ChatList> getChatLists() { + chatLists = new ArrayList<>(); + chatLists.add(new com.chatman.model.ChatList( + R.drawable.priagung, + "Priagung Satyagama", + "Woi sini ke sekre 2 temani diriku yang kesepian" + )); + chatLists.add(new com.chatman.model.ChatList( + R.drawable.rama, + "Yusuf Rahmat Pratama", + "Eh, gmn gmn?" + )); + chatLists.add(new com.chatman.model.ChatList( + R.drawable.priagung, + "Priagung Satyagama", + "Woi sini ke sekre 2 temani diriku yang kesepian" + )); + chatLists.add(new com.chatman.model.ChatList( + R.drawable.rama, + "Yusuf Rahmat Pratama", + "Eh, gmn gmn?" + )); + chatLists.add(new com.chatman.model.ChatList( + R.drawable.priagung, + "Priagung Satyagama", + "Woi sini ke sekre 2 temani diriku yang kesepian" + )); + chatLists.add(new com.chatman.model.ChatList( + R.drawable.rama, + "Yusuf Rahmat Pratama", + "Eh, gmn gmn?" + )); + chatLists.add(new com.chatman.model.ChatList( + R.drawable.priagung, + "Priagung Satyagama", + "Woi sini ke sekre 2 temani diriku yang kesepian" + )); + chatLists.add(new com.chatman.model.ChatList( + R.drawable.rama, + "Yusuf Rahmat Pratama", + "Eh, gmn gmn?" + )); + chatLists.add(new com.chatman.model.ChatList( + R.drawable.priagung, + "Priagung Satyagama", + "Woi sini ke sekre 2 temani diriku yang kesepian" + )); + chatLists.add(new com.chatman.model.ChatList( + R.drawable.rama, + "Yusuf Rahmat Pratama", + "Eh, gmn gmn?" + )); + chatLists.add(new com.chatman.model.ChatList( + R.drawable.priagung, + "Priagung Satyagama", + "Woi sini ke sekre 2 temani diriku yang kesepian" + )); + chatLists.add(new com.chatman.model.ChatList( + R.drawable.rama, + "Yusuf Rahmat Pratama", + "Eh, gmn gmn?" + )); + return chatLists; + } + +} diff --git a/android/app/src/main/res/layout/content_main.xml b/android/app/src/main/res/layout/content_main.xml index 9f85354..cc8d45e 100644 --- a/android/app/src/main/res/layout/content_main.xml +++ b/android/app/src/main/res/layout/content_main.xml @@ -1,22 +1,22 @@ <?xml version="1.0" encoding="utf-8"?> -<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" +<LinearLayout 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:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context=".MainActivity" + android:orientation="vertical" tools:showIn="@layout/app_bar_main"> - <android.support.v7.widget.RecyclerView - android:id="@+id/chat_list_rv" + <android.support.design.widget.TabLayout + android:id="@+id/tab_layout" android:layout_width="match_parent" - android:layout_height="match_parent" - app:layout_constraintStart_toStartOf="parent" - app:layout_constraintEnd_toEndOf="parent" - app:layout_constraintTop_toTopOf="parent" - app:layout_constraintBottom_toBottomOf="parent"> + android:layout_height="wrap_content"/> - </android.support.v7.widget.RecyclerView> + <android.support.v4.view.ViewPager + android:id="@+id/view_pager" + android:layout_width="match_parent" + android:layout_height="match_parent"/> -</android.support.constraint.ConstraintLayout> \ No newline at end of file +</LinearLayout> \ No newline at end of file diff --git a/android/app/src/main/res/layout/fragment_chat_bot.xml b/android/app/src/main/res/layout/fragment_chat_bot.xml new file mode 100644 index 0000000..ba28517 --- /dev/null +++ b/android/app/src/main/res/layout/fragment_chat_bot.xml @@ -0,0 +1,14 @@ +<?xml version="1.0" encoding="utf-8"?> +<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:tools="http://schemas.android.com/tools" + android:layout_width="match_parent" + android:layout_height="match_parent" + tools:context=".fragment.ChatBot"> + + <!-- TODO: Update blank fragment layout --> + <TextView + android:layout_width="match_parent" + android:layout_height="match_parent" + android:text="@string/hello_blank_fragment" /> + +</FrameLayout> \ No newline at end of file diff --git a/android/app/src/main/res/layout/fragment_chat_list.xml b/android/app/src/main/res/layout/fragment_chat_list.xml new file mode 100644 index 0000000..a8365f8 --- /dev/null +++ b/android/app/src/main/res/layout/fragment_chat_list.xml @@ -0,0 +1,29 @@ +<?xml version="1.0" encoding="utf-8"?> +<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:tools="http://schemas.android.com/tools" + android:layout_width="match_parent" + android:layout_height="match_parent" + tools:context=".fragment.ChatList"> + + <android.support.constraint.ConstraintLayout + xmlns:app="http://schemas.android.com/apk/res-auto" + android:layout_width="match_parent" + android:layout_height="match_parent" + app:layout_behavior="@string/appbar_scrolling_view_behavior" + tools:context=".MainActivity" + tools:showIn="@layout/app_bar_main"> + + <android.support.v7.widget.RecyclerView + android:id="@+id/chat_list_rv" + android:layout_width="match_parent" + android:layout_height="match_parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintTop_toTopOf="parent" + app:layout_constraintBottom_toBottomOf="parent"> + + </android.support.v7.widget.RecyclerView> + + </android.support.constraint.ConstraintLayout> + +</FrameLayout> \ No newline at end of file diff --git a/android/app/src/main/res/values/strings.xml b/android/app/src/main/res/values/strings.xml index d8880fc..3c0a395 100644 --- a/android/app/src/main/res/values/strings.xml +++ b/android/app/src/main/res/values/strings.xml @@ -19,5 +19,8 @@ <string name="register">Daftar</string> <string name="register_word">Belum punya akun?</string> <string name="login_word">Sudah memiliki akun?</string> + + <!-- TODO: Remove or change this placeholder text --> + <string name="hello_blank_fragment">Hello blank fragment</string> </resources> -- GitLab