Skip to content

Commit

Permalink
Support landscape
Browse files Browse the repository at this point in the history
  • Loading branch information
OmerShoulstein committed Jun 24, 2022
1 parent 2640e70 commit 038998a
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 25 deletions.
26 changes: 18 additions & 8 deletions android-client/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
android:theme="@style/Theme.MaKore"
android:usesCleartextTraffic="true"
tools:targetApi="31">
<activity
android:name=".MainActivityLandscape"
android:exported="false"
android:configChanges="orientation|screenSize|keyboardHidden"
/>

<service
android:name=".MessageService"
Expand All @@ -29,22 +34,26 @@

<activity
android:name=".chat.AddContactActivity"
android:configChanges="orientation|screenSize|keyboardHidden"
android:exported="false"
android:theme="@style/Theme.MaKore.NoActionBar"
android:label="@string/title_activity_add_contact" />
android:label="@string/title_activity_add_contact"
android:theme="@style/Theme.MaKore.NoActionBar" />
<activity
android:name=".chat.SettingsActivity"
android:configChanges="orientation|screenSize|keyboardHidden"
android:exported="false"
android:theme="@style/Theme.MaKore.NoActionBar"
android:label="@string/title_activity_settings" />
android:label="@string/title_activity_settings"
android:theme="@style/Theme.MaKore.NoActionBar" />
<activity
android:name=".auth.SignUpActivity"
android:theme="@style/Theme.MaKore.NoActionBar"
android:exported="false" />
android:configChanges="orientation|screenSize|keyboardHidden"
android:exported="false"
android:theme="@style/Theme.MaKore.NoActionBar" />
<activity
android:name=".auth.SignInActivity"
android:theme="@style/Theme.MaKore.NoActionBar"
android:exported="true">
android:configChanges="orientation|screenSize|keyboardHidden"
android:exported="true"
android:theme="@style/Theme.MaKore.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand All @@ -53,6 +62,7 @@
</activity>
<activity
android:name=".MainActivity"
android:configChanges="orientation|screenSize|keyboardHidden"
android:exported="false"
android:theme="@style/Theme.MaKore.NoActionBar" />
</application>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.example.makore;

import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
Expand Down Expand Up @@ -27,14 +29,16 @@
public class ChatFragment extends Fragment {

private FragmentChatBinding binding;
// private ContactsListAdapter adapter;
private ContactsViewModel viewModel;
private MessageListAdapter adapter;
private String contactId;

@Override
public View onCreateView(
@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Bundle savedInstanceState
) {

binding = FragmentChatBinding.inflate(inflater, container, false);
return binding.getRoot();
Expand All @@ -43,15 +47,26 @@ public View onCreateView(

public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
viewModel = new ViewModelProvider(this).get(ContactsViewModel.class);
viewModel = new ViewModelProvider(requireActivity()).get(ContactsViewModel.class);
viewModel.reload();
contactId = null;
// Get bundle from previous fragment
Bundle bundle = getArguments();
if (bundle != null) {
contactId = bundle.getString("contactId");
String name = bundle.getString("contactName");
}
viewModel.getContactIdLiveData().observe(getViewLifecycleOwner(), id -> {
contactId = id;
if (contactId != null) {
// Set contact name
viewModel.setContactId(contactId);
RecyclerView messagesRecyclerView = binding.lstMessages;
adapter = new MessageListAdapter(getContext());
messagesRecyclerView.setAdapter(adapter);
messagesRecyclerView.setLayoutManager(new androidx.recyclerview.widget.LinearLayoutManager(getContext()));

viewModel.getMessages().observe(getViewLifecycleOwner(), messages -> {
adapter.setMessages(viewModel.getMessagesWithContact());
System.out.println("Messages: " + messages);
});
}
});

if (contactId != null) {
RecyclerView messagesRecyclerView = binding.lstMessages;
adapter = new MessageListAdapter(getContext());
Expand Down Expand Up @@ -104,4 +119,11 @@ public void onDestroyView() {
binding = null;
}

@Override
public void onConfigurationChanged(@NonNull Configuration newConfig) {
super.onConfigurationChanged(newConfig);
//Return to main activity
Intent intent = new Intent(getContext(), MainActivity.class);
startActivity(intent);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.makore;

import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
Expand Down Expand Up @@ -38,10 +39,9 @@ public View onCreateView(
Intent intent = new Intent(getActivity(), AddContactActivity.class);
startActivity(intent);
});
viewModel = new ViewModelProvider(this).get(ContactsViewModel.class);
viewModel = new ViewModelProvider(requireActivity()).get(ContactsViewModel.class);

return binding.getRoot();

}

public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
Expand All @@ -68,13 +68,14 @@ public void onDestroyView() {

@Override
public void onContactClick(View v, Contact contact) {
// Navigate to chat fragment
// Create bundle with contact info
Bundle bundle = new Bundle();
bundle.putString("contactId", contact.getId());
bundle.putString("contactName", contact.getName());
NavHostFragment.findNavController(ContactsFragment.this)
.navigate(R.id.action_ContactsFragment_to_ChatFragment, bundle);
// Set current contact
viewModel.setLiveContactId(contact.getId());
// If portrait, go to chat fragment
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
// Navigate to chat fragment
NavHostFragment.findNavController(ContactsFragment.this)
.navigate(R.id.action_ContactsFragment_to_ChatFragment);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationManagerCompat;
import androidx.navigation.NavController;
Expand Down Expand Up @@ -45,6 +47,10 @@ private void initDB() {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int orientation = getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
this.onConfigurationChanged(getResources().getConfiguration());
}

binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
Expand Down Expand Up @@ -120,4 +126,11 @@ public boolean onSupportNavigateUp() {
return NavigationUI.navigateUp(navController, appBarConfiguration)
|| super.onSupportNavigateUp();
}

@Override
public void onConfigurationChanged(@NonNull Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Intent intent = new Intent(this, MainActivityLandscape.class);
startActivity(intent);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.example.makore;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivityLandscape extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_landscape);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,21 @@
public class ContactsViewModel extends ViewModel {
private final ContactsRepository contactsRepository;
private String contactId;
private final MutableLiveData<String> contactIdLiveData;

public ContactsViewModel() {
contactsRepository = new ContactsRepository();
contactIdLiveData = new MutableLiveData<>();
contactId = null;
}

public MutableLiveData<String> getContactIdLiveData() {
return contactIdLiveData;
}

public void setLiveContactId(String contactId) {
contactIdLiveData.setValue(contactId);
}

public MutableLiveData<List<Contact>> getContacts() {
reload();
Expand Down
24 changes: 24 additions & 0 deletions android-client/app/src/main/res/layout/activity_main_landscape.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivityLandscape">

<fragment
android:id="@+id/nav_host_fragment_content_main"
android:name="com.example.makore.ContactsFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="41" />

<fragment
android:id="@+id/nav_host_fragment_chat_main"
android:name="com.example.makore.ChatFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="59" />


</LinearLayout>

0 comments on commit 038998a

Please sign in to comment.